Skip to content

Commit

Permalink
feat: add health, metrics and version
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicToast committed Aug 26, 2024
1 parent 7f6f41d commit 95ec6a5
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 75 deletions.
22 changes: 0 additions & 22 deletions apps/cronjob-service/src/app/app.controller.spec.ts

This file was deleted.

13 changes: 0 additions & 13 deletions apps/cronjob-service/src/app/app.controller.ts

This file was deleted.

16 changes: 10 additions & 6 deletions apps/cronjob-service/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { HealthModule } from './health/health.module';
import { MetricsModule } from './metrics/metrics.module';
import { VersionModule } from './version/version.module';

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
HealthModule,
MetricsModule,
VersionModule,
],
})
export class AppModule {}
21 changes: 0 additions & 21 deletions apps/cronjob-service/src/app/app.service.spec.ts

This file was deleted.

8 changes: 0 additions & 8 deletions apps/cronjob-service/src/app/app.service.ts

This file was deleted.

39 changes: 39 additions & 0 deletions apps/cronjob-service/src/app/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Controller, Get, Inject } from '@nestjs/common';
import {
HealthCheck,
HealthCheckService,
MemoryHealthIndicator,
MicroserviceHealthIndicator,
} from '@nestjs/terminus';
import { Transport } from '@nestjs/microservices';
import { azkaban_cronjob } from '@toxictoast/azkaban-broker-rabbitmq';

@Controller('health')
export class HealthController {
constructor(
@Inject('MEMORY_HEAP_TRESHOLD') private readonly heapTreshold: number,
@Inject('MEMORY_RSS_TRESHOLD') private readonly rssTreshold: number,
@Inject('BROKER_CONNECTION_STRING')
private readonly brokerConnectionString: string,
private readonly service: HealthCheckService,
private readonly memory: MemoryHealthIndicator,
private readonly microservices: MicroserviceHealthIndicator,
) {}

@Get()
@HealthCheck()
check() {
return this.service.check([
() => this.memory.checkHeap('memory_heap', this.heapTreshold),
() => this.memory.checkRSS('memory_rss', this.rssTreshold),
() =>
this.microservices.pingCheck('rabbitmq', {
transport: Transport.RMQ,
options: {
urls: [this.brokerConnectionString],
queue: azkaban_cronjob,
},
}),
]);
}
}
42 changes: 42 additions & 0 deletions apps/cronjob-service/src/app/health/health.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { HealthController } from './health.controller';
import { ConfigService } from '@nestjs/config';

@Module({
imports: [
TerminusModule.forRoot({
errorLogStyle: 'pretty',
}),
],
controllers: [HealthController],
providers: [
{
provide: 'MEMORY_HEAP_TRESHOLD',
useFactory: (config: ConfigService) => {
return config.get('MEMORY_HEAP_TRESHOLD', 0);
},
inject: [ConfigService],
},
{
provide: 'MEMORY_RSS_TRESHOLD',
useFactory: (config: ConfigService) => {
return config.get('MEMORY_RSS_TRESHOLD', 0);
},
inject: [ConfigService],
},
{
provide: 'BROKER_CONNECTION_STRING',
useFactory: (config: ConfigService) => {
const username = config.get('BROKER_USERNAME', 'guest');
const password = config.get('BROKER_PASSWORD', 'guest');
const hostname = config.get('BROKER_HOST', 'localhost');
const port = config.get('BROKER_PORT', 5672);
//
return `amqp://${username}:${password}@${hostname}:${port}`;
},
inject: [ConfigService],
},
],
})
export class HealthModule {}
17 changes: 17 additions & 0 deletions apps/cronjob-service/src/app/metrics/metrics.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';

@Module({
imports: [
PrometheusModule.register({
defaultMetrics: {
enabled: true,
},
defaultLabels: {
app: 'cronjob-service',
},
path: '/metrics',
}),
],
})
export class MetricsModule {}
14 changes: 14 additions & 0 deletions apps/cronjob-service/src/app/version/version.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Controller } from '@nestjs/common';
import { VersionService } from './version.service';
import { MessagePattern } from '@nestjs/microservices';
import { CronjobTopics } from '@toxictoast/azkaban-broker-rabbitmq';

@Controller('version')
export class VersionController {
constructor(private readonly service: VersionService) {}

@MessagePattern(CronjobTopics.VERSION)
async getVersion() {
return this.service.getVersion();
}
}
19 changes: 19 additions & 0 deletions apps/cronjob-service/src/app/version/version.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Module } from '@nestjs/common';
import { VersionController } from './version.controller';
import { VersionService } from './version.service';
import { ConfigService } from '@nestjs/config';

@Module({
controllers: [VersionController],
providers: [
VersionService,
{
provide: 'APP_VERSION',
useFactory: (config: ConfigService) => {
return config.get('APP_VERSION', 'local');
},
inject: [ConfigService],
},
],
})
export class VersionModule {}
10 changes: 10 additions & 0 deletions apps/cronjob-service/src/app/version/version.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Inject, Injectable } from '@nestjs/common';

@Injectable()
export class VersionService {
constructor(@Inject('APP_VERSION') private readonly appVersion: string) {}

getVersion(): string {
return this.appVersion;
}
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
"@toxictoast/azkaban-base-domain": "^0.0.2",
"@toxictoast/azkaban-base-helpers": "^0.0.13",
"@toxictoast/azkaban-base-types": "^0.0.1",
"@toxictoast/azkaban-broker-rabbitmq": "^0.0.26",
"@toxictoast/azkaban-broker-rabbitmq": "^0.0.27",
"@toxictoast/azkaban-sdk": "^0.0.14",
"@willsoto/nestjs-prometheus": "^6.0.1",
"amqp-connection-manager": "^4.1.14",
Expand Down

0 comments on commit 95ec6a5

Please sign in to comment.