openconext / monitor-bundle
一个为Symfony 6/7应用程序提供健康和info端点的Symfony包。
Requires
- php: >=8.2, <9.0-dev
- doctrine/dbal: ^3.1|^4.0
- endroid/installer: ^1.4
- symfony/dependency-injection: ^6.3|^7.0
- symfony/framework-bundle: ^6.3|^7.0
- webmozart/assert: ^1.10
Requires (Dev)
- malukenho/docheader: ^1.0
- mockery/mockery: ^1.3.5|^1.4.4
- php-parallel-lint/php-parallel-lint: ^1.3
- phpmd/phpmd: ^2.13
- phpunit/phpunit: ^9.6|^10.4
- sebastian/phpcpd: ^4.1|^5.0|^6.0
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-09-18 07:36:35 UTC
README
一个为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项目中使用的发布策略的更多信息。