Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add RedisCommandExecutedListener for enhanced Redis command tracking #838

Merged
merged 8 commits into from
Feb 9, 2025

Conversation

huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Feb 9, 2025

Summary by CodeRabbit

  • 新功能

    • 新增事件监听器,用于捕获和记录 Redis 命令执行事件,提升监控与追踪能力。
    • 添加了配置机制以启用所有 Redis 连接的事件处理。
  • Chores

    • 标记部分旧版 Redis 监控逻辑为弃用,后续版本将移除这些功能以优化系统维护。

Copy link

coderabbitai bot commented Feb 9, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

该 PR 更新了 Redis 相关的代码逻辑:在 RedisAspect 类中增加了对 CommandExecuted 类的检查,并添加了弃用标识,调整了 process 方法的控制流;同时新增了两个监听器类,分别为 RedisCommandExecutedListener(用于面包屑记录)和 TracingRedisListener(用于 Redis 命令的追踪),并在 ConfigProvider 中注册了这两类监听器。

Changes

文件 变更描述
src/.../Aspect/RedisAspect.php
src/.../Tracing/Aspect/RedisAspect.php
添加 CommandExecuted 的引入,标记类为弃用,并在 process 方法中增加对 CommandExecuted 类存在性的判断,调整了控制流。
src/.../Listener/RedisCommandExecutedListener.php
src/.../Tracing/Listener/TracingRedisListener.php
src/.../Listener/SetRedisEventEnableListener.php
新增监听器类:RedisCommandExecutedListener 用于面包屑记录,TracingRedisListener 用于 Redis 命令追踪,SetRedisEventEnableListener 用于启用 Redis 事件,均对事件和开关状态进行检查。
src/.../ConfigProvider.php 在 listeners 数组中新增 RedisCommandExecutedListener 与 TracingRedisListener 的注册,扩展了 Redis 事件监听功能。

Sequence Diagram(s)

sequenceDiagram
    participant Event as CommandExecuted事件
    participant Listener as RedisCommandExecutedListener
    participant Switcher as Switcher服务
    participant Integration as Integration模块

    Event->>Listener: 触发 CommandExecuted 事件
    Listener->>Switcher: 检查面包屑记录是否启用
    alt 面包屑记录禁用
        Listener-->>Listener: 直接返回,不处理事件
    else 面包屑记录启用
        Listener->>Integration: 添加面包屑(记录命令信息)
    end
Loading
sequenceDiagram
    participant Event as CommandExecuted事件
    participant TracingListener as TracingRedisListener
    participant Switcher as Switcher服务
    participant Tracer as 追踪器

    Event->>TracingListener: 触发 CommandExecuted 事件
    TracingListener->>Switcher: 检查追踪功能是否启用
    alt 追踪禁用
        TracingListener-->>TracingListener: 直接返回,不处理事件
    else 追踪启用
        TracingListener->>Tracer: 开始追踪 span(记录命令详情)
        TracingListener->>Tracer: 添加额外标签(如返回结果)
        TracingListener->>Tracer: 完成 span
    end
Loading

Possibly related PRs

Suggested reviewers

  • guandeng

Poem

我是一只快乐小兔,
蹦跳间领代码新语,
Redis指令翩翩起,
面包屑与追踪共嬉戏,
代码花园处处秀,前路同心共驰骋! 🐰✨

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.0.3)

/bin/bash: line 1: /vendor/bin/phpstan: No such file or directory


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ca73bf and 0c783a9.

📒 Files selected for processing (4)
  • src/sentry/src/ConfigProvider.php (2 hunks)
  • src/sentry/src/Listener/RedisCommandExecutedListener.php (1 hunks)
  • src/sentry/src/Listener/SetRedisEventEnableListener.php (1 hunks)
  • src/sentry/src/Tracing/Listener/TracingRedisListener.php (1 hunks)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 0

🧹 Nitpick comments (7)
src/sentry/src/Tracing/Listener/RedisCommandExecutedListener.php (3)

