Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Oct 15, 2025

Summary

  • Add connection pool statistics to Sentry breadcrumbs for database events
  • Captures max connections, waiting connections, and current connections in use
  • Provides better visibility into database connection pool usage for debugging

Details

This change enhances the Sentry integration by adding database connection pool metrics to breadcrumbs when database events occur. The following metrics are now included:

  • max: Maximum number of connections in the pool
  • waiting: Number of connections waiting in the channel
  • use: Number of connections currently in use

The pool information retrieval is wrapped in a try-catch block to ensure that any errors during metrics collection are captured by Sentry without breaking the breadcrumb creation process.

Test plan

  • Verify breadcrumbs are created with pool metrics for database events
  • Confirm that errors during pool metrics collection are properly handled
  • Check that the metrics accurately reflect the connection pool state

Summary by CodeRabbit

  • 新功能

    • 面包屑现可展示数据库与 Redis 连接池统计(最大连接数、等待数、当前使用量),便于定位性能与连接问题。
    • HTTP 活动的面包屑类型与标签已调整为更明确的“http”类别,提升日志可读性。
  • 稳定性修复

    • 获取连接池信息时发生异常将被安全捕获并记录,不影响后续处理,提升监控与诊断可靠性。

Add connection pool statistics (max connections, waiting connections, and current connections in use) to Sentry breadcrumbs for database events. This provides better visibility into database connection pool usage when debugging issues.
@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

Walkthrough

为 DbEvent\QueryExecuted 与 Redis 命令面包屑增强池统计:在监听器中通过 DbPoolFactoryRedisPoolFactory 获取连接池统计(max、waiting、in-use),将统计加入 breadcrumb 数据;对获取过程使用 try-catch 并通过 Sentry captureException 上报异常。另将 Guzzle HTTP 面包屑类型从 DEFAULT 改为 HTTP,标签改为 http

Changes

Cohort / File(s) Change Summary
监听器:数据库与 Redis 池统计增强
src/sentry/src/Listener/EventHandleListener.php
handleDbQueryExecutedhandleRedisCommandExecuted 中分别尝试通过 DbPoolFactory / RedisPoolFactory 获取对应连接池并读取池统计(max connections、waiting、in-use),将池统计并入 breadcrumb 数据;对池获取封装 try/catch,并在异常时调用 Sentry\captureException;新增相应 use 导入。
切面:Guzzle 面包屑类型与标签调整
src/sentry/src/Aspect/GuzzleHttpClientAspect.php
将记录的 breadcrumb 类型由 DEFAULT 改为 HTTP,并将标签由 'guzzle' 改为 'http'

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant App as 应用
    participant Listener as EventHandleListener
    participant DbPoolFactory as DbPoolFactory
    participant DbPool as DB 连接池
    participant RedisPoolFactory as RedisPoolFactory
    participant RedisPool as Redis 连接池
    participant Sentry as Sentry

    App->>Listener: 触发 DbEvent\QueryExecuted / RedisCommandExecuted
    rect rgb(245,250,255)
    note right of Listener: 尝试收集对应池的统计并加入 breadcrumb
    alt 处理 DB 事件
        Listener->>DbPoolFactory: 获取 DB 池
        DbPoolFactory-->>Listener: 返回 Pool 或 抛出异常
        alt 成功
            Listener->>DbPool: 读取 max/waiting/in-use
            DbPool-->>Listener: 池统计
            Listener->>Listener: 将统计加入 DB breadcrumb
        else 异常
            Listener->>Sentry: captureException(e)
        end
    end
    alt 处理 Redis 事件
        Listener->>RedisPoolFactory: 获取 Redis 池
        RedisPoolFactory-->>Listener: 返回 Pool 或 抛出异常
        alt 成功
            Listener->>RedisPool: 读取 max/waiting/in-use
            RedisPool-->>Listener: 池统计
            Listener->>Listener: 将统计数组加入 Redis breadcrumb
        else 异常
            Listener->>Sentry: captureException(e)
        end
    end
    end
    Listener-->>App: 继续处理(面包屑包含或不包含池统计)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

我是仓耳兔,蹦进监听里,
数着池中线,max 与 in-use 奇;
若风里起错,我把异常递,
面包屑撒路上,调试更利。 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 标题包含 feat(sentry) 前缀,准确点明了本次变更的核心内容是在 Sentry 面包屑中添加数据库连接池指标,既简洁又明确,能够让团队成员快速了解此次 PR 的主要改动。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/sentry-db-pool-metrics

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a0a7628 and e19fe3d.

📒 Files selected for processing (1)
  • src/sentry/src/Listener/EventHandleListener.php (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/sentry/src/Listener/EventHandleListener.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 PHPStan (2.1.31)

At least one path must be specified to analyse.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f064a47 and f24894a.

📒 Files selected for processing (1)
  • src/sentry/src/Listener/EventHandleListener.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Listener/EventHandleListener.php
**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Listener/EventHandleListener.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
🔇 Additional comments (2)
src/sentry/src/Listener/EventHandleListener.php (2)

23-23: 导入语句正确

导入 PoolFactorycaptureException 函数的语句是必要的,格式正确。

Also applies to: 41-41


287-292: 验证连接池方法可用性

请确认 getPool($event->connectionName) 返回的 Hyperf 连接池实例中包含并正确实现以下方法:

  • getOption()->getMaxConnections()
  • getConnectionsInChannel()
  • getCurrentConnections()

如果这些方法定义在 vendor/hyperf/db-connection/src/**,请检查对应类并确保其行为符合预期。

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (1)
src/sentry/src/Listener/EventHandleListener.php (1)

292-294: 一致化异常上报的处理方式:已修复,赞

这里改为调用 $this->captureException($e) 与类内策略保持一致,解决了此前评审指出的不一致问题。

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a15948 and a0a7628.

📒 Files selected for processing (1)
  • src/sentry/src/Listener/EventHandleListener.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Listener/EventHandleListener.php
**/*.php

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Listener/EventHandleListener.php
🔇 Additional comments (2)
src/sentry/src/Listener/EventHandleListener.php (2)

23-23: LGTM:新增 DB PoolFactory 引用合理

用于采集连接池指标,搭配后续 try-catch 使用无问题。


30-30: LGTM:新增 Redis PoolFactory 引用合理

用于采集 Redis 连接池指标,后续代码已做异常保护。

@huangdijia huangdijia merged commit 77d0796 into main Oct 15, 2025
16 checks passed
@huangdijia huangdijia deleted the feat/sentry-db-pool-metrics branch October 15, 2025 08:07
huangdijia added a commit that referenced this pull request Oct 15, 2025
* feat(sentry): add database connection pool metrics to breadcrumbs

Add connection pool statistics (max connections, waiting connections, and current connections in use) to Sentry breadcrumbs for database events. This provides better visibility into database connection pool usage when debugging issues.

* feat(sentry): update breadcrumb type for Guzzle HTTP requests

* refactor(EventHandleListener): replace direct captureException call with method reference

* feat(redis): enhance Redis command breadcrumb with connection pool metrics

* fix(EventHandleListener): correct PoolFactory alias for database connection pool

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants