Hapi plugin for k8s metrics and health check
npm install hapi-k8s-health

Hapi plugin to expose health and metrics endpoint for kubernetes or other orchestration platform
Works with Hapi v17 or higher
npm i hapi-k8s-health
In your code:
``typescript
import { Server } from '@hapi/hapi'
import { HealthPlugin } from 'hapi-k8s-health'
const server = new Server({
port: 8080
})
server.register({
plugin: HealthPlugin,
options: options: {
livenessProbes: {
status: () => Promise.resolve('Yeah !')
},
readinessProbes: {
sequelize: () => container.sequelize.authenticate()
}
}
})
`
Exposes Prometheus formatted metrics for http requests and, by default, the default prom-client metrics for node.js.
#### http metrics
- http_request_count: Counter total http requests by:
- path
- method
- returned http code
- http_current_request_count: Gauge number of http requests currently running by:
- method
- http_request_duration_seconds: Histogram histogram of http requests duration in seconds by:
- path
- method
- returned http code
- http_request_duration_ms: Summary summary of http requests duration in milliseconds by:
- path
- method
- returned http code
Endpoint /liveness used by kubernetes to status whether or not the server is alive. Default probe shoule be enough for most cases.
Endpoint /readiness used by kubernetes to status whether or not the server is ready to accept connections. You should probably check your database connection here, for example.
- _prometheusRegister_: custom Prometheus register from prom-client library. Defaults to default register
- _collectDefaultMetrics_: whether or not the plugin should exposee prom-client default node.js metrics. Default to true{}
- _defaultMetricsOptions_: prom-client options for default metrics. Defaults to Promise
- _readinessProbes_: object containing the probes you want your readiness endpoint to execute. A probe is a function returning a object of a string or void. Example:`typescript`
{
database: () => Promise.resolve('It works'),
queuer: () => Promise.resolve()
}
Default:
`typescript`
{
status: Promise.resolve('OK')
}Promise
- _livenessProbes_: object containing the probes you want your liveness endpoint to execute. A probe is a function returning a object of a string or void. Example:`typescript`
{
database: () => Promise.resolve('It works'),
queuer: () => Promise.resolve()
}
Default:
`typescript`
{
status: Promise.resolve('OK')
}/liveness
- _livenessRoute_: the route you want for your liveness probes. Default: /readiness
- _readinessRoute_: the route you want for your readiness probes. Default: /metrics
- _metricsRoute_: the route you want for your metrics. Default: false
- _monitorProbes_: whether or not you want your probes to be monitored by metrics. Default to true
- _monitorAllRoutesByDefault_: whether or not you want all routes to be monitored by default. Default to true
- _exposeLiveness_: should the liveness probe be active. Default to true
- _exposeReadiness_: should the readiness probe be active. Default to true
- _exposeMetrics_: should the metrics endpoint be active. Default to 200
- _probesSuccessCode_: http status code when successfully executes all probes of liveness or readiness endpoints.Defaults to 500
- _probesErrorCode_: http status code when one of the probes of liveness or readiness endpoints throws an error. Defaults to `
- _auth_: hapi route auth option. Can either be for all endpoints
typescript`
auth: 'simple'
`
typescript`
auth: {
mode: 'try',
strategy: 'jwt'
}
`
Or for each endpoint:
typescript`
auth: {
liveness: false,
readiness: 'simple',
metrics: {
mode: 'try',
strategies: ['jwt', 'simple']
}
}
`
- _metricsName_: metrics naming. Override the default metrics names:
typescript``
// Default values
metricsName: {
requestCounter: 'http_request_count',
requestSummary: 'http_request_duration_ms',
requestDurationHistogram: 'http_request_duration_seconds',
currentRequests: 'http_current_request_count'
}