What is prom-client?
The prom-client npm package is a client for the Prometheus monitoring tool, which allows you to collect and expose metrics from your Node.js applications. It supports various metric types, custom labels, and can be integrated with the default Node.js metrics and other systems.
What are prom-client's main functionalities?
Counter
Counters are a metric that only goes up (e.g., page views, total number of requests).
const { Counter } = require('prom-client');
const httpRequestsTotal = new Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests'
});
httpRequestsTotal.inc(); // Increment by 1
httpRequestsTotal.inc(5); // Increment by a given value
Gauge
Gauges are a metric that can go up or down (e.g., memory usage, number of active users).
const { Gauge } = require('prom-client');
const memoryUsageGauge = new Gauge({
name: 'memory_usage_bytes',
help: 'Memory usage in bytes'
});
memoryUsageGauge.set(process.memoryUsage().heapUsed); // Set to current heap used
Histogram
Histograms track the size and number of events in buckets (e.g., request durations or response sizes).
const { Histogram } = require('prom-client');
const httpRequestDuration = new Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
buckets: [0.1, 0.2, 0.5, 1, 5]
});
httpRequestDuration.observe(0.4); // Observe a value in seconds
Summary
Summaries calculate percentiles of observed values (e.g., long-tail request durations).
const { Summary } = require('prom-client');
const httpRequestDurationSummary = new Summary({
name: 'http_request_duration_summary',
help: 'Summary of HTTP request durations',
percentiles: [0.5, 0.9, 0.99]
});
httpRequestDurationSummary.observe(0.3); // Observe a value in seconds
Default Metrics
Collect default metrics from Node.js runtime and system, such as CPU usage, event loop lag, etc.
const { collectDefaultMetrics } = require('prom-client');
collectDefaultMetrics();
Other packages similar to prom-client
metrics-client
This package is similar to prom-client in that it provides a way to collect and expose metrics for monitoring purposes. However, it is not specifically tied to Prometheus and can be used with other monitoring tools.
appmetrics-prometheus
This package is a Prometheus monitoring and metrics exporter for Node.js applications. It is similar to prom-client but is built on top of the appmetrics library, which provides additional insights into the Node.js runtime.
express-prom-bundle
This package is an Express middleware that automatically records request durations and exposes them in Prometheus format. It is similar to prom-client but is more focused on Express applications and provides out-of-the-box middleware for easy integration.
#Prometheus client for node.js
A prometheus client for node.js that supports histogram, gauges and counters.