Changing interceptor to not be global for prom metrics and exposing prom metrics on /metrics
This commit is contained in:
parent
b7141fc8af
commit
908651b894
7 changed files with 42 additions and 6 deletions
|
|
@ -3,7 +3,6 @@ import { ConfigService } from '@nestjs/config'
|
|||
import { NestFactory } from '@nestjs/core'
|
||||
|
||||
import { AppModule } from './app.module'
|
||||
import { CustomResponseTransformInterceptor } from './common/customResponse'
|
||||
import { setDefaults } from './common/dayjs'
|
||||
|
||||
async function bootstrap() {
|
||||
|
|
@ -15,7 +14,6 @@ async function bootstrap() {
|
|||
const port = config.get('PORT', 3000)
|
||||
app.enableShutdownHooks()
|
||||
app.useGlobalPipes(new ValidationPipe({ transform: true }))
|
||||
app.useGlobalInterceptors(new CustomResponseTransformInterceptor())
|
||||
setDefaults()
|
||||
await app.listen(port, () => console.log(`Listening on port ${port}`))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
import { Controller, Get, Inject, Query } from '@nestjs/common'
|
||||
import { Controller, Get, Inject, Query, UseInterceptors } from '@nestjs/common'
|
||||
|
||||
import { CustomResponseTransformInterceptor } from '../common/customResponse'
|
||||
import { MembersService } from './service'
|
||||
|
||||
@Controller('members')
|
||||
@UseInterceptors(CustomResponseTransformInterceptor)
|
||||
export class MembersController {
|
||||
constructor(
|
||||
@Inject(MembersService)
|
||||
|
|
|
|||
16
src/prometheus/controller.ts
Normal file
16
src/prometheus/controller.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { Controller, Get, Inject } from '@nestjs/common'
|
||||
|
||||
import { PrometheusProvider } from './provider'
|
||||
|
||||
@Controller('metrics')
|
||||
export class PrometheusController {
|
||||
constructor(
|
||||
@Inject(PrometheusProvider)
|
||||
private readonly promProvider: PrometheusProvider,
|
||||
) {}
|
||||
|
||||
@Get('/')
|
||||
async getMetrics() {
|
||||
return await this.promProvider.getMetrics()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
import { Module } from '@nestjs/common'
|
||||
|
||||
import { PrometheusController } from './controller'
|
||||
import { PrometheusProvider } from './provider'
|
||||
|
||||
@Module({
|
||||
controllers: [PrometheusController],
|
||||
providers: [PrometheusProvider],
|
||||
})
|
||||
export class PrometheusModule {}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
import { OnApplicationBootstrap } from '@nestjs/common'
|
||||
import { collectDefaultMetrics, Registry } from 'prom-client'
|
||||
import { collectDefaultMetrics, Counter, Registry } from 'prom-client'
|
||||
|
||||
export class PrometheusProvider implements OnApplicationBootstrap {
|
||||
private readonly registry: Registry
|
||||
private readonly counters: Map<string, Counter>
|
||||
|
||||
constructor() {
|
||||
this.registry = new Registry()
|
||||
this.counters = new Map()
|
||||
}
|
||||
|
||||
onApplicationBootstrap() {
|
||||
|
|
@ -15,4 +17,18 @@ export class PrometheusProvider implements OnApplicationBootstrap {
|
|||
labels: { GIT_COMMIT: process.env.GIT_COMMIT ?? 'unknown' },
|
||||
})
|
||||
}
|
||||
|
||||
async getMetrics() {
|
||||
return this.registry.metrics()
|
||||
}
|
||||
|
||||
async count(name: string, help: string, increment = 1) {
|
||||
let counter = this.counters.get(name)
|
||||
if (counter == null) {
|
||||
counter = new Counter({ name, help })
|
||||
this.counters.set(name, counter)
|
||||
}
|
||||
|
||||
counter.inc(increment)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import {
|
|||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
|
||||
import { CustomResponseTransformInterceptor } from '../common/customResponse'
|
||||
import { DayOfWeek } from './entity'
|
||||
import { RecurringReservationsService } from './service'
|
||||
|
||||
|
|
@ -79,7 +80,7 @@ export class UpdateRecurringReservationRequest {
|
|||
}
|
||||
|
||||
@Controller('recurring-reservations')
|
||||
@UseInterceptors(ClassSerializerInterceptor)
|
||||
@UseInterceptors(ClassSerializerInterceptor, CustomResponseTransformInterceptor)
|
||||
export class RecurringReservationsController {
|
||||
constructor(
|
||||
@Inject(RecurringReservationsService)
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import {
|
|||
} from 'class-validator'
|
||||
import { Dayjs } from 'dayjs'
|
||||
|
||||
import { CustomResponseTransformInterceptor } from '../common/customResponse'
|
||||
import { DayjsTransformer } from '../common/dayjs'
|
||||
import { LoggerService } from '../logger/service.logger'
|
||||
import { RESERVATIONS_QUEUE_NAME, ReservationsQueue } from './config'
|
||||
|
|
@ -70,7 +71,7 @@ export class CreateReservationRequest {
|
|||
}
|
||||
|
||||
@Controller('reservations')
|
||||
@UseInterceptors(ClassSerializerInterceptor)
|
||||
@UseInterceptors(ClassSerializerInterceptor, CustomResponseTransformInterceptor)
|
||||
export class ReservationsController {
|
||||
constructor(
|
||||
@Inject(ReservationsService)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue