http-metrics-middleware is an Express.js middleware designed to integrate Prometheus metrics into Node.js applications, currently stable at version 2.2.1. It provides a convenient wrapper around `prom-client`, offering a set of default HTTP request metrics such as duration histograms and summaries, as well as status code counts. The library maintains a regular release cadence, primarily driven by dependency updates, security patches, and minor enhancements. Key differentiators include its configurable options for metric labels, such as normalized URL paths, and custom time/quantile buckets, allowing fine-grained control over the collected data. While primarily built for Express, it can be seamlessly integrated into Koa applications using `koa-connect`. It focuses on providing a robust, opinionated solution for common HTTP metrics without requiring extensive manual setup of individual `prom-client` metrics.
npm install http-metrics-middlewareVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up `http-metrics-middleware` with Express, configure a custom metrics path, and integrate a custom Prometheus counter using the exposed `promClient` instance.
Upgrade your Node.js runtime environment to version 10 or higher to ensure compatibility.
Set `includePath: false` in the middleware options or ensure your `normalizePath` function or Express route definitions heavily restrict path parameters to a limited set of known values.
Carefully review the changelogs of `express` and `prom-client` when upgrading `http-metrics-middleware` to newer versions to anticipate any breaking changes in their APIs or behaviors.
No direct user action is typically required for production code, but be aware if your CI/CD or local development environment relied on `@after-work.js/aw`.
Use the CommonJS `require` syntax: `const MetricsMiddleware = require('http-metrics-middleware')`.Ensure you have called `app.use(metrics.initRoutes())` after creating an instance of `MetricsMiddleware`.
Either disable the conflicting default metric via `http-metrics-middleware` options (e.g., `enableDurationHistogram: false`) or choose a unique name for your custom metric.
Ensure requests are hitting your application. Place `app.use(metrics.initRoutes())` early in your middleware chain, preferably before any routes that you want to measure. Custom metrics will only appear if their `inc()` or `observe()` methods are called.