Skip to content

Commit 98d9659

Browse files
committed
fix(prometheus): add generic argument for registry
1 parent df7ad97 commit 98d9659

8 files changed

Lines changed: 2540 additions & 1407 deletions

File tree

package-lock.json

Lines changed: 2499 additions & 1384 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/prometheus/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
"homepage": "https://github.com/routup/plugins#readme",
4848
"peerDependencies": {
49-
"prom-client": ">=13.0.0",
49+
"prom-client": ">=15.0.0",
5050
"routup": "^4.0.1"
5151
},
5252
"devDependencies": {

packages/prometheus/src/handler.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import {
55
useRequestPath,
66
} from 'routup';
77
import type {
8-
LabelValues,
9-
Registry,
8+
LabelValues, PrometheusContentType,
9+
Registry, RegistryContentType,
1010
} from 'prom-client';
1111
import promClient from 'prom-client';
1212
import { buildRequestDurationMetric, buildUptimeMetric } from './metrics';
1313
import type { Metrics, Options } from './type';
1414
import { onResponseFinished } from './utils';
1515

16-
export function createHandler(input?: Registry) {
17-
const registry : Registry = input || promClient.register;
16+
export function createHandler<
17+
T extends RegistryContentType = PrometheusContentType,
18+
>(input?: Registry<T>) {
19+
const registry : Registry<T> = input || promClient.register as Registry<T>;
1820

1921
return coreHandler(async (req, res, next) => {
2022
registry.metrics()
@@ -26,9 +28,11 @@ export function createHandler(input?: Registry) {
2628
});
2729
}
2830

29-
export function registerMetrics(
31+
export function registerMetrics<
32+
T extends RegistryContentType = PrometheusContentType,
33+
>(
3034
router: Router,
31-
options: Options,
35+
options: Options<T>,
3236
): Metrics {
3337
/* istanbul ignore next */
3438
if (options.collectDefaultMetrics) {

packages/prometheus/src/metrics/request-duration.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import type { Histogram, Summary } from 'prom-client';
1+
import type {
2+
Histogram, PrometheusContentType, Registry, RegistryContentType, Summary,
3+
} from 'prom-client';
24
import promClient from 'prom-client';
35
import { MetricTypeName } from '../constants';
46
import type { Options } from '../type';
57

6-
export function buildRequestDurationMetric(options: Options) : Histogram | Summary {
8+
export function buildRequestDurationMetric<
9+
T extends RegistryContentType = PrometheusContentType,
10+
>(options: Options<T>) : Histogram | Summary {
711
const labels : string[] = [
812
'method',
913
'path',
@@ -20,7 +24,7 @@ export function buildRequestDurationMetric(options: Options) : Histogram | Summa
2024
help: `duration (sec) summary of http responses labeled with: ${labels.join(', ')}`,
2125
labelNames: labels,
2226
percentiles: [0.5, 0.75, 0.95, 0.98, 0.99, 0.999],
23-
registers: [options.registry],
27+
registers: [options.registry as Registry],
2428
...options.requestDurationSummary,
2529
});
2630
}
@@ -30,7 +34,7 @@ export function buildRequestDurationMetric(options: Options) : Histogram | Summa
3034
help: `duration (sec) histogram of http responses labeled with: ${labels.join(', ')}`,
3135
labelNames: labels,
3236
buckets: [0.003, 0.03, 0.1, 0.3, 1.5, 10],
33-
registers: [options.registry],
37+
registers: [options.registry as Registry],
3438
...options.requestDurationHistogram,
3539
});
3640
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { performance } from 'node:perf_hooks';
2-
import type { Gauge } from 'prom-client';
2+
import type {
3+
Gauge, PrometheusContentType, Registry, RegistryContentType,
4+
} from 'prom-client';
35
import promClient from 'prom-client';
46
import type { Options } from '../type';
57

6-
export function buildUptimeMetric(options: Options) : Gauge {
8+
export function buildUptimeMetric<
9+
T extends RegistryContentType = PrometheusContentType,
10+
>(options: Options<T>) : Gauge {
711
const starTime = performance.now();
812

913
return new promClient.Gauge({
@@ -13,7 +17,7 @@ export function buildUptimeMetric(options: Options) : Gauge {
1317
this.set(parseInt(`${((performance.now() - starTime) / 1000)}`, 10));
1418
},
1519
registers: [
16-
options.registry,
20+
options.registry as Registry,
1721
],
1822
});
1923
}

packages/prometheus/src/module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import type { PrometheusContentType, RegistryContentType } from 'prom-client';
12
import type { Plugin } from 'routup';
23
import { createHandler, registerMetrics } from './handler';
34
import type { Options, OptionsInput } from './type';
45
import { buildOptions } from './utils';
56

6-
export function prometheus(input: OptionsInput = {}) : Plugin {
7+
export function prometheus<
8+
T extends RegistryContentType = PrometheusContentType,
9+
>(input: OptionsInput<T> = {}) : Plugin {
710
const options = buildOptions({
811
...(input || {}),
912
});

packages/prometheus/src/type.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type {
44
Gauge,
55
Histogram,
66
HistogramConfiguration,
7-
LabelValues,
8-
Registry,
7+
LabelValues, PrometheusContentType,
8+
Registry, RegistryContentType,
99
Summary,
1010
SummaryConfiguration,
1111
} from 'prom-client';
@@ -18,7 +18,7 @@ export type Metrics = {
1818

1919
export type LabelTransformer = (labels: LabelValues<string>, req: Request, res: Response) => void;
2020

21-
export type Options = {
21+
export type Options<T extends RegistryContentType = PrometheusContentType> = {
2222
metricsPath: string,
2323

2424
requestDuration: boolean,
@@ -36,8 +36,8 @@ export type Options = {
3636
normalizePath?: ((path: string, req: Request) => string);
3737
skip: (req: Request) => boolean;
3838

39-
collectDefaultMetrics?: DefaultMetricsCollectorConfiguration,
40-
registry: Registry;
39+
collectDefaultMetrics?: DefaultMetricsCollectorConfiguration<T>,
40+
registry: Registry<T>;
4141
};
4242

43-
export type OptionsInput = Partial<Options>;
43+
export type OptionsInput<T extends RegistryContentType = PrometheusContentType> = Partial<Options<T>>;

packages/prometheus/src/utils/options.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import type { PrometheusContentType, Registry, RegistryContentType } from 'prom-client';
12
import promClient from 'prom-client';
23
import { MetricName, MetricTypeName } from '../constants';
34
import type { Options, OptionsInput } from '../type';
45

5-
export function buildOptions(input?: OptionsInput) : Options {
6+
export function buildOptions<
7+
T extends RegistryContentType = PrometheusContentType,
8+
>(input?: OptionsInput<T>) : Options<T> {
69
input = input || {};
710

811
return {
@@ -14,7 +17,7 @@ export function buildOptions(input?: OptionsInput) : Options {
1417
requestDurationType: MetricTypeName.HISTOGRAM,
1518

1619
skip: (req) => false,
17-
registry: promClient.register,
20+
registry: promClient.register as Registry<T>,
1821

1922
metricsPath: '/metrics',
2023

0 commit comments

Comments
 (0)