Skip to content

Commit e87d1bc

Browse files
committed
Add hardcoded healt check functionality
1 parent a71c6e9 commit e87d1bc

File tree

8 files changed

+118
-14
lines changed

8 files changed

+118
-14
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
22
import { AppController } from './app.controller';
33
import { AppService } from './app.service';
44
import { StatusMonitorModule } from '../../../dist/status.monitor.module';
5+
import { HealtController } from './healtController';
56

67
@Module({
78
imports: [StatusMonitorModule],
8-
controllers: [AppController],
9+
controllers: [AppController, HealtController],
910
providers: [AppService],
1011
})
1112
export class AppModule {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Get, Controller, HttpCode } from '@nestjs/common';
2+
3+
@Controller('healt')
4+
export class HealtController {
5+
@Get('alive')
6+
@HttpCode(200)
7+
alive(): string {
8+
return 'OK';
9+
}
10+
11+
@Get('dead')
12+
@HttpCode(500)
13+
dead(): string {
14+
return 'DEAD';
15+
}
16+
}

package-lock.json

Lines changed: 17 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"dependencies": {
3030
"@nestjs/websockets": "^5.4.0",
31+
"axios": "^0.18.0",
3132
"debug": "^2.6.8",
3233
"handlebars": "~4.0.12",
3334
"on-headers": "^1.0.1",

src/healt.check.service.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Injectable, Inject, forwardRef } from '@nestjs/common';
2+
import axios from 'axios';
3+
4+
@Injectable()
5+
export class HealtCheckService {
6+
healthChecks = [
7+
{
8+
protocol: 'http',
9+
host: 'localhost',
10+
path: '/healt/alive',
11+
port: '3001',
12+
},
13+
{
14+
protocol: 'http',
15+
host: 'localhost',
16+
path: '/healt/dead',
17+
port: '3001',
18+
},
19+
];
20+
21+
checkAllEndpoints() {
22+
const checkPromises = [];
23+
24+
this.healthChecks.forEach(healthCheck => {
25+
checkPromises.push(this.checkEndpoint(healthCheck));
26+
});
27+
28+
let checkResults = [];
29+
30+
return this.allSettled(checkPromises).then(results => {
31+
results.forEach((result, index) => {
32+
if (result.state === 'rejected') {
33+
checkResults.push({
34+
path: this.healthChecks[index].path,
35+
status: 'failed',
36+
});
37+
} else {
38+
checkResults.push({
39+
path: this.healthChecks[index].path,
40+
status: 'ok',
41+
});
42+
}
43+
});
44+
45+
return checkResults;
46+
});
47+
}
48+
49+
private checkEndpoint(healthCheck): Promise<any> {
50+
let uri = `${healthCheck.protocol}://${healthCheck.host}`;
51+
52+
if (healthCheck.port) {
53+
uri += `:${healthCheck.port}`;
54+
}
55+
56+
uri += healthCheck.path;
57+
58+
//TODO (ivasiljevic) use http service instead of axios
59+
return axios({
60+
url: uri,
61+
method: 'GET',
62+
});
63+
}
64+
65+
private allSettled(promises: Promise<any>[]): Promise<any> {
66+
let wrappedPromises = promises.map(p =>
67+
Promise.resolve(p).then(
68+
val => ({ state: 'fulfilled', value: val }),
69+
err => ({ state: 'rejected', value: err }),
70+
),
71+
);
72+
73+
return Promise.all(wrappedPromises);
74+
}
75+
}

src/status.monitor.controller.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Get, Controller, HttpCode } from '@nestjs/common';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import Handlebars from 'handlebars';
5+
import { HealtCheckService } from './healt.check.service';
56

67
const controllerPath = 'monitor';
78

@@ -10,7 +11,7 @@ export class StatusMonitorController {
1011
data;
1112
render;
1213

13-
constructor() {
14+
constructor(private readonly healtCheckService: HealtCheckService) {
1415
this.data = {
1516
title: 'Nest.js Status',
1617
port: 3001,
@@ -32,7 +33,9 @@ export class StatusMonitorController {
3233

3334
@Get()
3435
@HttpCode(200)
35-
root() {
36+
async root() {
37+
const healtData = await this.healtCheckService.checkAllEndpoints();
38+
this.data.healthCheckResults = healtData;
3639
return this.render(this.data);
3740
}
3841
}

src/status.monitor.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { StatusMonitorController } from './status.monitor.controller';
33
import { StatusMonitorGateway } from './status.monitor.gateway';
44
import { StatusMonitoringService } from './status.monitoring.service';
55
import { StatusMonitorMiddleware } from './status.monitor.middleware';
6+
import { HealtCheckService } from './healt.check.service';
67

78
@Module({
89
controllers: [StatusMonitorController],
9-
providers: [StatusMonitorGateway, StatusMonitoringService],
10+
providers: [StatusMonitorGateway, StatusMonitoringService, HealtCheckService],
1011
})
1112
export class StatusMonitorModule {
1213
configure(consumer: MiddlewareConsumer) {

src/status.monitoring.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ export class StatusMonitoringService {
9494
const responseTime = (diff[0] * 1e3 + diff[1]) * 1e-6;
9595
const category = Math.floor(statusCode / 100);
9696

97-
console.log(responseTime);
9897
this.spans.forEach(span => {
9998
const last = span.responses[span.responses.length - 1];
10099

0 commit comments

Comments
 (0)