1.1.1 • Published 4 years ago

hapi-k8s-health v1.1.1

Weekly downloads
1,031
License
MIT
Repository
github
Last release
4 years ago

hapi-k8s-health

npm version Build Status

Hapi plugin to expose health and metrics endpoint for kubernetes or other orchestration platform

Requirements

Works with Hapi v17 or higher

Installation

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()
      }
    }
})

Features

Metrics endpoint

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

Liveness endpoint

Endpoint /liveness used by kubernetes to status whether or not the server is alive. Default probe shoule be enough for most cases.

Readiness endpoint

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.

Options

  • 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 a string or void. 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 a string or void. 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'
    auth: {
      mode: 'try',
      strategy: 'jwt'
    }
    Or for each endpoint:
    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'
    }