openconext/monitor-bundle

一个为Symfony 6/7应用程序提供健康和info端点的Symfony包。

安装次数: 64,641

依赖者: 0

建议者: 0

安全: 0

星标: 5

关注者: 12

分支: 2

开放问题: 2

类型:symfony-bundle

4.3.1 2024-06-17 08:59 UTC

README

Code_Checks

一个为Symfony 5/6/7添加了/internal/health和/internal/info端点的包。

这些端点返回JSON响应。/internal/info端点尽可能地提供有关当前安装的应用程序版本的信息。这些信息基于安装的构建路径。同时还包括当前活动的Symfony环境和是否启用调试器。

/internal/health端点报告应用程序的健康状况。例如,负载均衡器可以使用这些信息。示例输出

{"status":"UP"}

当健康检查失败时,HTTP响应状态码将为503。JSON响应格式如下

{"status":"DOWN", "message":"Lorem ipsum dolor sit"}

❗ 请注意,只有第一个失败的检查会被报告。

❗ 自3.1.0版本起,我们开始在/internal/上公开/health和/info路由。在下一个主要版本中,我们将停止在/上提供/info和/health端点。

安装

  • 将包添加到Composer文件中

    composer require openconext/monitor-bundle
  • 如果您不使用Symfony Flex,您必须在应用程序中手动启用该包

    // config/bundles.php
    // in older Symfony apps, enable the bundle in config/bundles.php
    return [
    // ...
     OpenConext\MonitorBundle\OpenConextMonitorBundle::class => ['all' => true],
    ];
  • 通过在config/routes.yaml中添加来包含路由配置

    open_conext_monitor:
        resource: "@OpenConextMonitorBundle/src/Controller"
        type: attribute
        prefix: /

注意:这目前由包本身通过名为https://github.com/endroid/installer的外部依赖项完成

  • 在config/packages/security.yaml中添加安全异常(如果需要的话)
    security:
        firewalls:
            monitor:
                pattern: ^/internal/(info|health)$
                security: false
    
  • /internal/info和/internal/health端点现在对所有用户都可用。是否应用自定义访问限制取决于此包的实施者。

添加健康检查

Monitor包含了两个健康检查。这些检查是

  • 基于Doctrine配置的数据库连接检查
  • 会话状态检查

创建检查器

可以实现HealthCheckInterface来创建自己的健康检查。下面的示例显示了该接口的实现可能的样子。

use OpenConext\MonitorBundle\HealthCheck\HealthCheckInterface;
use OpenConext\MonitorBundle\HealthCheck\HealthReportInterface;
use OpenConext\MonitorBundle\Value\HealthReport;

class ApiHealthCheck implements HealthCheckInterface
{
    public function __construct(private readonly MyService $service)
    {
    }

    public function check(HealthReportInterface $report): HealthReportInterface
    {
        if (!$this->service->everythingOk()) {
            // Return a HealthReport with a DOWN status when there are indications the application is not functioning as
            // intended. You can provide an optional message that is displayed alongside the DOWN status.
            return HealthReport::buildStatusDown('Not everything is allright.');
        }
        // By default, return the report that was passed along as a parameter to the check method
        return $report;
    }
}

❗ 请注意,检查方法接收并返回一个HealthReport。Health report会传递给注册的健康检查器的链。如果一切正常,只需返回传递给方法的那份报告。

注册检查器

通过实现HealthCheckInterface,您可以注册自己的健康检查。该接口会自动标记,因此您无需自己执行。

覆盖默认健康检查

要使用DoctrineConnectionHealthCheck运行自定义查询,您需要在自己的项目中覆盖它。

例如,在您的使用monitor包的ACME包中

services.yaml

    # Override the service in `/src/config/services.yaml`
    OpenConext\MonitorBundle\HealthCheck\DoctrineConnectionHealthCheck:
        # Point to your own implementation of the check
        class: Acme\GreatSuccessBundle\HealthCheck\DoctrineConnectionHealthCheck

其余的服务配置取决于您的需求。您可以注入参数、工厂调用和其他服务功能。

发布策略

请参阅https://github.com/OpenConext/Stepup-Deploy/wiki/Release-Management以获取有关Stepup项目中使用的发布策略的更多信息。