The `prometheus-middleware` package provides a straightforward solution for integrating Prometheus metrics into Node.js HTTP applications. Currently at version 1.4.2, it appears to maintain an active development cadence given its continuous integration setup and recent releases. It acts as an abstraction layer, setting up an HTTP server to expose metrics, instantiating and exposing a `prom-client` instance, and patching the native Node.js HTTP server to automatically track request-response times. A key differentiator is its out-of-the-box support for popular Node.js HTTP frameworks like Express and Fastify, along with the default Node.js HTTP server. It also includes sensible defaults, such as normalizing 404 error paths to prevent high cardinality issues in Prometheus, and automatically collects metrics for event loop, garbage collection, CPU, and memory usage. Developers can also easily define custom metrics using the underlying `prom-client` instance.
npm install prometheus-middlewareVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes `prometheus-middleware` to expose metrics on port 9350 at `/metrics`. It also demonstrates adding a custom Prometheus counter using `apm.client.Counter` and integrates it into a basic Node.js HTTP server. A graceful shutdown mechanism is included.
If unique 404 path labels are required, initialize APM with `new APM({ NORMALIZE_ENDPOINT: false })`. Be aware of potential high cardinality with this setting.Ensure your Node.js environment is updated to version 16.0.0 or later. The package officially tests and supports Node.js 18.x, 20.x, and 22.x.
For production deployments, use Node.js versions 18.x, 20.x, or 22.x to benefit from tested compatibility. If using 16.x, ensure thorough application-level testing.
If your project is an ESM module (e.g., `"type": "module"` in package.json), use `import APM from 'prometheus-middleware';`. If it's CommonJS, use `const APM = require('prometheus-middleware');`.For CommonJS, use `const APM = require('prometheus-middleware');`. For ESM, use `import APM from 'prometheus-middleware';` or `import { APM } from 'prometheus-middleware';`.Review your `prometheus-middleware` configuration. The default behavior normalizes 404 paths. If this is not sufficient, consider implementing custom path normalization using a library like `express-url-trimmer` before `prometheus-middleware` in your HTTP server's middleware chain, or ensure `NORMALIZE_ENDPOINT` is not set to `false` without careful consideration.