glesys/butler-health

Glesys Butler Health 包。

v0.6.2 2024-07-31 13:50 UTC

README

🚧 尚未准备用于生产。

Butler Health

Laravel 包,用于返回应用程序的 "健康" 状态。

入门

composer require glesys/butler-health
php artisan vendor:publish --provider="Butler\Health\ServiceProvider" --tag=config
php artisan serve # visit https://:8000/health

路由

默认路由为 /health,配置在 butler.health.route。端点将以 JSON 格式返回数据。

{
    "about": {
        "environment": {},
        "cache": {},
        "drivers": {},
        "butlerHealth": {
            "version": "0.1"
        },
    },
    "checks": [
        {
            "name": "Database",
            "slug": "database",
            "group": "core",
            "description": "Check all database connections.",
            "runtimeInMilliseconds": 10,
            "result": {
                "value": 1,
                "message": "Connected to all databases.",
                "state": "ok"
            }
        }
    ]
}

自定义路由

butler.health.route 设置为假值以禁用默认路由。然后添加您自己的路由。

Route::get('/status', Butler\Health\Controller::class)->middleware('api');

检查

该包自带一些检查,请参阅 检查

butler.health.checks 中注册您想要的检查。

// config/butler.php
return [
    'health' => [
        // ...
        'checks' => [
            Butler\Health\Checks\Database::class,
            App\Health\MyCheck::class,
        ],
    ],
];

创建检查

扩展 Butler\Health\Check 并将其添加到 butler.health.checks,完成。

自定义关于信息

您可以推送额外的 "关于" 信息。

Butler\Health\Repository::add('environment', ['operatingSystem' => php_uname('s')]);

Butler\Health\Repository::add('environment', fn () => ['time' => time()]);
{
    "about": {
        "environment": {
            "operatingSystem": "Linux",
            "time": 1678100209
        },
        "cache": {},
        "drivers": {},
        "butlerHealth": {},
    },
    "checks": []
}

心跳

配置 butler.health.heartbeat.urlbutler.health.heartbeat.token 以启用。

heartbeat('foo bar'); // POST http://heartbeat.localhost/foo-bar/1

heartbeat('foo baz', 5); // POST http://heartbeat.localhost/foo-baz/5

日志驱动程序

为防止在本地环境中进行 HTTP 请求,将 butler.health.heartbeat.driver 设置为 "log"。

伪造

您可以在测试中伪造 Laravel 的 Http 客户端,而不是伪造心跳,请参阅以下示例。

public function test_something()
{
    Heartbeat::fake();

    // Assert that nothing was sent...
    Heartbeat::nothingSent();

    // Assert a heartbeat was not sent...
    Heartbeat::assertNotSent('foobar');

    heartbeat('foobar');

    // Assert 1 heartbeat was sent...
    Heartbeat::assertSentCount(1);

    // Assert a heartbeat was sent...
    Heartbeat::assertSent('foobar');
}

测试

vendor/bin/phpunit
vendor/bin/pint --test

如何贡献

开发发生在 GitHub 上;任何使用拉取请求的典型工作流程都受到欢迎。本着同样的精神,我们使用 GitHub 问题跟踪器来处理所有报告(无论报告的性质如何,功能请求、错误等)。

所有更改都应通过单元测试来覆盖,如果测试不可行或非常不切实际,则需要在拉取请求的评论部分进行讨论。

代码标准

由于库打算在 Laravel 应用程序中使用,我们鼓励代码标准遵循 上游 Laravel 实践 - 简而言之,这意味着 PSR-2PSR-4