Prometheus metrics in Node.js: prom-client library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Prometheus metrics in Node.js: prom-client library



Prometheus is a powerful open-source monitoring and alerting system that collects metrics from your applications and infrastructure. For integrating Prometheus with Node.js applications, there is an official and most popular library prom-client. It provides ready-made tools for creating, registering, and exporting metrics in a format that Prometheus understands.



In this article, we will thoroughly examine how to install and use prom-client, what metric types are available, and how to set up an endpoint for data collection.



Installing prom-client



The library is installed using the standard npm package manager:



npm install prom-client


After installation, you can import the library into your project:



const client = require('prom-client');


Main Prometheus metric types



Prometheus supports four main metric types, each serving its own purpose:



  • Counter — a counter that can only increase (e.g., number of requests, errors).
  • Gauge — a value that can both increase and decrease (e.g., number of active users, memory usage).
  • Histogram — a histogram for measuring value distribution (e.g., request response time).
  • Summary — similar to a histogram, but calculates quantiles on the application side.


Examples of using prom-client



Let's look at creating each metric type using a simple Express application as an example.



1. Counter



A counter is ideal for counting the number of events, such as HTTP requests or errors.



const httpRequestsCounter = new client.Counter({  name: 'http_requests_total',  help: 'Total number of HTTP requests',  labelNames: ['method', 'status']});

// Usage:httpRequestsCounter.inc({ method: 'GET', status: 200 });httpRequestsCounter.inc({ method: 'POST', status: 500 });


2. Gauge



A Gauge allows you to track a current value that can change in both directions.



const activeUsersGauge = new client.Gauge({  name: 'active_users',  help: 'Number of active users'});

// Increase by 1:activeUsersGauge.inc();// Decrease by 1:activeUsersGauge.dec();// Set a specific value:activeUsersGauge.set(42);


3. Histogram



A histogram helps analyze the distribution of operation execution time.



const requestDurationHistogram = new client.Histogram({  name: 'http_request_duration_seconds',  help: 'Duration of HTTP requests in seconds',  labelNames: ['method', 'route'],  buckets: [0.1, 0.5, 1, 2, 5] // bucket boundaries (in seconds)});

// Timing:const end = requestDurationHistogram.startTimer();// ... request execution ...end({ method: 'GET', route: '/api/users' });


4. Summary



A Summary is similar to a histogram but provides precise quantiles (e.g., median, 95th percentile).



const requestDurationSummary = new client.Summary({  name: 'http_request_duration_summary_seconds',  help: 'Summary of HTTP request duration in seconds',  labelNames: ['method', 'route'],  percentiles: [0.5, 0.9, 0.99] // quantiles for calculation});

// Usage is similar to histogram:const endSummary = requestDurationSummary.startTimer();// ... request execution ...endSummary({ method: 'GET', route: '/api/data' });


Setting up an endpoint for Prometheus



For Prometheus to collect metrics, you need to create a separate HTTP endpoint (usually /metrics) that will return da

Recommendations