This library provides comprehensive API and process monitoring for Node.js microservices, integrating with Prometheus through `prom-client`. It automatically collects metrics such as response time, request/response size, and server connection counts for each API call, in addition to standard process metrics recommended by Prometheus. It supports both Express and Koa frameworks and allows for custom metrics and exclusion of specific routes. The current stable version is 4.0.0. Releases are somewhat irregular, with significant updates typically every 1-2 years for major/minor versions. Key differentiators include its ease of integration as middleware, support for custom metrics, and the ability to collect HTTP request durations from common HTTP clients. Users must install `prom-client` as a peer dependency.
npm install prometheus-api-metricsVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up an Express server with the `prometheus-api-metrics` middleware, exposing API and process metrics at the `/metrics` endpoint for Prometheus scraping. It includes custom buckets, a metric prefix, and an excluded route.
Upgrade your Node.js environment to version 18 or higher to ensure compatibility.
Install the peer dependency: `npm install prom-client@'^15.0.0'` (adjust the version based on the latest compatible range, currently up to 16.x).
Place `app.use(apiMetrics())` early in your main application's middleware stack, ideally before other route handlers or dedicated error-handling middleware.
Configure the `defaultMetricsInterval` option when initializing `apiMetrics`: `app.use(apiMetrics({ defaultMetricsInterval: 5000 }))` for 5-second intervals. Ensure Prometheus scrape intervals are aligned.For CommonJS, use `const apiMetrics = require('prometheus-api-metrics');`. For ESM, use `import apiMetrics from 'prometheus-api-metrics';` as it is a default export.Install the peer dependency: `npm install prom-client` or `yarn add prom-client`. Check the `prom-client` version compatibility in `package.json`.
Ensure `apiMetrics()` and `collectDefaultMetrics()` are called only once during your application's setup phase. Verify that you don't have multiple instances of the middleware running.
Verify that `app.use(apiMetrics())` is properly placed in your Express/Koa application. Confirm the server is running and check for any custom `metricsPath` configuration in the middleware options.