plusforta/health-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

plusforta/health-bundle

Composer 安装命令:

composer require plusforta/health-bundle

包简介

Symfony health check bundle inspired by Spatie's laravel-health

README 文档

README

A Symfony bundle for monitoring the health of your application. It provides a set of built-in health checks, an HTTP endpoint compatible with Oh Dear application health monitoring, and a simple API for creating your own custom checks.

This package was entirely inspired by the excellent spatie/laravel-health package.

Installation

composer require plusforta/health-bundle

Requirements: PHP 8.2+, Symfony 6.4 or 7.0

Configuration

Create config/packages/health.yaml:

health:
    endpoint:
        enabled: true
        path: /health-check-results
        secret: '%env(HEALTH_CHECK_SECRET)%'
        always_send_fresh_results: true
        failure_status_code: 200

    result_stores:
        json_file:
            enabled: true
            path: '%kernel.project_dir%/var/health-check-results.json'
        cache:
            enabled: true
            pool: cache.app
        doctrine:
            enabled: true
            connection: ~
            keep_history_for_days: 5

    notifications:
        enabled: false
        throttle_minutes: 60
        only_on_failure: true
        email:
            to: admin@example.com
            from: health@example.com
        slack:
            dsn: 'slack://token@default/channel'

    checks:
        database:
            class: Plusforta\Health\Check\Checks\DatabaseCheck
            label: Database Connection
        disk_space:
            class: Plusforta\Health\Check\Checks\UsedDiskSpaceCheck
            options:
                warning_threshold: 70
                error_threshold: 90

Oh Dear Integration

This bundle is designed to work with Oh Dear's application health monitoring out of the box. The JSON endpoint uses the ohdearapp/health-check-results package to produce output in the exact format Oh Dear expects:

{
    "finishedAt": 1234567890,
    "checkResults": [
        {
            "name": "Database",
            "label": "Database Connection",
            "notificationMessage": "",
            "shortSummary": "Ok",
            "status": "ok",
            "meta": {
                "connection_name": "default"
            }
        }
    ]
}

Simply point Oh Dear at your /health-check-results endpoint (with the secret token if configured) and it will start monitoring your checks.

You can also protect the endpoint with a secret token, passed via the X-Secret-Token header or ?secret= query parameter.

While built for Oh Dear, the JSON output is straightforward and can be consumed by any monitoring system.

Built-in Checks

CheckDescriptionRequires
DatabaseCheckVerifies database connectivity via SELECT 1doctrine/dbal
DatabaseConnectionCountCheckMonitors active database connectionsdoctrine/dbal
DatabaseSizeCheckChecks total database size in GBdoctrine/dbal
DatabaseTableSizeCheckChecks individual table sizedoctrine/dbal
CacheCheckTests cache read/write/delete operationssymfony/cache
RedisCheckVerifies Redis connectivity via PINGRedis extension or Predis
RedisMemoryUsageCheckMonitors Redis memory consumptionRedis extension or Predis
PingCheckMakes HTTP requests to verify external servicessymfony/http-client
MeilisearchCheckVerifies Meilisearch health endpointsymfony/http-client
UsedDiskSpaceCheckMonitors disk usage with configurable thresholdssymfony/process
EnvironmentCheckAsserts the application environment (prod, dev, etc.)-
DebugModeCheckAsserts the kernel debug mode state-
ProfilerCheckDetects if the WebProfilerBundle is active-
MessengerCheckHeartbeat mechanism to detect stuck message consumerssymfony/messenger

Creating Custom Checks

Creating a custom health check is straightforward. Extend the abstract Check class and implement the run() method:

<?php

declare(strict_types=1);

namespace App\Health\Checks;

use Plusforta\Health\Check\Check;
use Plusforta\Health\Result\Result;

class QueueDepthCheck extends Check
{
    public function __construct(
        private readonly int $warningThreshold = 100,
        private readonly int $errorThreshold = 500,
    ) {
    }

    public function run(): Result
    {
        $depth = $this->getQueueDepth();

        if ($depth >= $this->errorThreshold) {
            return Result::make("Queue depth is {$depth}")
                ->failed()
                ->shortSummary("{$depth} jobs")
                ->meta(['depth' => $depth]);
        }

        if ($depth >= $this->warningThreshold) {
            return Result::make("Queue depth is {$depth}")
                ->warning()
                ->shortSummary("{$depth} jobs")
                ->meta(['depth' => $depth]);
        }

        return Result::make()
            ->ok()
            ->shortSummary("{$depth} jobs")
            ->meta(['depth' => $depth]);
    }

    private function getQueueDepth(): int
    {
        // Your logic here
    }
}

The Result Object

The Result class uses a fluent builder pattern. Available methods:

  • Result::make(string $message = '') - Create a new result
  • ->ok(), ->warning(), ->failed(), ->crashed(), ->skipped() - Set the status
  • ->shortSummary(string) - A brief status string (shown in Oh Dear dashboard)
  • ->notificationMessage(string) - Message used in notifications
  • ->meta(array) - Arbitrary metadata (included in JSON output)

Registering Custom Checks

Register your check in your bundle configuration:

health:
    checks:
        queue_depth:
            class: App\Health\Checks\QueueDepthCheck
            label: Message Queue
            options:
                warning_threshold: 100
                error_threshold: 500

Or tag it as a service directly:

services:
    App\Health\Checks\QueueDepthCheck:
        tags: ['health.check']

Naming and Labels

Check names and labels are derived automatically from the class name. QueueDepthCheck becomes:

  • Name: QueueDepth
  • Label: Queue Depth

You can override both:

QueueDepthCheck::new()
    ->name('queue')
    ->label('RabbitMQ Queue Depth');

CLI Commands

# Run all health checks and display results
php bin/console health:check

# Run checks but don't store results or send notifications
php bin/console health:check --no-store --no-notification

# Exit with non-zero code if any check fails
php bin/console health:check --fail-on-error

# List all registered checks
php bin/console health:list

# Pause health checks (e.g. during deployments)
php bin/console health:pause

# Resume health checks
php bin/console health:resume

Events

The bundle dispatches events before and after each check runs:

  • CheckStartingEvent - Dispatched before a check runs
  • CheckEndedEvent - Dispatched after a check completes, includes the result

Use these to add logging, metrics, or custom side effects.

Result Stores

Results can be persisted to multiple stores simultaneously:

StoreDescription
InMemoryResultStoreStatic in-memory storage (useful for testing)
JsonFileResultStorePersists results to a JSON file
CacheResultStoreUses Symfony's cache component
DoctrineResultStoreDatabase storage with automatic history tracking and pruning

License

MIT

plusforta/health-bundle 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-24