# fetch-prometheus

> Fetch module wrapper that submits request metrics to Prometheus

Latest version **1.0.6** (published 2018-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install fetch-prometheus
pnpm add fetch-prometheus
yarn add fetch-prometheus
bun add fetch-prometheus
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2018-12-16 |
| First published | 2018-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 130.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Roman Zaiats |
| Maintainers | roman_zaiats |
| Keywords | fetch, Fetch API, prometheus, prom-client, metrics |

## Links

- npm: https://www.npmjs.com/package/fetch-prometheus
- Repository: https://github.com/approce/fetch-prometheus
- Homepage: https://github.com/approce/fetch-prometheus#readme
- Issues: https://github.com/approce/fetch-prometheus/issues
- npm.io page: https://npm.io/package/fetch-prometheus

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^4.1.0
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.3.0
- [prom-client](https://npm.io/package/prom-client.md) ^11.2.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2018-12-16
- 1.0.5 — 2018-12-16
- 1.0.4 — 2018-12-16
- 1.0.3 — 2018-12-14
- 1.0.2 — 2018-12-14
- 1.0.1 — 2018-12-14
- 1.0.0 — 2018-12-14

## README

# Fetch Prometheus
This is a wrapper for 'node-fetch' module that submits metrics for all executed requests to Prometheus service.

## Install 
```
npm i --save fetch-prometheus
```

## Example

Basic example:
```js
const fetch = require('fetch-prometheus')(); //will use default node-fetch module to fetch data

fetch('https://api.ipify.org?format=json');
```

Custom fetch module usage:
```js
const fetchRetry = require('fetch-retry');
const fetch      = require('fetch-prometheus')(fetchRetry); //will use custom 'fetch-retry' module to fetch Data.

fetch('https://api.ipify.org?format=json');
```

Custom `publishURL` example:
```js
fetch('https://api.ipify.org?format=json', {
  publishURL: 'get_ip_request' //instead of publishing 'https://api.ipify.org?format=json' to prometheus it will publish metrics for this request under 'get_ip_request' request name. It should improve readability of metrics. 
});
```
## Example of submitted metrics at Grafana (dashboard for Prometheus):

Requests per minute chart:
<p align="center">
  <img src="https://github.com/approce/fetch-prometheus/blob/master/screenshots/performance.jpg?raw=true">
</p>

Request execution time in seconds:
<p align="center">
  <img src="https://github.com/approce/fetch-prometheus/blob/master/screenshots/execution_time.jpg?raw=true">
</p>


## Options added to default node-fetch options. 
| Name | Description |
| :-: | :- |
| publishURL |Customize name of request when publishing metrics to prometheus. Designed to increase readability of metrics at Prometheus |

## `prom-client` integration:

This module only submits metrics to Prometheus by using [prom-client](https://github.com/siimon/prom-client) module. Make sure you expose metrics as an endpoint so Prometheus can collect them, or make sure you push them by using PushGateway.

Example of exposing `prom-client` metrics assuming you use `Express.js`:

```js
const Prometheus = require('prom-client');
const express = require('express');

app.get('prometheus-metrics', (req, res) => {
  res.set('Content-Type', Prometheus.register.contentType);
  res.end(Prometheus.register.metrics());
});
```

Example of pushing metrics to `Prometheus` by using `PushGateway`:

```js
const {Pushgateway} = require('prom-client');
const gateway  = new Pushgateway('http://localhost:9091');

const push = () => gateway.push({jobName: 'test_job'}, err => {
  if (err) {
    console.error(`Error on pushing metrics to Prometheus. Reason: ${err.message}`);
  } else {
    console.log('Metrics pushed to Prometheus');
  }
});

setInterval(() => {
  push();
}, 10000);
```

For more detailed information visit [prom-client](https://github.com/siimon/prom-client) page.

## Metrics exposed
- `http_requests_total`: Counter for total requests received, has labels `url`, `method`, `status`, `generalized_status`
- `http_request_duration_seconds`: Duration of HTTP requests in seconds, has labels `url`, `method`, `status`, `generalized_status`

Label `generalized_status` is normalized to status code family groups, like `2XX` or `4XX`

Label `url` may be customized by passing custom `publishURL` in options

---
_Source: https://npm.io/package/fetch-prometheus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
