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:
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_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
{}
- readinessProbes: object containing the probes you want your readiness endpoint to execute. A probe is a function returning a
Promise
object of astring
orvoid
. Example:
{
database: () => Promise.resolve('It works'),
queuer: () => Promise.resolve()
}
Default:
{
status: Promise.resolve('OK')
}
- livenessProbes: object containing the probes you want your liveness endpoint to execute. A probe is a function returning a
Promise
object of astring
orvoid
. Example:
{
database: () => Promise.resolve('It works'),
queuer: () => Promise.resolve()
}
Default:
{
status: Promise.resolve('OK')
}
- livenessRoute: the route you want for your liveness probes. Default:
/liveness
- readinessRoute: the route you want for your readiness probes. Default:
/readiness
- metricsRoute: the route you want for your metrics. Default:
/metrics
- monitorProbes: whether or not you want your probes to be monitored by metrics. Default to
false
- 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
true
- probesSuccessCode: http status code when successfully executes all probes of liveness or readiness endpoints.Defaults to
200
- probesErrorCode: http status code when one of the probes of liveness or readiness endpoints throws an error. Defaults to
500
- auth: hapi route auth option. Can either be for all endpoints
auth: 'simple'
Or for each endpoint:auth: { mode: 'try', strategy: 'jwt' }
auth: { liveness: false, readiness: 'simple', metrics: { mode: 'try', strategies: ['jwt', 'simple'] } }
- metricsName: metrics naming. Override the default metrics names:
// Default values metricsName: { requestCounter: 'http_request_count', requestSummary: 'http_request_duration_ms', requestDurationHistogram: 'http_request_duration_seconds', currentRequests: 'http_current_request_count' }