mirror of
https://github.com/apricote/Listory.git
synced 2026-02-06 17:57:03 +00:00
feat(api): add prometheus metrics
Currently we support metrics for the Node.js runtime and HTTP endpoints.
This commit is contained in:
parent
9869f0a061
commit
e2056b4734
6 changed files with 118 additions and 5 deletions
51
src/metrics/metrics.module.ts
Normal file
51
src/metrics/metrics.module.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { InboundMiddleware, PromModule } from "@digikare/nestjs-prom";
|
||||
import { DEFAULT_PROM_OPTIONS } from "@digikare/nestjs-prom/dist/prom.constants";
|
||||
import {
|
||||
DynamicModule,
|
||||
MiddlewareConsumer,
|
||||
Module,
|
||||
NestModule,
|
||||
} from "@nestjs/common";
|
||||
|
||||
// Dirty hack because we can not conditionally import modules based on
|
||||
// injected services and upstream module does not support dynamic configuration
|
||||
//
|
||||
// https://github.com/digikare/nestjs-prom/issues/27
|
||||
const promEnabled = process.env.PROMETHEUS_ENABLED === "true";
|
||||
|
||||
@Module({})
|
||||
export class MetricsModule implements NestModule {
|
||||
static forRoot(): DynamicModule {
|
||||
const module = {
|
||||
imports: [],
|
||||
providers: [],
|
||||
};
|
||||
if (promEnabled) {
|
||||
const promOptions = {
|
||||
metricPath: "/api/metrics",
|
||||
withDefaultsMetrics: true,
|
||||
withDefaultController: true,
|
||||
};
|
||||
|
||||
module.imports.push(PromModule.forRoot(promOptions));
|
||||
|
||||
module.providers.push({
|
||||
provide: DEFAULT_PROM_OPTIONS,
|
||||
useValue: promOptions,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
module: MetricsModule,
|
||||
...module,
|
||||
};
|
||||
}
|
||||
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
if (promEnabled) {
|
||||
// We register the Middleware ourselves to avoid tracking
|
||||
// latency for static files served for the frontend.
|
||||
consumer.apply(InboundMiddleware).forRoutes("/api");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue