Skip to content

Commit a0ff55c

Browse files
committed
Enable ignoring some paths for response time metrics
1 parent 7f5f195 commit a0ff55c

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

examples/test-status-monitor/src/app.module.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import { HealtController } from './healtController';
88
const statusMonitorConfig: StatusMonitorConfiguration = {
99
pageTitle: 'Nest.js Monitoring Page',
1010
port: 3001,
11-
path: 'status',
12-
theme: 'default.css',
13-
ignoreStartsWith: '',
11+
path: '/status',
12+
ignoreStartsWith: '/healt/alive',
1413
healthChecks: [
1514
{
1615
protocol: 'http',

src/config/status.monitor.configuration.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export interface StatusMonitorConfiguration {
66
path: string;
77
port: number;
88
pageTitle: string;
9-
theme: string;
109
ignoreStartsWith: string;
1110
healthChecks: HealthCheckConfiguration[];
1211
spans: SpansConfiguration[];

src/status.monitor.controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export class StatusMonitorController {
1414

1515
constructor(
1616
private readonly healtCheckService: HealthCheckService,
17-
@Inject(STATUS_MONITOR_OPTIONS_PROVIDER)
18-
private readonly config: StatusMonitorConfiguration,
17+
@Inject(STATUS_MONITOR_OPTIONS_PROVIDER) config: StatusMonitorConfiguration,
1918
) {
2019
const bodyClasses = Object.keys(config.chartVisibility)
2120
.reduce((accumulator, key) => {

src/status.monitor.middleware.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
import { Injectable, NestMiddleware, MiddlewareFunction } from '@nestjs/common';
1+
import {
2+
Injectable,
3+
NestMiddleware,
4+
MiddlewareFunction,
5+
Inject,
6+
} from '@nestjs/common';
27
import * as onHeaders from 'on-headers';
38
import { StatusMonitoringService } from './status.monitoring.service';
9+
import { STATUS_MONITOR_OPTIONS_PROVIDER } from './status.monitor.constants';
10+
import { StatusMonitorConfiguration } from './config/status.monitor.configuration';
411

512
@Injectable()
613
export class StatusMonitorMiddleware implements NestMiddleware {
714
constructor(
815
private readonly statusMonitoringService: StatusMonitoringService,
16+
@Inject(STATUS_MONITOR_OPTIONS_PROVIDER)
17+
private readonly config: StatusMonitorConfiguration,
918
) {}
1019

1120
resolve(...args: any[]): MiddlewareFunction {
1221
return (req, res, next) => {
13-
const startTime = process.hrtime();
14-
onHeaders(res, () => {
15-
this.statusMonitoringService.collectResponseTime(
16-
res.statusCode,
17-
startTime,
18-
);
19-
});
22+
if (
23+
this.config.ignoreStartsWith &&
24+
!req.originalUrl.startsWith(this.config.ignoreStartsWith) &&
25+
!req.originalUrl.startsWith(this.config.path)
26+
) {
27+
const startTime = process.hrtime();
28+
onHeaders(res, () => {
29+
this.statusMonitoringService.collectResponseTime(
30+
res.statusCode,
31+
startTime,
32+
);
33+
});
34+
}
35+
2036
next();
2137
};
2238
}

0 commit comments

Comments
 (0)