Skip to content

Commit

Permalink
feat: ability to set defaultLabels
Browse files Browse the repository at this point in the history
Labels will be passed directly into `promClient.register.setDefaultLabels`

Closes #1209

Signed-off-by: Will Soto <willsoto@users.noreply.github.com>
  • Loading branch information
willsoto committed Mar 9, 2022
1 parent 79a67f1 commit 7fd289a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export interface PrometheusOptions {
path?: string;
/** {@inheritDoc PrometheusDefaultMetrics} */
defaultMetrics?: PrometheusDefaultMetrics;
/**
* Will be passed into `setDefaultLabels`
*
* {@link https://github.com/siimon/prom-client#default-labels-segmented-by-registry}
*/
// Using this type to match what prom-client specifies.
// eslint-disable-next-line @typescript-eslint/ban-types
defaultLabels?: Object;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DynamicModule, Module, Provider } from "@nestjs/common";
import { collectDefaultMetrics } from "prom-client";
import * as promClient from "prom-client";
import { PROMETHEUS_OPTIONS } from "./constants";
import { PrometheusController } from "./controller";
import {
Expand Down Expand Up @@ -99,7 +99,11 @@ export class PrometheusModule {

private static configureServer(options: Required<PrometheusOptions>): void {
if (options.defaultMetrics.enabled) {
collectDefaultMetrics(options.defaultMetrics.config);
promClient.collectDefaultMetrics(options.defaultMetrics.config);
}

if (Object.keys(options.defaultLabels).length > 0) {
promClient.register.setDefaultLabels(options.defaultLabels);
}

Reflect.defineMetadata("path", options.path, options.controller);
Expand All @@ -115,6 +119,7 @@ export class PrometheusModule {
config: {},
},
controller: PrometheusController,
defaultLabels: {},
...options,
};
}
Expand Down

0 comments on commit 7fd289a

Please sign in to comment.