定制 browncat/healthcheck-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

browncat/healthcheck-bundle

Composer 安装命令:

composer require browncat/healthcheck-bundle

包简介

Tool for health checks

README 文档

README

This bundle can be used to easily write health checks and expose endpoints which can be used by e.g. Kubernetes to determine the health of the application.

This bundle consists of two core components that interweave with eachother, checkers and checks.

  • A (health) check is an component which contains logic to check if the system is behaving correctly.
  • A (health) checker runs these checks when needed. For example when the endpoint /healthz is requested.

Usage

This package can be installed using composer:

composer require browncat/healthcheck-bundle

Enable endpoints

This bundle comes with a set of endpoints which can be enabled to expose the following set of endpoints:

  • /health/liveness Returns 503 if one or multiple checks fail. Coupled to LivenessChecker
  • /health/readiness Returns 503 if one or multiple checks fail. Coupled to ReadinessChecker
  • /health/startup Returns 503 if one or multiple checks fail. Coupled to StartupChecker
  • /healthz JSON result of all checks, returns 503 if one or multiple checks fail.

To enable them all add the following to your routes.yaml:

health:
  resource: "@HealthCheckBundle/Resources/config/routes.xml"

Bundle provided health checks

This bundle comes with some pre defined health checks. These checks are not enabled by default as to not get into the way of your workflow. They can be enabled and configured through Symfony's config component.

Enable bundle provided health checks

To enable for example the check doctrine.connection create or modify the file config/packages/healthcheck.yaml and add the following:

# config/packages/healthcheck.yaml

health_check:
    checks:
        doctrine.connection:

A list of package provided health checks can be found here.

The config above will enable the doctrine connection check for all available checkers. To use a subset of checkers add the following to the config:

# config/packages/healthcheck.yaml

health_check:
    checks:
        doctrine.connection:
            checkers:
                - Browncat\HealthCheckBundle\Checker\LivenessChecker
                - Browncat\HealthCheckBundle\Checker\ReadinessChecker

A list of availble checkers can be found here

List of bundle provided health checks

id description since
doctrine.connection Checks if all connections configured in doctrine work. v0.1.0

Creating your own health checks

Health checks are defined in classes extending Browncat\HealthCheckBundle\Check\HealthCheck. For example, you may want to check the connection between the application and a remote system:

// src/Check/ExampleCheck.php
<?php

namespace App\Check;

use Browncat\HealthCheckBundle\Check\HealthCheck;
use Browncat\HealthCheckBundle\Checker\LivenessChecker;
use Browncat\HealthCheckBundle\Checker\ReadinessChecker;
use Psr\Container\ContainerInterface;

final class ExampleCheck extends HealthCheck
{
    // Name of the health check
    protected $name = 'example:connection';

    // List of checkers who should execute this check.
    public static $checkers = [ReadinessChecker::class, LivenessChecker::class];

    public function __construct(ContainerInterface $container)
    {
        if ($container->has('example')) {
            $exampleService = $container->get('example');
                
            if (!$exampleService->isConnected()) {
                $this->succeeded = false;
                $this->reasonPhrase = "Could not establish connection to example " . $connection->getName() . ".";
            } else {
                $this->succeeded = true;
            }
        } else {
            $this->skipped = true;
            $this->reasonPhrase = "example is not installed so this check is skipped.";
        }
    }
}

Registering the health check

Health checks must be registered as services and tagged with the health_check.check tag. If you’re using the default services.yaml configuration, this is already done for you, thanks to autoconfiguration.

Naming a check

A check should have a common name. This makes sure it can be located if a big list of checks is executed. A check can be named by populating the proteced $name.

// src/Check/ExampleCheck.php
use Browncat\HealthCheckBundle\Check\HealthCheck;

final class ExampleCheck extends HealthCheck
{
    protected $name = 'example:connection';

    ...
}

Passing or failing a check

A check can be failed or passed by passing a boolean value to the $succeeded propety.

// src/Check/ExampleCheck.php
...
use Browncat\HealthCheckBundle\Check\HealthCheck;
...
final class ExampleCheck extends HealthCheck
{
    public function __construct(SomeService $someService)
    {
        if ($someService->isLoaded() {
            $this->succeeded = true 
        } else {
            $this->succeeded = false;
            // (optional) set a reason for the failed test
            $this->reasonPhrase = "SomeService Could not be loaded!";
        }

        
    }
}

Skipping a check

To skip a check set the property $skipped to true.

// src/Check/ExampleCheck.php
...
use Browncat\HealthCheckBundle\Check\HealthCheck;
use Psr\Container\ContainerInterface;
...
final class ExampleCheck extends HealthCheck
{
    public function __construct(ContainerInterface $container)
    {
        if (!$container->has('someService')) {
            $this->skipped = true;
            $this->reasonPhrase = 'SomeService is skipped because it does not exist.';
        }
        ...
    }
}

(Optional) set checkers

By default all checkers (readiness, liveness and maybe some other configured ones) run the check you've written. If you want to narrow the check down to only run with a specific checker populate public static $checkers with the class references of the desired checker.

// src/Check/ExampleCheck.php
use Browncat\HealthCheckBundle\Check\HealthCheck;
use Browncat\HealthCheckBundle\Checker\ReadinessChecker;

final class ExampleCheck extends HealthCheck
{
    public static $checkers = [ReadinessChecker::class]; 

    ...
}

List of available checkers

  • Browncat\HealthCheckBundle\Checker\LivenessChecker
  • Browncat\HealthCheckBundle\Checker\ReadinessChecker
  • Browncat\HealthCheckBundle\Checker\StartupChecker
  • Browncat\HealthCheckBundle\Checker\GlobalHealthChecker (this one processes all registered checks no matter what)

browncat/healthcheck-bundle 适用场景与选型建议

browncat/healthcheck-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.21k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 06 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 browncat/healthcheck-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 browncat/healthcheck-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 browncat/healthcheck-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.21k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-06