├── .gitignore ├── .travis.yml ├── .yarnrc ├── LICENSE ├── README.md ├── lib └── index.js ├── package.json ├── test └── index.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # Dependencies 2 | node_modules 3 | package-lock.json 4 | 5 | # Logs 6 | *.logs 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | { 2 | language: "node_js", 3 | node_js: ["8"], 4 | os: "osx" 5 | } 6 | -------------------------------------------------------------------------------- /.yarnrc: -------------------------------------------------------------------------------- 1 | save-prefix "" 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Federico Miras 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # micro-health 2 | [![NPM version](https://img.shields.io/npm/v/micro-health.svg)](https://www.npmjs.com/package/micro-health) 3 | [![Build Status](https://travis-ci.org/fmiras/micro-health.svg?branch=master)](https://travis-ci.org/fmiras/micro-health) 4 | [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) 5 | [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) 6 | [![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/micro) 7 | [![Greenkeeper badge](https://badges.greenkeeper.io/fmiras/micro-health.svg)](https://greenkeeper.io/) 8 | 9 | micro-health is used as an extension for micro to add the route `/health` to your service. It was meant to implement the Health Check API microservice design pattern for microservices monitoring. For long read I suggest reading [this page](http://microservices.io/patterns/observability/health-check-api.html). 10 | 11 | ## Usage 12 | 13 | ```bash 14 | cd my-micro-project/ 15 | npm install --save micro-health 16 | ``` 17 | 18 | and add use the package like this: 19 | 20 | ```javascript 21 | // index.js 22 | const health = require('micro-health') 23 | 24 | const microFn = () => 'Hello World' 25 | module.exports = health(microFn) 26 | ``` 27 | 28 | then just run your microservice normally and it will expose a `/health` endpoint. 29 | 30 | By default the health request it will return you an HTTP 200 response with a 'success' message and it will fail if the microservice isn't available, this guarantees that your microservice is working or not regardless of the other functions of microservice itself. Also it supports a custom errorChecker: 31 | 32 | ```javascript 33 | // index.js 34 | const health = require('micro-health') 35 | 36 | const microFn = (req, res) => { 37 | // Wonderful code 38 | } 39 | 40 | const errorChecker = async () => { 41 | // Check if database pool connection is full 42 | // Check if there is still space on filesystem 43 | // Check if external APIs are working 44 | try { 45 | await customChecking() 46 | catch (e) { 47 | return { error: e } 48 | } 49 | // You can also return false to pass the health checking 50 | } 51 | 52 | module.exports = health(microFn, errorChecker) 53 | ``` 54 | 55 | If error aren't falsy the health request will return an HTTP 500 response with the returned object by the error checker. 56 | 57 | There are several tools for health checking such us: 58 | - [Microservice Graph Explorer](https://github.com/hootsuite/microservice-graph-explorer) 59 | - [nginx](https://github.com/nginxinc/NGINX-Demos/tree/master/fun-with-health-checks) 60 | - [CoScale](https://www.coscale.com) 61 | 62 | Or... you can just build your own! 63 | 64 | ## Contributing 65 | 66 | 1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device 67 | 2. Link the package to the global module directory: `npm link` 68 | 3. Within the module you want to test your local development instance of micro-cacheable, just link it to the dependencies: `npm link micro-health`. Instead of the default one from npm, node will now use your clone of micro-health! 69 | 70 | ## Credits 71 | 72 | Thanks to [ZEIT](https://zeit.co) Team for giving us [micro](https://github.com/zeit/micro) to make our life easier! 73 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | const { send } = require('micro') 2 | 3 | module.exports = (microFn, errorChecker) => async (req, res) => { 4 | if (!req.url.includes('/health')) { 5 | return microFn(req, res) 6 | } 7 | 8 | let error 9 | if (errorChecker) { 10 | error = await errorChecker() 11 | } 12 | 13 | if (error) { 14 | send(res, 500, error) 15 | } 16 | 17 | return 'success' 18 | } 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "micro-health", 3 | "version": "2.0.0", 4 | "description": "An extension of micro with a Health Check API", 5 | "author": "fmiras", 6 | "license": "MIT", 7 | "repository": "fmiras/micro-health", 8 | "bugs": "https://github.com/fmiras/micro-health/issues", 9 | "homepage": "https://github.com/fmiras/micro-health", 10 | "keywords": [ 11 | "micro", 12 | "health", 13 | "microservices", 14 | "micro-health", 15 | "health-check-api" 16 | ], 17 | "main": "lib/index.js", 18 | "files": [ 19 | "lib" 20 | ], 21 | "scripts": { 22 | "test": "xo && nyc ava", 23 | "precommit": "lint-staged && ava" 24 | }, 25 | "lint-staged": { 26 | "*.js": [ 27 | "xo --fix" 28 | ] 29 | }, 30 | "xo": { 31 | "extends": "prettier" 32 | }, 33 | "devDependencies": { 34 | "ava": "1.3.0", 35 | "husky": "1.3.0", 36 | "lint-staged": "8.1.0", 37 | "micro": "9.3.3", 38 | "nyc": "13.3.0", 39 | "prettier": "1.17.0", 40 | "rewire": "4.0.1", 41 | "sinon": "7.2.5", 42 | "xo": "0.24.0" 43 | }, 44 | "peerDependencies": { 45 | "micro": "^9.1.4" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const test = require('ava') 2 | const sinon = require('sinon') 3 | const rewire = require('rewire') 4 | 5 | const health = rewire('../lib') 6 | 7 | test('Return success on health check', t => { 8 | const microFn = () => 'Micro Response' 9 | const handler = health(microFn) 10 | 11 | const req = { url: '/health' } 12 | t.is('success', handler(req)) 13 | }) 14 | 15 | test('Execute microservice if isn\'t health check', t => { 16 | const microFn = () => 'Micro Response' 17 | const handler = health(microFn) 18 | 19 | const req = { url: '/' } 20 | t.is('Micro Response', handler(req)) 21 | }) 22 | 23 | test('With custom checker if !error then success', t => { 24 | const microFn = () => 'Micro Response' 25 | const errorChecker = sinon.stub() 26 | errorChecker.callsFake(() => false) 27 | const handler = health(microFn, errorChecker) 28 | 29 | const req = { url: '/health' } 30 | handler(req) 31 | t.true(errorChecker.calledOnce) 32 | }) 33 | 34 | test('With custom checker if !error send 500', t => { 35 | const microFn = () => 'Micro Respo nse' 36 | health.__set__('send', sinon.stub().callsFake(() => { 37 | throw new Error('Error!') 38 | })) 39 | const errorChecker = sinon.stub().returns({ error: 'big error' }) 40 | const handler = health(microFn, errorChecker) 41 | 42 | const req = { url: '/health' } 43 | t.throws(() => handler(req)) 44 | }) 45 | 46 | test('With custom checker that isn\'t a function', t => { 47 | const microFn = () => 'Micro Respo nse' 48 | health.__set__('send', sinon.stub().callsFake(() => { 49 | throw new Error('Error!') 50 | })) 51 | const errorChecker = { foo: 'bar' } 52 | const handler = health(microFn, errorChecker) 53 | 54 | const req = { url: '/health' } 55 | t.throws(() => handler(req)) 56 | }) 57 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fmiras/micro-health/282536c8bd3b066e3b0c3f14c9c6e88856a338c9/yarn.lock --------------------------------------------------------------------------------