Merge pull request #46 from aaronleesmith/feature/customizable-prom-registry

Allows for customizing promRegistry when creating prom bundle.
This commit is contained in:
Konstantin Pogorelov
2020-07-22 18:10:08 +02:00
committed by GitHub
4 changed files with 14 additions and 6 deletions

View File

@@ -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

View File

@@ -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

3
types/index.d.ts vendored
View File

@@ -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;

View File

@@ -46,6 +46,7 @@ promBundle({
collectDefaultMetrics: {
}
},
promRegistry: new promClient.Registry(),
urlValueParser: {
minHexLength: 5,
extraMasks: [