@accelbyte/healthcheck
v1.0.4
Published
This library provides backend service dependencies health check and its telemetry, used for service written in Javascript and Typescript.
Keywords
Readme
healthcheck js/ts sdk
This library provides backend service dependencies health check and its telemetry, used for service written in Javascript and Typescript.
The library will execute health check on demand or periodically in background for all dependencies
registered in HealthCheck class.
The result from library is in form of general json object that can be return as http response to health endpoint using any web framework.
Installation guide
yarn add @accelbyte/healthcheckLibrary usage
Initial set up the HealthCheck object
import {
AlertLevel,
HealthCheck,
redisDependencyCheck,
s3DependencyCheck,
sqlDependencyCheck
} from '@accelbyte/healthcheck'
// instantiate health check object with service name and interval for periodic background dependency check
const healthCheck = new HealthCheck({
name: 'my-service',
checkIntervalSecond: 60,
})
// add dependency check using provided check function, available function:
// - sqlDependencyCheck
// - redisDependencyCheck
// - s3DependencyCheck
healthCheck.addDependencyCheck(sqlDependencyCheck({
prisma: prismaClient,
name: 'postgresql',
// available values: Critical (P0), Important (P1), Utility (P2)
// is used to adjust the internal alerting priority when this dependency become unhealthy
alertLevel: AlertLevel.Critical,
}))
// add dependency check manually
healthCheck.addDependencyCheck({
name: 'my-dependency-1',
alertLevel: AlertLevel.Utility,
checkFunction: async () => {
// code to check for dependency
// resolve successfully for a healthy result, or
// throw error to indicate dependency unhealthy
if (!check()) {
throw 'dependency checking error'
}
}
})
// We need to expose the metrics emitted by this library to opentelemetry MeterProvider for service monitoring and alerting purpose
healthCheck.addMetrics(metricsStorage.meterProvider)
// start periodic background check, the inverval is configured by checkIntervalSecond to every 60s above
healthCheck.startBackgroundCheck()Example get the check result and return as response
import { ParameterizedContext } from 'koa'
export const healthzHandler = async (ctx: ParameterizedContext): Promise<void> => {
// get the result object from health check, results is a json object standard with other healthcheck library
const results = await healthCheck.getResult()
ctx.body = results
ctx.status = isHealthy ? 200 : 500
}Contribute
Set up the project dependencies
yarn installTest written in /tests directory
To run unit test:
yarn testBuild the library
yarn buildMake sure to copy .env.example to .env and update the NPM_TOKEN value, then commit and push your changes to the remote repository before building and publishing the library with:
sh release.sh