diff --git a/README.md b/README.md index bddaf84..15f585a 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ API and process monitoring with [Prometheus](https://prometheus.io) for Node.js - Process Metrics as recommended by Prometheus [itself](https://prometheus.io/docs/instrumenting/writing_clientlibs/#standard-and-runtime-collectors) - Endpoint to retrieve the metrics - used for Prometheus scraping - Prometheus format - - JSON format (`${path}.json`) + - JSON format (default to `${path}.json`, can be changed in options) - Support custom metrics - [Http function to collect request.js HTTP request duration](#requestjs-http-request-duration-collector) @@ -61,6 +61,7 @@ app.use(apiMetrics()) ### Options - metricsPath - Path to access the metrics. `default: /metrics` +- metricsJsonPath - Path to access the json formatted metrics. `default: {metricsPath}.json` - defaultMetricsInterval - the interval to collect the process metrics in milliseconds. `default: 10000` - durationBuckets - Buckets for response time in seconds. `default: [0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5]` - requestSizeBuckets - Buckets for request size in bytes. `default: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]` @@ -88,6 +89,7 @@ curl http[s]://:[port]/metrics.json 2. If you are using express framework and no route was found for the request (e.g: 404 status code), the request will not be collected. that's because we'll risk memory leak since the route is not a pattern but a hardcoded string. +3. You can specify your own metrics path via options. see : `metricsJsonPath` ## Custom Metrics diff --git a/src/express-middleware.js b/src/express-middleware.js index 544d036..36e6452 100644 --- a/src/express-middleware.js +++ b/src/express-middleware.js @@ -98,8 +98,8 @@ class ExpressMiddleware { res.set('Content-Type', Prometheus.register.contentType); return res.end(Prometheus.register.metrics()); } - if (routeUrl === `${this.setupOptions.metricsRoute}.json`) { - debug('Request to /metrics endpoint'); + if (routeUrl === this.setupOptions.metricsJsonRoute) { + debug('Request to /metrics/json endpoint'); return res.json(Prometheus.register.getMetricsAsJSON()); } @@ -121,4 +121,4 @@ class ExpressMiddleware { }; } -module.exports = ExpressMiddleware; +module.exports = ExpressMiddleware; \ No newline at end of file diff --git a/src/index.d.ts b/src/index.d.ts index 8783e7d..b54475a 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -12,6 +12,7 @@ export class HttpMetricsCollector { export interface ApiMetricsOpts { metricsPath?: string; + metricsJsonPath?: string, defaultMetricsInterval?: number; durationBuckets?: number[]; requestSizeBuckets?: number[]; diff --git a/src/metrics-middleware.js b/src/metrics-middleware.js index f2458a6..950cfb0 100644 --- a/src/metrics-middleware.js +++ b/src/metrics-middleware.js @@ -10,9 +10,10 @@ const setupOptions = {}; module.exports = (appVersion, projectName, framework = 'express') => { return (options = {}) => { - const { metricsPath, defaultMetricsInterval = 10000, durationBuckets, requestSizeBuckets, responseSizeBuckets, useUniqueHistogramName, metricsPrefix, excludeRoutes, includeQueryParams } = options; + const { metricsPath, metricsJsonPath, defaultMetricsInterval = 10000, durationBuckets, requestSizeBuckets, responseSizeBuckets, useUniqueHistogramName, metricsPrefix, excludeRoutes, includeQueryParams } = options; debug(`Init metrics middleware with options: ${JSON.stringify(options)}`); setupOptions.metricsRoute = metricsPath || '/metrics'; + setupOptions.metricsJsonRoute = metricsJsonPath || `${setupOptions.metricsRoute}.json`; setupOptions.excludeRoutes = excludeRoutes || []; setupOptions.includeQueryParams = includeQueryParams; setupOptions.defaultMetricsInterval = defaultMetricsInterval;