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!

use native Clickhouse ping method

Blue Mouse2385891ed848
diff --git a/apps/production/src/health/health-indicators/clickhouse.health-indicator.ts b/apps/production/src/health/health-indicators/clickhouse.health-indicator.ts
new file mode 100644
index 000000000..0fedcb020
--- /dev/null
+++ b/apps/production/src/health/health-indicators/clickhouse.health-indicator.ts
@@ -0,0 +1,20 @@
+import { Injectable } from '@nestjs/common'
+import {
+ HealthIndicator,
+ HealthIndicatorResult,
+ HealthCheckError,
+} from '@nestjs/terminus'
+import { clickhouse } from '../../common/integrations/clickhouse'
+
+@Injectable()
+export class ClickhouseHealthIndicator extends HealthIndicator {
+ async pingCheck(key: string): Promise<HealthIndicatorResult> {
+ const ping = await clickhouse.ping()
+ const result = this.getStatus(key, ping.success)
+
+ // I'm doing "=== true" because otherwise Typescript complains that "ping.error" beneath if undefined
+ if (ping.success === true) return result
+
+ throw new HealthCheckError('Clickhouse check failed', ping.error)
+ }
+}
diff --git a/apps/production/src/health/health.controller.ts b/apps/production/src/health/health.controller.ts
index d8eb2a529..d4cd3830b 100644
--- a/apps/production/src/health/health.controller.ts
+++ b/apps/production/src/health/health.controller.ts
@@ -1,40 +1,27 @@
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'
+import { ClickhouseHealthIndicator } from './health-indicators/clickhouse.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,
+ private readonly clickhouseHealthIndicator: ClickhouseHealthIndicator,
) {}
@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.clickhouseHealthIndicator.pingCheck('analyticsDatabase'),
() => this.redisHealthIndicator.pingCheck('cache'),
])
}
diff --git a/apps/production/src/health/health.module.ts b/apps/production/src/health/health.module.ts
index 2c47a9b31..b7fec45f0 100644
--- a/apps/production/src/health/health.module.ts
+++ b/apps/production/src/health/health.module.ts
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common'
import { TerminusModule } from '@nestjs/terminus'
import { RedisHealthIndicator } from './health-indicators/redis.health-indicator'
+import { ClickhouseHealthIndicator } from './health-indicators/clickhouse.health-indicator'
import { HealthController } from './health.controller'
@Module({
imports: [TerminusModule.forRoot({ logger: false })],
controllers: [HealthController],
- providers: [RedisHealthIndicator],
+ providers: [RedisHealthIndicator, ClickhouseHealthIndicator],
})
export class HealthModule {}