@seas-computing/nestjs-healthcheck
v0.1.0
Published
A standalone healthcheck module for NestJS apps
Keywords
Readme
NestJS HealthCheck Module
The nestjs-healthcheck package provides a standalone NestJS module that verifies the availability of essential application dependencies. This includes:
- pinging the application database using
TypeOrmHealthIndicator - performing an HTTP ping to the HarvardKey OIDC discovery document URL using
HttpHealthIndicator - pinging Valkey with a custom
ValkeyHealthServiceIt returns a composite health status result and the current app version. It is intended to be included in a larger application and assumes the presence of a Valkey connection and database connection during runtime.
Installation
npm install @seas-computing/nestjs-healthcheckUsage
To add the nestjs healthcheck system, you should make the following changes in the consuming application's AppModule (or other root module):
- be sure that the
ConfigModuleis at the top of the imports list - import and register the
HealthcheckModuleusingregisterAsync - import
TypeOrmModule - import a
ValkeyModuleto provide the Valkey Client (see example in oap-grad-database
In the ConfigService, configuration values (i.e. HealthcheckModuleOptions) should be made available and passed as options to the HealthcheckModule.
Example app.module based on oap-grad-database:
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { LogMiddleware, LogModule } from '@seas-computing/nestjs-logging';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { HarvardKeyModule } from '@seas-computing/nestjs-harvard-key';
import { SessionModule, NestSessionOptions } from 'nestjs-session';
import { RedisStore } from 'connect-redis';
import { HealthcheckModule, HealthcheckModuleOptions } from '@seas-computing/nestjs-healthcheck';
import Valkey from 'iovalkey';
import { ConfigModule } from './config/config.module';
import { ConfigService } from './config/config.service';
import { HealthCheckModule } from './healthCheck/healthCheck.module';
import { VALKEY_CONNECTION_TOKEN } from './valkey/valkey.token';
import { ValkeyModule } from './valkey/valkey.module';
@Module({
imports: [
ConfigModule, // Ensure this is at the top
HealthCheckModule,
ValkeyModule,
LogModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => config.logLevel,
}),
HarvardKeyModule.registerAsync({
inject: [ConfigService],
imports: [ConfigModule],
useFactory: (
config: ConfigService
) => ({
...config.harvardKeyConfig,
}),
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (
config: ConfigService
): TypeOrmModuleOptions => ({
...config.dbOptions,
maxQueryExecutionTime: 1000,
}),
}),
HealthcheckModule.registerAsync({
imports: [ConfigModule, ValkeyModule],
inject: [
ConfigService,
{
token: VALKEY_CONNECTION_TOKEN,
optional: false,
},
],
useFactory: (
configService: ConfigService,
valkeyClient: Valkey
): HealthcheckModuleOptions => {
return {
...configService.healthcheckConfig,
valkeyClient,
};
},
}),
],
controllers: [],
providers: [],
})
class AppModule implements NestModule {
/**
* Apply our logging middleware for all routes except the healthcheck
*/
configure(consumer: MiddlewareConsumer): void {
consumer
.apply(LogMiddleware)
.exclude(
'heartbeat',
{ path: 'oapGradDatabase/health-check', method: RequestMethod.ALL } // exclude healthcheck
)
.forRoutes('*');
}
}
export { AppModule };
Examples of additions to config.service.ts from oap-grad-database:
/**
* Returns the OIDC discovery document URL, which will be sent to the
* Healthcheck Module. The /.well-known/openid-configuration endpoint
* is the standard, unauthenticated health signal exposed by all OIDC
* providers.
*/
public get oidcCheckURL(): string {
return `${this.oidcBaseURL}/.well-known/openid-configuration`;
}
public get healthcheckConfig(): HealthcheckModuleOptions {
return {
oidcCheckURL: this.oidcCheckURL,
dbOptions: this.dbOptions,
buildVersion: this.buildVersion,
strategy: this.isProduction
? HEALTH_STRATEGY_NAME.HEALTHCHECK
: HEALTH_STRATEGY_NAME.DEV,
};
}Sample Response
After running
curl http://127.0.0.1:3001/[yourAppName]/health-checkfrom the cli, you should receive a response that looks like this:
{
"status": "ok",
"info": {
"database": {
"status": "up"
},
"harvard-key": {
"status": "up"
},
"valkey": {
"status": "up",
"result": "PONG"
}
},
"error": {},
"details": {
"database": {
"status": "up"
},
"harvard-key": {
"status": "up"
},
"valkey": {
"status": "up",
"result": "PONG"
}
}
}