I am using prom-client in nodejs to publish a /metrics
endpoint. I want to monitor sales of varying amounts which occur sporadically over time.
What is the best way to track a sporadic or discontinuous metric in prometheus? None of the existing metric types seem to be a good fit.
- The basic prometheus metric type for tracking a single value (
Gauge
) is geared towards continuous data (such as CPU speed or concurrent requests). - The
Histogram
metric can capture discontinuous data, but requires manual percentiles and apparently only estimates quantiles (https://prometheus.io/docs/practices/histograms/#errors-of-quantile-estimation). Also the counts are wiped out when the metrics server restarts. - The
Summary
metric can capture discontinuous data, but is “in general not aggregatable” (https://latencytipoftheday.blogspot.com/2014/06/latencytipoftheday-you-cant-average.html).
Here is a simple setup with a Gauge
, which obviously does not capture the
import express from 'express'
import promClient, { Gauge } from 'prom-client'
export const someMetric = new Gauge({
name: 'some_metric',
help: 'Track some metric; type = [a, b, c]',
labelNames: ['one', 'two'],
})
const metricServer = express()
metricServer.get('/metrics', async (req, res) => {
console.log('Metrics scraped')
res
.set('content-type', 'text/plain')
.send(await promClient.register.metrics())
})
// intermittent callback that reports sales
service.onSale(value => {
// this will simply overwrite the previous sale :(
someMetric.labels('a', 'all').set(value)
})
metricServer.listen(9991, () =>
console.log(` Prometheus listening on http://localhost:9991/metrics`)
)
My current plan is to create a new database to internally track a rolling 24-hr average of sales, and then expose that as a single continuous metric to prometheus. It seems awkward to keep a rolling average internally in addition to prometheus’s aggregation capabilities though.