28-34: 在构造函数中直接调用 setRedisEventEnable 的影响
在构造函数中统一启用所有 Redis 连接的事件功能,可能在应用启动时带来额外的连接操作或资源开销。可考虑让用户自行配置或在特定初始化阶段调用,以减少对启动性能的潜在影响。


92-105: 异常捕获后再次抛出可考虑附加更多信息
当前在捕获后直接 throw $exception;若有更丰富的上下文(如用户 ID、请求信息等),可在抛出前写入到日志或 Sentry 自定义标签,方便后续排查。


111-116: 全局启用 Redis 事件的潜在影响
在遍历所有 Redis 配置时都设置 event.enable=true,会在所有环境或连接中启用事件。若某些场景不需要监听事件,建议提供更灵活的配置策略。

src/sentry/src/Aspect/RedisAspect.php (1)

24-25: 弃用注解可补充替代方案
已通过 @deprecated 标明 3.1 版本后将移除,但可在注释中进一步说明替换用法或迁移指导,帮助使用者尽快过渡。

src/sentry/src/Listener/RedisCommandExecutedListener.php (1)

62-67: 在所有连接中统一开启 Redis 事件
方法中将所有 Redis 连接都设置为开启事件,有利于统一管理和追踪。但若部分连接无需记录,也可能带来不必要的开销,可考虑在配置中做更精细的开启策略。

src/sentry/src/Tracing/Aspect/RedisAspect.php (2)

62-74: 建议优化性能关键指标的收集

当前实现在每次 Redis 操作时都收集了大量连接池相关的指标。建议考虑以下优化:

  1. 可以将部分非关键指标(如 idle_time、pool 相关指标)设置为可配置项
  2. 考虑增加采样率配置,避免在高频操作场景下产生过多追踪数据

76-78: 考虑启用被注释的追踪信息

被注释的代码包含了更详细的操作描述格式,建议考虑通过配置项来控制是否启用这些额外的追踪信息,以便在需要详细调试时使用。

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b7f495 and 030511c.

