first commit
This commit is contained in:
33
src/metrics/metrics.module.ts
Normal file
33
src/metrics/metrics.module.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import {
|
||||
PrometheusModule,
|
||||
makeCounterProvider,
|
||||
makeGaugeProvider,
|
||||
} from '@willsoto/nestjs-prometheus';
|
||||
import { MetricsService } from './metrics.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PrometheusModule.register({
|
||||
path: '/metrics',
|
||||
defaultMetrics: { enabled: true },
|
||||
}),
|
||||
],
|
||||
providers: [
|
||||
MetricsService,
|
||||
makeCounterProvider({
|
||||
name: 'sso_logins_total',
|
||||
help: 'Total number of successful logins',
|
||||
}),
|
||||
makeCounterProvider({
|
||||
name: 'sso_logins_failed_total',
|
||||
help: 'Total number of failed login attempts',
|
||||
}),
|
||||
makeGaugeProvider({
|
||||
name: 'sso_active_sessions',
|
||||
help: 'Number of active sessions',
|
||||
}),
|
||||
],
|
||||
exports: [MetricsService],
|
||||
})
|
||||
export class MetricsModule {}
|
||||
27
src/metrics/metrics.service.ts
Normal file
27
src/metrics/metrics.service.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectMetric } from '@willsoto/nestjs-prometheus';
|
||||
import { Counter, Gauge } from 'prom-client';
|
||||
|
||||
@Injectable()
|
||||
export class MetricsService {
|
||||
constructor(
|
||||
@InjectMetric('sso_logins_total')
|
||||
private readonly loginsCounter: Counter<string>,
|
||||
@InjectMetric('sso_logins_failed_total')
|
||||
private readonly failedLoginsCounter: Counter<string>,
|
||||
@InjectMetric('sso_active_sessions')
|
||||
private readonly activeSessionsGauge: Gauge<string>,
|
||||
) {}
|
||||
|
||||
incrementLogins(): void {
|
||||
this.loginsCounter.inc();
|
||||
}
|
||||
|
||||
incrementFailedLogins(): void {
|
||||
this.failedLoginsCounter.inc();
|
||||
}
|
||||
|
||||
setActiveSessions(count: number): void {
|
||||
this.activeSessionsGauge.set(count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user