Skip to content

Commit a71c6e9

Browse files
committed
Collect data about time that is needed for request execution
1 parent 462075d commit a71c6e9

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/status.monitor.middleware.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable, NestMiddleware, MiddlewareFunction } from '@nestjs/common';
2+
import * as onHeaders from 'on-headers';
3+
import { StatusMonitoringService } from './status.monitoring.service';
4+
5+
@Injectable()
6+
export class StatusMonitorMiddleware implements NestMiddleware {
7+
constructor(
8+
private readonly statusMonitoringService: StatusMonitoringService,
9+
) {}
10+
11+
resolve(...args: any[]): MiddlewareFunction {
12+
return (req, res, next) => {
13+
const startTime = process.hrtime();
14+
onHeaders(res, () => {
15+
this.statusMonitoringService.collectResponseTime(
16+
res.statusCode,
17+
startTime,
18+
);
19+
});
20+
next();
21+
};
22+
}
23+
}

src/status.monitor.module.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { Module } from '@nestjs/common';
1+
import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
22
import { StatusMonitorController } from './status.monitor.controller';
33
import { StatusMonitorGateway } from './status.monitor.gateway';
44
import { StatusMonitoringService } from './status.monitoring.service';
5+
import { StatusMonitorMiddleware } from './status.monitor.middleware';
56

67
@Module({
78
controllers: [StatusMonitorController],
89
providers: [StatusMonitorGateway, StatusMonitoringService],
910
})
10-
export class StatusMonitorModule {}
11+
export class StatusMonitorModule {
12+
configure(consumer: MiddlewareConsumer) {
13+
consumer
14+
.apply(StatusMonitorMiddleware)
15+
.forRoutes({ path: '*', method: RequestMethod.ALL });
16+
}
17+
}

src/status.monitoring.service.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,34 @@ export class StatusMonitoringService {
8888
getData() {
8989
return this.spans;
9090
}
91+
92+
collectResponseTime(statusCode, startTime) {
93+
const diff = process.hrtime(startTime);
94+
const responseTime = (diff[0] * 1e3 + diff[1]) * 1e-6;
95+
const category = Math.floor(statusCode / 100);
96+
97+
console.log(responseTime);
98+
this.spans.forEach(span => {
99+
const last = span.responses[span.responses.length - 1];
100+
101+
if (
102+
last !== undefined &&
103+
last.timestamp / 1000 + span.interval > Date.now() / 1000
104+
) {
105+
last[category] += 1;
106+
last.count += 1;
107+
last.mean += (responseTime - last.mean) / last.count;
108+
} else {
109+
span.responses.push({
110+
2: category === 2 ? 1 : 0,
111+
3: category === 3 ? 1 : 0,
112+
4: category === 4 ? 1 : 0,
113+
5: category === 5 ? 1 : 0,
114+
count: 1,
115+
mean: responseTime,
116+
timestamp: Date.now(),
117+
});
118+
}
119+
});
120+
}
91121
}

0 commit comments

Comments
 (0)