📒 Files selected for processing (4)
  • src/sentry/src/Aspect/RedisAspect.php (2 hunks)
  • src/sentry/src/Listener/RedisCommandExecutedListener.php (1 hunks)
  • src/sentry/src/Tracing/Aspect/RedisAspect.php (2 hunks)
  • src/sentry/src/Tracing/Listener/RedisCommandExecutedListener.php (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.6
🔇 Additional comments (8)
src/sentry/src/Tracing/Listener/RedisCommandExecutedListener.php (1)

48-50: 校验开关逻辑的使用恰当
通过 switcher 检查是否开启 Redis 的追踪,能有效避免在不需要追踪时进行多余操作。该逻辑实现简洁、明了,值得赞赏。

src/sentry/src/Aspect/RedisAspect.php (2)

18-18: 引入 CommandExecuted 类的做法合理
导入 Hyperf\Redis\Event\CommandExecuted 便于与新监听器结合使用,为后续弃用逻辑提供了判断。


43-45: 类存在性检查与开关判断结合有效
在此处通过 class_exists(CommandExecuted::class) 或开关判断,能避免重复处理 Redis 命令并确保兼容性,可行性较高。

src/sentry/src/Listener/RedisCommandExecutedListener.php (3)

23-28: 在构造函数中调用 setRedisEventEnable
与 Tracing 版本的 Listener 类似,这里在构造函数中统一启用所有 Redis 连接的事件处理。若不希望在所有环境中都强行开启,可提供可配置的接口来控制此行为。


37-47: 对事件类型的强制判断
process 方法中仅当事件为 CommandExecuted 且已启用面包屑时才处理。若未来需要支持自定义的 Redis 事件,可能需要额外的扩展或判断。


49-59: 通过 Breadcrumb 记录 Redis 命令执行信息
该段逻辑能将命令、参数、执行耗时等信息加入面包屑,极大地提升了可观测性与调试效率,值得肯定。

src/sentry/src/Tracing/Aspect/RedisAspect.php (2)

26-32: 请注意类的弃用时间表

该类将在 v3.2 版本中移除。建议及时规划迁移工作,将相关功能迁移到新的 RedisCommandExecutedListener 实现中。


49-54: 优化条件判断逻辑

新增的条件判断确保了在以下情况下会提前返回:

  1. 当 CommandExecuted 事件类存在时,避免重复追踪
  2. 当 Redis 追踪功能未启用时,减少不必要的处理

这种处理方式有助于平滑过渡到新的事件监听器架构。

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: 0

🧹 Nitpick comments (3)
src/sentry/src/Tracing/Listener/TracingRedisListener.php (3)

28-34: 在构造函数中默认启用 Redis 事件可能需要考虑可配置性
从构造函数中调用 setRedisEventEnable() 会对所有 Redis 连接启用事件追踪,如果有场景只想对部分连接进行事件捕获,可能需要提供可选的可配置方式,如在 config 中按连接配置是否启用事件。否则会造成对所有连接统一处理的刚性。


52-81: 注意记录的参数体量与潜在敏感信息
本段收集并记录大量数据库相关信息(如参数、Pool 状态),在高并发或参数体量较大的场景下,可能会影响性能或产生敏感数据泄漏的风险。可根据需求在 config 中增加字段过滤、或设定最大记录长度等策略。


111-116: 针对不同连接启用事件的灵活性
当前 setRedisEventEnable() 方法会为 redis 配置里所有连接都启用事件。如果后期需要区分启用策略,建议增加更精细的配置开关,以便对部分连接保留可选的禁用或启用。

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 030511c and 5ca73bf.

📒 Files selected for processing (2)
  • src/sentry/src/ConfigProvider.php (2 hunks)
  • src/sentry/src/Tracing/Listener/TracingRedisListener.php (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.6
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.0
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.6
🔇 Additional comments (5)
src/sentry/src/Tracing/Listener/TracingRedisListener.php (3)

36-41: 监听事件正确配置
listen() 方法仅监听 Hyperf\Redis\Event\CommandExecuted 事件,符合本监听器的核心用途,逻辑清晰。


48-50: 条件分支简化早返回提高可读性
在开头判断是否启用 tracingSpan 并及时返回是一个简洁的设计,可以减少函数深度,提高可读性。


92-105: 异常处理逻辑完善
在捕获到可抛异常时,使用 SpanStatus::internalError() 标记跨度,并将异常信息写入标签,然后重抛异常,方便上层逻辑统一处理,做法非常合适。

src/sentry/src/ConfigProvider.php (2)

59-59: 新增 RedisCommandExecutedListener
在 listeners 中增加 Listener\RedisCommandExecutedListener::class,有助于捕获并记录 Redis 命令执行的详细信息,为调试和审计提供额外的面包屑记录。


69-69: 新增 TracingRedisListener
Tracing\Listener\TracingRedisListener::class 与前者互补,实现对 Redis 命令执行的细粒度追踪和监控,进一步提升可观测性。该组合方案整体设计合理。

@huangdijia huangdijia marked this pull request as ready for review February 9, 2025 10:23
@huangdijia huangdijia merged commit c0a9762 into main Feb 9, 2025
16 checks passed
@huangdijia huangdijia deleted the add-listeners branch February 9, 2025 10:23
huangdijia added a commit that referenced this pull request Feb 9, 2025
…cking (#838)

* feat: add RedisCommandExecutedListener for enhanced Redis command tracking

* feat: replace RedisCommandExecutedListener with TracingRedisListener for improved Redis command tracing

* refactor: improve parameter handling and connection name usage in TracingRedisListener

* feat: add SetRedisEventEnableListener to enable Redis event tracking on application boot

* refactor: simplify constructor syntax in SetRedisEventEnableListener

* fix: correct formatting of Redis command parameters in TracingRedisListener

* refactor: remove unused key variable in TracingRedisListener

* refactor: add duration field to Redis command event data for improved tracking

---------

Co-authored-by: Deeka Wong <[email protected]>
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.

1 participant