NodeJS Prometheus Wrapper
Wrapper to official NodeJS Prometheus exporter (prom-client)
WHY
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("<counter-name>").inc()
EXAMPLES
Execute this command to launch an express server exposing some metrics. Browse localhost:8080/metrics to monitor the metrics (data is changing after 10s).
$ node examples/index.js
See the source files in /examples to see suggestions of implementation.
HOW TO
init.js
var prometheus = ; var express = ;var app = ; prometheus; app; // Counterprometheus; // Gaugeprometheus; // Histogramprometheus; // Summaryprometheus; app;
wherever-in-your-code.js
var prometheus = ; ; prometheus; var end = prometheus;; for var i = 0; i < 100000; ++i prometheus;
Exposed /metrics
:
$ 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"} 0myapp_myhistogram_bucket{le="30"} 1myapp_myhistogram_bucket{le="60"} 1myapp_myhistogram_bucket{le="300"} 1myapp_myhistogram_bucket{le="600"} 1myapp_myhistogram_bucket{le="1800"} 1myapp_myhistogram_bucket{le="3600"} 1myapp_myhistogram_bucket{le="+Inf"} 1myapp_myhistogram_sum 15.002myapp_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.009997170550270069myapp_mysummary{quantile="0.1"} 0.09957970759409267myapp_mysummary{quantile="0.5"} 0.5016970195079504myapp_mysummary{quantile="0.9"} 0.8993228241841542myapp_mysummary{quantile="0.99"} 0.9901868947762174myapp_mysummary_sum 4999.807495853165myapp_mysummary_count 10000
Labels
Example:
var prometheus = ; // Init Counterprometheus; ; prometheus;
$ curl 127.0.0.1:8080 # HELP myapp_mycounter A number we occasionally increment. # TYPE myapp_mycounter counter myapp_mycounter{foo="bar"} 42myapp_mycounter{foo="baz"} 42
Full list of metrics 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 <basename> exposes multiple time series during a scrape:
- cumulative counters for the observation buckets, exposed as <basename>_bucket{le="<upper inclusive bound>"}
- the total sum of all observed values, exposed as <basename>_sum
- the count of events that have been observed, exposed as <basename>_count (identical to <basename>_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 <basename> exposes multiple time series during a scrape:
- streaming φ-quantiles (0 ≤ φ ≤ 1) of observed events, exposed as <basename>{quantile="<φ>"}
- the total sum of all observed values, exposed as <basename>_sum
- the count of events that have been observed, exposed as <basename>_count
Methods available
setNamespace
client.setNamespace(<namespace>)
=> prefixes every metric name
getMetrics
client.getMetrics()
=> what to expose in your server under /metrics
Counter
client.createCounter(<name>, <help>, [ <label-list> ])
client.get(<name>).get()
client.get(<name>).inc()
client.get(<name>).inc(<delta>)
Gauge
client.createGauge(<name>, <help>, [ <label-list> ])
client.get(<name>).get()
client.get(<name>).set(<value>)
client.get(<name>).setToCurrentTime()
=> expose a timestamp in msvar end = client.get(<name>).startTimer()
=> callend()
to stop the timer, expose a timestamp in seconds
Histogram
client.createHistogram(<name>, <help>, buckets: [ <categories> ], [ <label-list> ])
client.get(<name>).get()
client.get(<name>).observe(<value>)
client.get(<name>).reset()
var end = client.get(<name>).startTimer()
=> callend()
to stop the timer, expose a timestamp in seconds
Summary
client.createSummary(<name>, <help>, buckets: [ <categories> ], [ <label-list> ])
client.get(<name>).get()
client.get(<name>).observe(<value>)
client.get(<name>).reset()
var end = client.get(<name>).startTimer()
=> callend()
to stop the timer, expose a timestamp in seconds