-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: pull request #24 from bddvlpr/feature/metrics
Prometheus metrics endpoint
- Loading branch information
Showing
9 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { MetricsController } from './metrics.controller'; | ||
|
||
describe('MetricsController', () => { | ||
let controller: MetricsController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [MetricsController], | ||
}).compile(); | ||
|
||
controller = module.get<MetricsController>(MetricsController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Controller, Get, Res } from '@nestjs/common'; | ||
import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; | ||
import { PrometheusController } from '@willsoto/nestjs-prometheus'; | ||
import { Response } from 'express'; | ||
|
||
@ApiTags('metrics') | ||
@Controller('metrics') | ||
export class MetricsController extends PrometheusController { | ||
@ApiOkResponse({ description: 'Returns metrics to be used by Prometheus.' }) | ||
@Get() | ||
getMetrics(@Res({ passthrough: true }) response: Response) { | ||
return super.index(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { MetricsMiddleware } from './metrics.middleware'; | ||
|
||
describe('MetricsMiddleware', () => { | ||
it('should be defined', () => { | ||
expect(new MetricsMiddleware()).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import { InjectMetric } from '@willsoto/nestjs-prometheus'; | ||
import { Request, Response } from 'express'; | ||
import { Counter, Summary } from 'prom-client'; | ||
|
||
@Injectable() | ||
export class MetricsMiddleware implements NestMiddleware { | ||
constructor( | ||
@InjectMetric('uis_request_count') | ||
private readonly requestCountMetric: Counter, | ||
@InjectMetric('uis_request_duration') | ||
private readonly requestDurationMetric: Summary, | ||
) {} | ||
|
||
use(_req: Request, res: Response, next: () => void) { | ||
this.requestCountMetric.inc(); | ||
const end = this.requestDurationMetric.startTimer(); | ||
res.on('finish', () => end()); | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; | ||
import { | ||
makeCounterProvider, | ||
makeSummaryProvider, | ||
PrometheusModule, | ||
} from '@willsoto/nestjs-prometheus'; | ||
import { ClassesController } from 'src/classes/classes.controller'; | ||
import { HolidaysController } from 'src/holidays/holidays.controller'; | ||
import { LessonsController } from 'src/lessons/lessons.controller'; | ||
import { SubjectsController } from 'src/subjects/subjects.controller'; | ||
import { MetricsController } from './metrics.controller'; | ||
import { MetricsMiddleware } from './metrics.middleware'; | ||
|
||
@Module({ | ||
imports: [ | ||
PrometheusModule.register({ | ||
controller: MetricsController, | ||
}), | ||
], | ||
controllers: [MetricsController], | ||
providers: [ | ||
makeCounterProvider({ | ||
name: 'uis_request_count', | ||
help: 'The amount of requests processed.', | ||
}), | ||
makeSummaryProvider({ | ||
name: 'uis_request_duration', | ||
help: 'The amount of time taken to process a request.', | ||
}), | ||
], | ||
}) | ||
export class MetricsModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer | ||
.apply(MetricsMiddleware) | ||
.forRoutes( | ||
ClassesController, | ||
SubjectsController, | ||
LessonsController, | ||
HolidaysController, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters