项目作者: qiwi

项目描述 :
Health indicator for server-side monitoring & balancing
高级语言: TypeScript
项目地址: git://github.com/qiwi/health-indicator.git
创建时间: 2018-02-09T20:14:22Z
项目社区:https://github.com/qiwi/health-indicator

开源协议:Other

下载


Health indicator

CI
Maintainability
Test Coverage
@qiwi/health-indicator"">npm (tag)

Health indicator kit for server-side monitoring and balancing.
Inspired by Part V. Spring Boot Actuator: Production-ready feature 47.6.2 Writing custom HealthIndicators

Install

  1. npm i @qiwi/health-indicator
  2. yarn add @qiwi/health-indicator

Usage

  1. import express from 'express'
  2. import {SemaphoreIndicator, Endpoint} from '@qiwi/health-indicator'
  3. const app = express()
  4. const indicator = new SemaphoreIndicator()
  5. const endpoint = new Endpoint(indicator)
  6. // Override 'getStatus' impl in accordance with your business logic
  7. indicator.getStatus = () => {
  8. ...
  9. return 'GREEN'
  10. }
  11. // ... or use separate class MyIndicator extends SemaphoreIndicator {...}
  12. // ... or just directly set indicator status property
  13. indicator.status = 'RED'
  14. app.get('/health', endpoint.middleware)

Indicator composes aggregator logic, so its health status may be resolved from deps.

  1. const dep1 = new SemaphoreIndicator({status: 'RED', critical: true})
  2. const dep2 = new SemaphoreIndicator({status: 'GREEN'})
  3. const dep3 = new SemaphoreIndicator({deps: {dep1, dep2}})
  4. const indicator = new SemaphoreIndicator({deps: {dep3}})
  5. indicator.health()
  6. /*
  7. {
  8. status: 'RED',
  9. critical: true,
  10. deps: {
  11. status: 'RED',
  12. critical: true,
  13. deps: {
  14. dep1: {
  15. status: 'RED',
  16. critical: true
  17. },
  18. dep2: {
  19. status: 'GREEN'
  20. }
  21. }
  22. }
  23. }
  24. */

Customization

The lib exports only a couple of indicator implementations:

  • StandardIndicator (DOWN, OUT_OF_SERVICE, UNKNOWN, UP)
  • SemaphoreIndicator (RED, BROKEN, GREEN)

To declare any CustomIndicator you may easily extend the AbstractIndicator

  1. import {AbstractIndicator} from '@qiwi/health-indicator'
  2. const OK = 200
  3. const SERVICE_UNAVAILABLE = 503
  4. export const GREEN = 'GREEN'
  5. export const RED = 'RED'
  6. export const BROKEN = 'BROKEN'
  7. export const STATUS_MAP = {GREEN, BROKEN, RED}
  8. export const SEVERITY_ORDER = [RED, BROKEN, GREEN]
  9. export const DEFAULT_HTTP_CODE = OK
  10. export const HTTP_MAP = {[GREEN]: OK, [RED]: SERVICE_UNAVAILABLE, [BROKEN]: SERVICE_UNAVAILABLE}
  11. export default class SemaphoreIndicator extends AbstractIndicator {
  12. static getDefaultStatus (): string {
  13. return BROKEN
  14. }
  15. static getDefaultHttpCode (): number {
  16. return DEFAULT_HTTP_CODE
  17. }
  18. static getHttpMap (): Object {
  19. return HTTP_MAP
  20. }
  21. static getStatusMap (): Object {
  22. return STATUS_MAP
  23. }
  24. static getSeverityOrder (): string[] {
  25. return SEVERITY_ORDER
  26. }
  27. }

License

MIT