diff --git a/README.md b/README.md index 0659326..89aa3b5 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ Which labels to include in `http_request_duration_seconds` metric: * **metricsPath**: replace the `/metrics` route with a **regex** or exact **string**. Note: it is highly recommended to just stick to the default * **metricType**: histogram/summary selection. See more details below + ### metricType option ### Two metric types are supported for `http_request_duration_seconds` metric: @@ -85,6 +86,7 @@ Additional options for **summary**: * **promClient**: options for promClient startup, e.g. **collectDefaultMetrics**. This option was added to keep `express-prom-bundle` runnable using confit (e.g. with kraken.js) without writing any JS code, see [advanced example](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/advanced-example.js) +* **promRegistry**: Optional `promClient.Registry` instance to attach metrics to. Defaults to global `promClient.register`. ### More details on includePath option diff --git a/package-lock.json b/package-lock.json index b5e66bf..5361f5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "express-prom-bundle", - "version": "5.1.4", + "version": "5.1.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/index.js b/src/index.js index 9da8924..bf747fc 100644 --- a/src/index.js +++ b/src/index.js @@ -50,7 +50,8 @@ function main(opts) { normalizePath: main.normalizePath, formatStatusCode: main.normalizeStatusCode, metricType: 'histogram', - promClient: {} + promClient: {}, + promRegistry: promClient.register }, opts ); @@ -92,14 +93,16 @@ function main(opts) { labelNames: labels, percentiles: opts.percentiles || [0.5, 0.75, 0.95, 0.98, 0.99, 0.999], maxAgeSeconds: opts.maxAgeSeconds, - ageBuckets: opts.ageBuckets + ageBuckets: opts.ageBuckets, + registers: [opts.promRegistry] }); } else if (opts.metricType === 'histogram' || !opts.metricType) { return new promClient.Histogram({ name: httpMetricName, help: 'duration histogram of http responses labeled with: ' + labels.join(', '), labelNames: labels, - buckets: opts.buckets || [0.003, 0.03, 0.1, 0.3, 1.5, 10] + buckets: opts.buckets || [0.003, 0.03, 0.1, 0.3, 1.5, 10], + registers: [opts.promRegistry] }); } else { throw new Error('metricType option must be histogram or summary'); @@ -113,14 +116,15 @@ function main(opts) { if (opts.includeUp !== false) { metrics.up = new promClient.Gauge({ name: 'up', - help: '1 = up, 0 = not up' + help: '1 = up, 0 = not up', + registers: [opts.promRegistry] }); metrics.up.set(1); } const metricsMiddleware = function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); - res.end(promClient.register.metrics()); + res.end(opts.promRegistry.metrics()); }; const metricsMatch = opts.metricsPath instanceof RegExp ? opts.metricsPath diff --git a/types/index.d.ts b/types/index.d.ts index 1a77877..d5645c4 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,7 @@ // TypeScript Version: 2.8 import { Request, RequestHandler, Response } from 'express'; -import { DefaultMetricsCollectorConfiguration } from 'prom-client'; +import { DefaultMetricsCollectorConfiguration, Registry } from 'prom-client'; export {}; @@ -31,6 +31,7 @@ declare namespace express_prom_bundle { metricType?: 'summary' | 'histogram'; metricsPath?: string; promClient?: { collectDefaultMetrics?: DefaultMetricsCollectorConfiguration }; + promRegistry?: Registry, normalizePath?: NormalizePathEntry[] | NormalizePathFn; formatStatusCode?: NormalizeStatusCodeFn; transformLabels?: TransformLabelsFn; diff --git a/types/test.ts b/types/test.ts index 354be4b..c002c43 100644 --- a/types/test.ts +++ b/types/test.ts @@ -47,6 +47,7 @@ promBundle({ timeout: 1000 } }, + promRegistry: new promClient.Registry(), urlValueParser: { minHexLength: 5, extraMasks: [