{"library":"prometheus-middleware","title":"Prometheus Middleware for Node.js HTTP Servers","description":"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.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install prometheus-middleware"],"cli":null},"imports":["import APM from 'prometheus-middleware';\n// Or for the class itself:\nimport { APM } from 'prometheus-middleware';","const APM = require('prometheus-middleware');","import APM from 'prometheus-middleware';\nconst apm = new APM();\nconst counter = new apm.client.Counter({\n  name: 'my_counter',\n  help: 'A custom counter metric'\n});"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import APM from 'prometheus-middleware';\nimport http from 'http';\n\nconst apm = new APM({\n  PORT: 9350,\n  METRICS_ROUTE: '/metrics'\n});\napm.init();\n\n// Example of adding a custom metric\nconst customRequestsCounter = new apm.client.Counter({\n  name: 'custom_http_requests_total',\n  help: 'Total number of custom HTTP requests',\n  labelNames: ['method', 'route', 'code']\n});\n\n// Create a simple HTTP server to demonstrate the middleware\nconst server = http.createServer((req, res) => {\n  if (req.url === '/test') {\n    customRequestsCounter.inc({ method: req.method, route: '/test', code: 200 });\n    res.writeHead(200, { 'Content-Type': 'text/plain' });\n    res.end('Hello from /test\\n');\n  } else {\n    res.writeHead(404, { 'Content-Type': 'text/plain' });\n    res.end('Not Found\\n');\n  }\n});\n\nserver.listen(3000, () => {\n  console.log('HTTP server listening on port 3000');\n  console.log('Prometheus metrics available at http://localhost:9350/metrics');\n});\n\n// Graceful shutdown to destroy the APM instance and its HTTP server\nprocess.on('SIGTERM', () => {\n  console.log('SIGTERM received, destroying APM...');\n  apm.destroy();\n  server.close(() => {\n    console.log('HTTP server closed.');\n    process.exit(0);\n  });\n});","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}