exframe-health

Rest framework module for health checks

Usage no npm install needed!

<script type="module">
  import exframeHealth from 'https://cdn.skypack.dev/exframe-health';
</script>

README

exframe Health Module

A framework for setting up health checks within microservices. Each microservice is responsible for checking the availability of its dependencies and reporting its own health through an endpoint that accepts a GET request.

Features

  • Built in creation of health checks for external GET endpoints.
  • Custom creaton of health checks where the built in health check doesn't meet requirements.

Use

const health = require('exframe-health');

Full Example

const health = require('exframe-health');
const app = require('exframe-rest').create(8787);
const mongoose = require('mongoose');

const mongooseCheck = () => {
  if (mongoose.connection.readyState === 1) {
    return Promise.resolve({
      status: 200,
      message: 'OK'
    });
  } else {
    return Promise.reject({
      status: 503,
      message: 'MongoDB is unavailable'
    });
  }
};

health.add('MongoDB', mongooseCheck);
health.add('Example', 'http://www.example.com');

app.use('/health', health.check);

Methods

add()

Syntax

health.add(dependency, check)

Parameter Values

  • dependency Required. A string value containing the name of the dependency being checked.

  • check Required. Either a string representing the url of an external health check endpoint for the dependency or a custom function that tests the availability of the dependency (examples below)

Examples

Create a health check for an external dependency that has a healthcheck url.

health.add('Example Service', 'http://api.example.com/health');

Create a custom health check. The custom health check should return a promise. Whether the promise is resolved or rejected, a json object should be returned containing (at a minimum) a status and a message. Other optional keys can be added as necessary. Any status of 400 or greater will result in the parent service being declared unhealthy.

const mongoose = require('mongoose');
const checkMongoose = () => {
  if (mongoose.connection.readyState === 1) {
    return Promise.resolve({
      status: 200,
      message: 'OK'
    });
  } else {
    return Promise.reject({
      status: 503,
      message: 'Service Unavailable'
    });
  }
};
health.add('MongoDB', checkMongoose);

get()

Syntax

health.get()

Returns a json object representing the health checks that have been added to the module.

remove()

Syntax

health.remove(dependency)

Removes a healthcheck from the module.

check

Syntax

health.check(req, res)