Skip to content

Commit e43b01a

Browse files
committed
feat: added envs logs and metrics, added health check on start
1 parent ba53fe8 commit e43b01a

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
lines changed

src/app/app.service.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Inject, Injectable, LoggerService, OnModuleInit } from '@nestjs/common';
22
import { LOGGER_PROVIDER } from '@lido-nestjs/logger';
33

4-
import { ConfigService } from 'common/config';
4+
import { ConfigService, ENV_KEYS, EnvironmentVariables } from 'common/config';
55
import { PrometheusService } from 'common/prometheus';
66
import { ConsensusProviderService } from 'common/consensus-provider';
77
import { ExecutionProviderService } from 'common/execution-provider';
88
import { APP_NAME, APP_VERSION } from './app.constants';
9+
import { commonPatterns, satanizer } from '@lidofinance/satanizer';
910

1011
@Injectable()
1112
export class AppService implements OnModuleInit {
@@ -20,14 +21,8 @@ export class AppService implements OnModuleInit {
2021

2122
public async onModuleInit(): Promise<void> {
2223
await this.validateNetwork();
23-
24-
const network = await this.executionProviderService.getNetworkName();
25-
const env = this.configService.get('NODE_ENV');
26-
const version = APP_VERSION;
27-
const name = APP_NAME;
28-
29-
this.prometheusService.buildInfo.labels({ env, network, name, version }).inc();
30-
this.logger.log('Init app', { env, network, name, version });
24+
await this.prometheusBuildInfoMetrics();
25+
this.prometheusEnvsInfoMetrics();
3126
}
3227

3328
/**
@@ -43,4 +38,27 @@ export class AppService implements OnModuleInit {
4338
throw new Error('Chain ids do not match');
4439
}
4540
}
41+
42+
protected async prometheusBuildInfoMetrics() {
43+
const network = await this.executionProviderService.getNetworkName();
44+
const env = this.configService.get('NODE_ENV');
45+
const version = APP_VERSION;
46+
const name = APP_NAME;
47+
48+
this.prometheusService.buildInfo.labels({ env, network, name, version }).inc();
49+
this.logger.log('Init app', { env, network, name, version });
50+
}
51+
52+
protected prometheusEnvsInfoMetrics() {
53+
const secrets = this.configService.secrets;
54+
const mask = satanizer([...commonPatterns, ...secrets]);
55+
56+
const allConfigEnvs = {};
57+
ENV_KEYS.forEach((key: keyof EnvironmentVariables) => {
58+
allConfigEnvs[key] = mask(this.configService.get(key));
59+
});
60+
61+
this.prometheusService.envsInfo.labels(allConfigEnvs).inc();
62+
this.logger.log('Init app dumping envs', allConfigEnvs);
63+
}
4664
}

src/common/config/env.validation.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,21 @@ export function validate(config: Record<string, unknown>) {
9696

9797
return validatedConfig;
9898
}
99+
100+
export const ENV_KEYS = [
101+
'NODE_ENV',
102+
'PORT',
103+
'CORS_WHITELIST_REGEXP',
104+
'GLOBAL_THROTTLE_TTL',
105+
'GLOBAL_THROTTLE_LIMIT',
106+
'GLOBAL_CACHE_TTL',
107+
'SENTRY_DSN',
108+
'LOG_LEVEL',
109+
'LOG_FORMAT',
110+
'JOB_INTERVAL_VALIDATORS',
111+
'JOB_INTERVAL_QUEUE_INFO',
112+
'JOB_INTERVAL_CONTRACT_CONFIG',
113+
'CL_API_URLS',
114+
'EL_RPC_URLS',
115+
'CHAIN_ID',
116+
];

src/common/health/health.module.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1-
import { Module } from '@nestjs/common';
1+
import { Inject, LoggerService, Module, OnModuleInit } from '@nestjs/common';
22
import { TerminusModule } from '@nestjs/terminus';
33
import { HealthController } from './health.controller';
44
import { ExecutionProviderHealthIndicator } from './execution-provider.indicator';
55
import { ConsensusProviderIndicator } from './consensus-provider.indicator';
66
import { GenesisTimeModule } from '../genesis-time';
7+
import { LOGGER_PROVIDER } from '@lido-nestjs/logger';
78

89
@Module({
910
providers: [ExecutionProviderHealthIndicator, ConsensusProviderIndicator],
1011
controllers: [HealthController],
1112
imports: [TerminusModule, GenesisTimeModule],
1213
})
13-
export class HealthModule {}
14+
export class HealthModule implements OnModuleInit {
15+
constructor(
16+
@Inject(LOGGER_PROVIDER) protected readonly logger: LoggerService,
17+
protected readonly consensusProviderIndicator: ConsensusProviderIndicator,
18+
protected readonly executionProviderIndicator: ExecutionProviderHealthIndicator,
19+
) {}
20+
21+
async onModuleInit() {
22+
await this.startUpChecks();
23+
}
24+
25+
async startUpChecks() {
26+
try {
27+
await this.consensusProviderIndicator.isHealthy('consensusProvider');
28+
await this.executionProviderIndicator.isHealthy('executionProvider');
29+
this.logger.log(`Start up checks passed successfully`);
30+
} catch (e) {
31+
this.logger.error(`Start up checks failed with error: ${e}`);
32+
}
33+
}
34+
}

src/common/prometheus/prometheus.service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getOrCreateMetric } from '@willsoto/nestjs-prometheus';
22
import { Options, Metrics, Metric } from './interfaces';
33
import { METRICS_PREFIX } from './prometheus.constants';
44
import { RequestSourceType } from '../../http/request-time/headers/request-source-type';
5+
import { ENV_KEYS } from '../config';
56

67
export class PrometheusService {
78
protected prefix = METRICS_PREFIX;
@@ -25,7 +26,13 @@ export class PrometheusService {
2526
public buildInfo = this.getOrCreateMetric('Gauge', {
2627
name: 'build_info',
2728
help: 'Build information',
28-
labelNames: ['name', 'version', 'env', 'network', 'startSlot'],
29+
labelNames: ['name', 'version', 'env', 'network'],
30+
});
31+
32+
public envsInfo = this.getOrCreateMetric('Gauge', {
33+
name: 'envs_info',
34+
help: 'Environment variables information',
35+
labelNames: ENV_KEYS,
2936
});
3037

3138
public clApiRequestDuration = this.getOrCreateMetric('Histogram', {

src/http/common/middleware/logger.middleware.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Inject, Injectable, LoggerService, NestMiddleware } from '@nestjs/common';
22
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
33
import { Request, Reply } from './interfaces';
4-
import { FastifyRequest } from 'fastify';
54

65
@Injectable()
76
export class LoggerMiddleware implements NestMiddleware {
@@ -10,15 +9,13 @@ export class LoggerMiddleware implements NestMiddleware {
109
private readonly logger: LoggerService,
1110
) {}
1211

13-
use(request: any, reply: Reply, next: () => void) {
12+
use(request: Request, reply: Reply, next: () => void) {
1413
const { ip, method, headers, originalUrl } = request;
1514
const userAgent = headers['user-agent'] ?? '';
1615

17-
const ips = request.ips ? request.ips : [];
18-
1916
reply.on('finish', () => {
2017
const { statusCode } = reply;
21-
const log = { method, originalUrl, statusCode, userAgent, ip, ips };
18+
const log = { method, originalUrl, statusCode, userAgent, ip };
2219

2320
this.logger.log(JSON.stringify(log));
2421
});

0 commit comments

Comments
 (0)