Prometheus Metrics for Express

8.0.0 · active · verified Wed Apr 22

express-prom-bundle is an Express.js middleware designed to integrate popular Prometheus metrics into an application with minimal configuration. It bundles essential metrics, notably `up` and `http_request_duration_seconds`, which can be configured as either a histogram or a summary and labeled with HTTP status codes, methods, and paths. The current stable version is 8.0.0, which requires Express.js v5 and `prom-client` v15 or higher. The library maintains a steady release cadence, incorporating dependabot updates and new features, with major versions typically aligning with peer dependency updates. A key differentiator is its focus on ease of use, providing a consolidated solution for common Express metric needs, as well as features like path normalization and custom label support. Its middleware order dependency allows for selective metric collection, and it explicitly supports both CommonJS and ESM environments, shipping with TypeScript type definitions.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up `express-prom-bundle` with an Express application, including basic routes and custom labels, then exposes the metrics endpoint.

import express from 'express';
import promBundle from 'express-prom-bundle';

const app = express();

// Configure the metrics middleware
const metricsMiddleware = promBundle({
  includeMethod: true,
  includePath: true, // IMPORTANT: only measures routes registered AFTER this middleware
  customLabels: { service_name: 'my-express-app' },
  promClient: { collectDefaultMetrics: {} } // Collect default Node.js process metrics
});

// Register the metrics middleware BEFORE your routes
app.use(metricsMiddleware);

// Define your application routes
app.get('/', (req, res) => {
  res.send('Hello, Prometheus!');
});

app.get('/api/users/:id', (req, res) => {
  // Simulate an async operation
  setTimeout(() => {
    res.json({ id: req.params.id, name: `User ${req.params.id}` });
  }, Math.random() * 200);
});

app.get('/error', (req, res) => {
  res.status(500).send('Internal Server Error');
});

// The /metrics endpoint is automatically exposed by default
// Access it at http://localhost:3000/metrics
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Express app listening on port ${PORT}`);
  console.log(`Metrics available at http://localhost:${PORT}/metrics`);
});

view raw JSON →