{"library":"prometheus-api-metrics","title":"Prometheus API Monitoring for Node.js","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install prometheus-api-metrics"],"cli":null},"imports":["import apiMetrics from 'prometheus-api-metrics';","const apiMetrics = require('prometheus-api-metrics');","import type { Options } from 'prometheus-api-metrics';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import express from 'express';\nimport apiMetrics from 'prometheus-api-metrics';\nimport { collectDefaultMetrics, register } from 'prom-client';\n\nconst app = express();\nconst port = 3000;\n\n// Start collecting default metrics like CPU, memory, etc.\n// Interval must be a number, use Number() just in case for env vars\ncollectDefaultMetrics({ prefix: 'my_app_', timeout: Number(process.env.DEFAULT_METRICS_INTERVAL || 10000) });\n\n// Use the API metrics middleware\napp.use(apiMetrics({\n  metricsPath: '/metrics',\n  durationBuckets: [0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 1, 2, 5],\n  metricsPrefix: 'my_service_',\n  excludeRoutes: ['/health']\n}));\n\n// Define a health check endpoint\napp.get('/health', (req, res) => {\n  res.status(200).send('OK');\n});\n\n// Define a sample API endpoint\napp.get('/data', (req, res) => {\n  setTimeout(() => {\n    res.json({ message: 'Hello, Prometheus!' });\n  }, Math.random() * 500);\n});\n\n// Expose the metrics endpoint for Prometheus to scrape\napp.get('/metrics', async (req, res) => {\n  try {\n    res.set('Content-Type', register.contentType);\n    res.end(await register.metrics());\n  } catch (ex) {\n    res.status(500).end(ex);\n  }\n});\n\napp.listen(port, () => {\n  console.log(`Server listening on http://localhost:${port}`);\n  console.log(`Metrics available at http://localhost:${port}/metrics`);\n});\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}