https://github.com/iadvize/nodejs-prometheus-wrapper
Wrapper to official NodeJS Prometheus exporter
https://github.com/iadvize/nodejs-prometheus-wrapper
metrics nodejs prometheus
Last synced: 5 months ago
JSON representation
Wrapper to official NodeJS Prometheus exporter
- Host: GitHub
- URL: https://github.com/iadvize/nodejs-prometheus-wrapper
- Owner: iadvize
- License: mit
- Created: 2016-05-02T12:25:29.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-07-23T13:41:41.000Z (almost 4 years ago)
- Last Synced: 2025-08-19T04:09:29.999Z (10 months ago)
- Topics: metrics, nodejs, prometheus
- Language: JavaScript
- Homepage:
- Size: 39.1 KB
- Stars: 17
- Watchers: 36
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
NodeJS Prometheus Wrapper [](https://circleci.com/gh/iadvize/nodejs-prometheus-wrapper)
===========================
Wrapper to official NodeJS Prometheus exporter ([prom-client](https://github.com/siimon/prom-client))
The official NodeJS client for prometheus requires to transporter the metrics variables in your code (counters, gauges, histograms and summaries).
This small library allow to control variables from the whole code, in a singleton, by getting the metric by name, just like that: ```require('prometheus-wrapper').get("").inc()```
## Examples
init.js
```javascript
var prometheus = require("prometheus-wrapper");
var express = require('express');
var app = express();
prometheus.setNamespace("myapp");
app.get('/metrics', function(req, res) {
res.end(prometheus.getMetrics());
});
// Counter
prometheus.createCounter("mycounter", "A number we occasionally increment.");
// Gauge
prometheus.createGauge("mygauge", "A random number we occasionally set.");
// Histogram
prometheus.createHistogram("myhistogram", "A chat duration histogram.", {
buckets: [ 10, 30, 60, 300, 600, 1800, 3600 ]
});
// Summary
prometheus.createSummary("mysummary", "Compute quantiles and median of a random list of numbers.", {
percentiles: [ 0.01, 0.1, 0.5, 0.9, 0.99 ]
});
app.listen(8080);
```
wherever-in-your-code.js
```javascript
var prometheus = require("prometheus-wrapper");
setInterval(function () {
prometheus.get("mycounter").inc(42);
}, 10000);
prometheus.get("mygauge").set(42);
var end = prometheus.get("myhistogram").startTimer();
setTimeout(function () {
end();
}, 15000);
for (var i = 0; i < 100000; ++i) {
prometheus.get("mysummary").observe(Math.random());
}
```
Exposed ```/metrics``` :
```sh
$ curl 127.0.0.1:8080
# HELP myapp_mycounter A number we occasionally increment.
# TYPE myapp_mycounter counter
myapp_mycounter 84
# HELP myapp_mygauge A random number we occasionally set.
# TYPE myapp_mygauge gauge
myapp_mygauge 42
# HELP examples_myhistogram A chat duration histogram.
# TYPE examples_myhistogram histogram
myapp_myhistogram_bucket{le="10"} 0
myapp_myhistogram_bucket{le="30"} 1
myapp_myhistogram_bucket{le="60"} 1
myapp_myhistogram_bucket{le="300"} 1
myapp_myhistogram_bucket{le="600"} 1
myapp_myhistogram_bucket{le="1800"} 1
myapp_myhistogram_bucket{le="3600"} 1
myapp_myhistogram_bucket{le="+Inf"} 1
myapp_myhistogram_sum 15.002
myapp_myhistogram_count 1
# HELP myapp_mysummary Compute quantiles and median of a random list of numbers.
# TYPE myapp_mysummary summary
myapp_mysummary{quantile="0.01"} 0.009997170550270069
myapp_mysummary{quantile="0.1"} 0.09957970759409267
myapp_mysummary{quantile="0.5"} 0.5016970195079504
myapp_mysummary{quantile="0.9"} 0.8993228241841542
myapp_mysummary{quantile="0.99"} 0.9901868947762174
myapp_mysummary_sum 4999.807495853165
myapp_mysummary_count 10000
```
### Labels
Example:
```javascript
var prometheus = require("prometheus-wrapper");
// Init Counter
prometheus.createCounter("mycounter", "A number we occasionally increment.", ['foo']);
setInterval(function () {
prometheus.get("mycounter").inc({foo: 'bar'}, 42);
prometheus.get("mycounter").inc({foo: 'baz'}, 21);
}, 10000);
prometheus.get("mygauge").set(42);
```
```sh
$ curl 127.0.0.1:8080
# HELP myapp_mycounter A number we occasionally increment.
# TYPE myapp_mycounter counter
myapp_mycounter{foo="bar"} 42
myapp_mycounter{foo="baz"} 42
```
## Install
```
npm install --save prometheus-wrapper
```
## Documentation
### Full list of metrics types
[Official documentation](https://prometheus.io/docs/concepts/metric_types/)
#### Counter
```
A counter is a cumulative metric that represents a single numerical value that only ever goes up. A counter is typically used to count requests served, tasks completed, errors occurred, etc. Counters should not be used to expose current counts of items whose number can also go down, e.g. the number of currently running goroutines. Use gauges for this use case.
```
#### Gauge
```
A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
Gauges are typically used for measured values like temperatures or current memory usage, but also "counts" that can go up and down, like the number of running goroutines.
```
#### Histogram
```
A histogram samples observations (usually things like request durations or response sizes) and counts them in configurable buckets. It also provides a sum of all observed values.
A histogram with a base metric name of exposes multiple time series during a scrape:
- cumulative counters for the observation buckets, exposed as _bucket{le=""}
- the total sum of all observed values, exposed as _sum
- the count of events that have been observed, exposed as _count (identical to _bucket{le="+Inf"} above)
```
#### Summary
```
Similar to a histogram, a summary samples observations (usually things like request durations and response sizes). While it also provides a total count of observations and a sum of all observed values, it calculates configurable quantiles over a sliding time window.
A summary with a base metric name of exposes multiple time series during a scrape:
- streaming φ-quantiles (0 ≤ φ ≤ 1) of observed events, exposed as {quantile="<φ>"}
- the total sum of all observed values, exposed as _sum
- the count of events that have been observed, exposed as _count
```
### Methods available
#### setNamespace
- ```client.setNamespace()``` => prefixes every metric name
#### getMetrics
- ```client.getMetrics()``` => what to expose in your server under /metrics
#### Counter
- ```client.createCounter(, , [ ])```
- ```client.get().get()```
- ```client.get().inc()```
- ```client.get().inc()```
#### Gauge
- ```client.createGauge(, , [ ])```
- ```client.get().get()```
- ```client.get().set()```
- ```client.get().setToCurrentTime()``` => expose a timestamp in ms
- ```var end = client.get().startTimer()``` => call ```end()``` to stop the timer, expose a timestamp in seconds
#### Histogram
- ```client.createHistogram(, , buckets: [ ], [ ])```
- ```client.get().get()```
- ```client.get().observe()```
- ```client.get().reset()```
- ```var end = client.get().startTimer()``` => call ```end()``` to stop the timer, expose a timestamp in seconds
#### Summary
- ```client.createSummary(, , buckets: [ ], [ ])```
- ```client.get().get()```
- ```client.get().observe()```
- ```client.get().reset()```
- ```var end = client.get().startTimer()``` => call ```end()``` to stop the timer, expose a timestamp in seconds
## Contribute
Look at contribution guidelines here : [CONTRIBUTING.md](CONTRIBUTING.md)