Prometheus Client for Node.js

15.1.3 · active · verified Tue Apr 21

Prometheus client for Node.js (`prom-client`) is a comprehensive toolkit for instrumenting Node.js applications with Prometheus metrics. It supports all standard Prometheus metric types including counters, gauges, histograms, and summaries, alongside OpenMetrics and Exemplars which were introduced in version 15.0.0. The package is currently stable at version 15.1.3, with a release cadence that sees frequent patch and minor updates addressing bugs and adding features, while major versions are released less often to accommodate significant changes like Node.js version support adjustments and new Prometheus protocol features. Key differentiators include its robust support for Node.js `cluster` module aggregation, providing sensible defaults for common metrics, and a rich set of built-in 'default metrics' for runtime performance and resource usage. It ships with TypeScript types, enabling strong typing for metric definitions and interactions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up an Express.js server, collect default Node.js metrics, define a custom counter, and expose them on a /metrics endpoint, all using a dedicated Prometheus registry.

import express from 'express';
import { collectDefaultMetrics, Registry, Counter } from 'prom-client';

const app = express();
const port = process.env.PORT || 3000;

// Create a new registry to register custom metrics
const register = new Registry();

// Collect default metrics like CPU, memory, event loop lag, etc.
// These are registered to the custom 'register' instance.
collectDefaultMetrics({
  register: register,
  prefix: 'my_app_',
  labels: { APP_NAME: 'my_service' }
});

// Create a custom counter metric
const httpRequestCounter = new Counter({
  name: 'my_app_http_requests_total',
  help: 'Total number of HTTP requests',
  labelNames: ['method', 'route', 'status'],
  registers: [register]
});

// Example route to increment the counter
app.get('/', (req, res) => {
  httpRequestCounter.inc({ method: 'GET', route: '/', status: 200 });
  res.send('Hello World!');
});

// Expose metrics at the /metrics endpoint
app.get('/metrics', async (req, res) => {
  res.set('Content-Type', register.contentType);
  res.end(await register.metrics());
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
  console.log(`Metrics available at http://localhost:${port}/metrics`);
});

view raw JSON →