Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Added health check route (#231)

Yevhenii Kulisidi36252d06e879
diff --git a/apps/production/src/app.module.ts b/apps/production/src/app.module.ts
index 7d54bbd7e..263468e2e 100644
--- a/apps/production/src/app.module.ts
+++ b/apps/production/src/app.module.ts
@@ -23,6 +23,7 @@ import { AuthModule } from './auth/auth.module'
import { CaptchaModule } from './captcha/captcha.module'
import { OgImageModule } from './og-image/og-image.module'
import { IntegrationsModule } from './integrations/integrations.module'
+import { HealthModule } from './health/health.module'
const modules = [
ConfigModule.forRoot({
@@ -74,6 +75,7 @@ const modules = [
AuthModule,
CaptchaModule,
OgImageModule,
+ HealthModule,
]
@Module({
diff --git a/apps/production/src/health/health-indicators/redis.health-indicator.ts b/apps/production/src/health/health-indicators/redis.health-indicator.ts
new file mode 100644
index 000000000..8a3b49693
--- /dev/null
+++ b/apps/production/src/health/health-indicators/redis.health-indicator.ts
@@ -0,0 +1,19 @@
+import { Injectable } from '@nestjs/common'
+import {
+ HealthIndicator,
+ HealthIndicatorResult,
+ HealthCheckError,
+} from '@nestjs/terminus'
+import { redis } from '../../common/constants'
+
+@Injectable()
+export class RedisHealthIndicator extends HealthIndicator {
+ async pingCheck(key: string): Promise<HealthIndicatorResult> {
+ const isHealthy = (await redis.ping()) === 'PONG'
+ const result = this.getStatus(key, isHealthy)
+
+ if (isHealthy) return result
+
+ throw new HealthCheckError('Redis check failed', result)
+ }
+}
diff --git a/apps/production/src/health/health.controller.ts b/apps/production/src/health/health.controller.ts
new file mode 100644
index 000000000..d8eb2a529
--- /dev/null
+++ b/apps/production/src/health/health.controller.ts
@@ -0,0 +1,41 @@
+import { Controller, Get } from '@nestjs/common'
+import { ConfigService } from '@nestjs/config'
+import {
+ HealthCheckService,
+ TypeOrmHealthIndicator,
+ HttpHealthIndicator,
+ HealthCheck,
+} from '@nestjs/terminus'
+import { RedisHealthIndicator } from './health-indicators/redis.health-indicator'
+
+@Controller('health')
+export class HealthController {
+ constructor(
+ private readonly configService: ConfigService,
+ private readonly health: HealthCheckService,
+ private readonly typeOrmHealthIndicator: TypeOrmHealthIndicator,
+ private readonly httpHealthIndicator: HttpHealthIndicator,
+ private readonly redisHealthIndicator: RedisHealthIndicator,
+ ) {}
+
+ @Get()
+ @HealthCheck()
+ async check() {
+ const clickhouseHost = this.configService.get<string>('CLICKHOUSE_HOST')
+ const clickhousePort = Number(
+ this.configService.get<number>('CLICKHOUSE_PORT'),
+ )
+ const clickhousePingUrl = `${clickhouseHost}:${clickhousePort}/ping`
+
+ return this.health.check([
+ () => this.typeOrmHealthIndicator.pingCheck('database'),
+ () =>
+ this.httpHealthIndicator.responseCheck(
+ 'analyticsDatabase',
+ clickhousePingUrl,
+ res => res.data === 'Ok.\n',
+ ),
+ () => this.redisHealthIndicator.pingCheck('cache'),
+ ])
+ }
+}
diff --git a/apps/production/src/health/health.module.ts b/apps/production/src/health/health.module.ts
new file mode 100644
index 000000000..2c47a9b31
--- /dev/null
+++ b/apps/production/src/health/health.module.ts
@@ -0,0 +1,11 @@
+import { Module } from '@nestjs/common'
+import { TerminusModule } from '@nestjs/terminus'
+import { RedisHealthIndicator } from './health-indicators/redis.health-indicator'
+import { HealthController } from './health.controller'
+
+@Module({
+ imports: [TerminusModule.forRoot({ logger: false })],
+ controllers: [HealthController],
+ providers: [RedisHealthIndicator],
+})
+export class HealthModule {}
diff --git a/package.json b/package.json
index b7411ffdd..9b1eba98a 100644
--- a/package.json
+++ b/package.json
@@ -28,7 +28,7 @@
"dependencies": {
"@faker-js/faker": "^8.3.1",
"@nestjs-modules/mailer": "^1.11.2",
- "@nestjs/axios": "^3.0.1",
+ "@nestjs/axios": "^3.0.2",
"@nestjs/common": "^10.2.10",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.10",
@@ -38,10 +38,11 @@
"@nestjs/platform-express": "^10.2.10",
"@nestjs/schedule": "^4.0.0",
"@nestjs/swagger": "^7.1.16",
+ "@nestjs/terminus": "^10.2.3",
"@nestjs/typeorm": "^10.0.1",
"@paypal/payouts-sdk": "^1.1.1",
"@sentry/node": "^7.86.0",
- "axios": "^1.6.2",
+ "axios": "^1.6.8",
"bcrypt": "^5.1.1",
"blake3": "^2.1.7",
"body-parser": "^1.20.2",