gritzkoo/php-health-checker

简单健康检查包

v1.0.0 2022-06-25 19:12 UTC

This package is auto-updated.

Last update: 2024-09-27 03:56:45 UTC


README

test Coverage Status GitHub issues GitHub pull requests GitHub GitHub repo size Packagist Downloads GitHub tag (latest by date) Packagist Stars GitHub language count

这是一个PHP包,允许您跟踪应用程序的健康状况,提供两种检查方法:方法 $checkt->liveness()$checker->readiness()

如何安装

composer require gritzkoo/php-health-checker

启动检查器

<?php

require 'vendor/autoload.php';

use Gritzkoo\HealthChecker\Check;
use Gritzkoo\HealthChecker\HealthChecker;

$checker = new HealthChecker([
    // optional prop, will be used to readiness response
    'name' => 'My application name', 
    // version is used in liveness and readiness actions
    'version' => 'v1.0.0', // more details in version section of this document!
    // the list of checks you whant to test, is just an array of array with name and handle function
    'integrations' => [
        [
            // the name of the integration you are trying to verify
            'name' => 'github status check',
            // here is just an example of how to make your own check
            // you can inject this function the way you want, you only need to return
            // a instance of Gritzkoo\HealthChecker\Check
            // The HealthCheker will interpret your check fails when the $check->error is not empty
            'handle' => function () {
                $check = new Check([
                    'url' => 'https://github.com/status'
                ]);
                $ch = curl_init($check->url);
                try {
                    $response = curl_exec($ch);
                } catch (Exception $e) {
                    $check->error = $e;
                }
                $info = curl_getinfo($ch);
                curl_close($ch);
                if ($info['http_code'] != 200) {
                    $check->error = [
                        'response' => $response,
                        'info' => $info
                    ];
                }
                return $check;
            }
        ]
    ]
]);

使用示例

您可以在以下链接中查看这个完整的Laravel应用程序,其中已安装此包:

https://github.com/gritzkoo/php-health-checker-example-app

$checker->liveness()

将返回一个 数组,您可以将它转换为以下 JSON 格式,并允许您检查应用程序是否 正常 而无需检查任何类型的集成。

{
    "status": "fully functional", 
    "version": "v1.0.0"
}

$checker->readiness()

将返回一个 数组,您可以将它转换为以下 JSON 格式,并允许您检查应用程序是否正在运行,以及是否所有在配置列表中通知的集成都在运行。

{
    "name": "My application name",
    "version": "v1.0.0",
    // the main status checks, will return true when all integrations does not fail
    "status": true, 
    // ISO 8601 date
    "date": "2022-06-25T11:52:56-03:00",
    "duration": 0.08681011199951172,
    "integrations": [
        {
            "name": "github status check",
            "status": true,
            "response_time": 0.08406686782836914,
            "url": "https://github.com/status",
            "error": null
        }
    ]
}

创建一个HTTP接口以暴露问题

使用Laravel应用程序示例 https://github.com/gritzkoo/php-health-checker-example-app

一旦创建了 Gritzkoo\HealthChecker\HealthChecker 的实例,您应该在应用程序中创建两个路由来公开 livenessreadiness 动作,如下所示:

然后,您可以通过手动调用这些端点来查看应用程序的健康状况,但如果您使用现代Kubernetes部署,您可以根据以下设置配置您的图表以检查应用程序:

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: 'node' #your application image
    args:
    - /server
    livenessProbe:
      httpGet:
        path: /health-check/liveness
        port: 80
        httpHeaders:
        - name: Custom-Header
          value: Awesome
      initialDelaySeconds: 3
      periodSeconds: 3
  - name: readiness
    image: 'node' #your application image
    args:
    - /server
    readinessProbe:
      httpGet:
        path: /health-check/readiness
        port: 80
        httpHeaders:
        - name: Custom-Header
          value: Awesome
      initialDelaySeconds: 3
      periodSeconds: 3