@5app/health-check-helpers 中文文档教程

发布于 5年前 浏览 26 项目主页 更新于 3年前

health-check-helpers

一组帮助确定服务是否健康的函数

Usage

npm install --save @5app/health-check-helpers

然后,在您的应用程序中,您可以导入和使用任意数量的助手:

const {kueStats} = require('@5app/health-check-helpers');
const queueInstance = require('./queueInstance'); // your Kue instance

async function serviceHealth() {
  try {
    const {completeCount, failedCount} = await kueStats(queueInstance);

    return {
      healthy: true,
      kue: {
        completeCount,
        failedCount,
      },
    };
  }
  catch (error) {
    return {
      healthy: false,
      kue: {
        error: error.message,
      },
    };
  }
}

Motivation

我们最终在微服务之间多次复制相同的代码,所以我们想为什么不将它提取到模块并在多个项目中重用它。

Helpers

这个包包括帮助程序来获取一些系统基本统计数据(CPU、内存、pid)、关于 KueMySQL(进行中)。

kueStats

提供有关 Kue 的基本统计数据。 这相当于 Kue REST API 的 /stats 端点。

示例:

{
    completeCount: 42,
    failedCount: 1,
    inactiveCount: 0,
    activeCount: 0,
    delayedCount: 0
}

processMetadata

关于当前进程和平台的信息:

示例:

{
  platform:{
      type: 'linux',
      freeMemory: '324.320 MB',
      totalMemory: '5957.363 MB',
      startedOn: new Date('2019-04-03T18:13:37.794Z')
   },
   process:{
      pid: 16,
      cpuUsage:{
         user: 1370000,
         system: 780000
      },
      startedOn: new Date('2019-04-09T14:13:53.726Z')
   }
}

healthCheckServer

创建一个基本的http 服务器来处理应用程序健康检查需求。 该服务器需要在您的应用程序(例如您的服务器)的主线程中启动,默认情况下它将允许检查进程是否响应。 您可以将您的应用程序依赖项列表添加到此服务器,以允许检查这些依赖项是否健康。

示例:

const {healthCheckServer, bullStats} = require('@5app/health-check-helpers');

healthCheckServer({
  port: 8888, // this is optional and it will default to the value of the environement variable `HEALTHCHECK_PORT` if specified, otherwise it falls back to the port `9999` 
  dependencies: [
    {
      name: 'bull', // dependency name
      check: () => bullStats(myQueueObject),
    },
  ],
});

检查功能可以是同步的或异步的。 每次服务器收到请求时,服务器都会不带参数地调用它们。

注意:目前服务器为一般健康状态提供 1 个端点 (/),不提供特殊的 readinessliveness 和 debug 端点。

Other tools

bin/check.js

存储库的根目录中提供了一个用于进行健康检查查询的简单脚本。 您可以在 Dockerfile 中使用它,使用以下命令对 healthCheckServer 实例执行 ping 操作:

HEALTHCHECK --retries=2 --interval=10s --timeout=5s --start-period=5s CMD node node_modules/@5app/health-check-helpers/bin/check.js

这可以取代传统上使用 curl 进行的检查。

您可以使用以下环境变量配置脚本:

SettingEnvironement variableDefault value
server portHEALTHCHECK_PORT9999, the same as the default port of healthCheckServer
server hostHEALTHCHECK_HOSTlocalhost
request timeoutHEALTHCHECK_TIMEOUT50000 milliseconds
request pathHEALTHCHECK_PATH/

health-check-helpers

A set of functions to help determine if a service is healthy or not

Usage

npm install --save @5app/health-check-helpers

Then, in your application, you can import and use any number of helpers:

const {kueStats} = require('@5app/health-check-helpers');
const queueInstance = require('./queueInstance'); // your Kue instance

async function serviceHealth() {
  try {
    const {completeCount, failedCount} = await kueStats(queueInstance);

    return {
      healthy: true,
      kue: {
        completeCount,
        failedCount,
      },
    };
  }
  catch (error) {
    return {
      healthy: false,
      kue: {
        error: error.message,
      },
    };
  }
}

Motivation

We ended up duplicating the same code multiple times between microservices, so we thought why not extracting it into modules and reuse it across multiple projects.

Helpers

This package includes helpers to fetch some system basic stats (CPU, memory, pid), stats about Kue, and MySQL (work in progress).

kueStats

Provide basic stats about Kue. This is the equivalent of the /stats endpoint of Kue REST API.

Example:

{
    completeCount: 42,
    failedCount: 1,
    inactiveCount: 0,
    activeCount: 0,
    delayedCount: 0
}

processMetadata

Information about the current process and platform:

Example:

{
  platform:{
      type: 'linux',
      freeMemory: '324.320 MB',
      totalMemory: '5957.363 MB',
      startedOn: new Date('2019-04-03T18:13:37.794Z')
   },
   process:{
      pid: 16,
      cpuUsage:{
         user: 1370000,
         system: 780000
      },
      startedOn: new Date('2019-04-09T14:13:53.726Z')
   }
}

healthCheckServer

Creates a basic http server to handle application health checking requirements. This server needs to be started in the main thread of your application (e.g. your server) and by default it will allow checking if the process is responsive or not. You can add the list of your app dependencies to this server to allow checking if these dependencies are healthy or not.

Example:

const {healthCheckServer, bullStats} = require('@5app/health-check-helpers');

healthCheckServer({
  port: 8888, // this is optional and it will default to the value of the environement variable `HEALTHCHECK_PORT` if specified, otherwise it falls back to the port `9999` 
  dependencies: [
    {
      name: 'bull', // dependency name
      check: () => bullStats(myQueueObject),
    },
  ],
});

The check functions can be sync or async. They will be called by the server without parameters every time the server gets a request.

Note: currently the server provides 1 endpoint (/) for general health status and doesn't provide special readiness, liveness, and debug endpoints.

Other tools

bin/check.js

A simple script for making health check queries is available in the root of the repository. You can use it from your Dockerfile to ping the instance of healthCheckServer using the following command:

HEALTHCHECK --retries=2 --interval=10s --timeout=5s --start-period=5s CMD node node_modules/@5app/health-check-helpers/bin/check.js

This can replace checks that you would traditionally make with curl.

You can configure the script using the following environment variables:

SettingEnvironement variableDefault value
server portHEALTHCHECK_PORT9999, the same as the default port of healthCheckServer
server hostHEALTHCHECK_HOSTlocalhost
request timeoutHEALTHCHECK_TIMEOUT50000 milliseconds
request pathHEALTHCHECK_PATH/
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文