-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Conversation
Caution Review failedThe pull request is closed. Walkthrough该 PR 更新了 Redis 相关的代码逻辑:在 RedisAspect 类中增加了对 CommandExecuted 类的检查,并添加了弃用标识,调整了 process 方法的控制流;同时新增了两个监听器类,分别为 RedisCommandExecutedListener(用于面包屑记录)和 TracingRedisListener(用于 Redis 命令的追踪),并在 ConfigProvider 中注册了这两类监听器。 Changes
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
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
Possibly related PRs
Suggested reviewers
Poem
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 detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (4)
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this 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 操作时都收集了大量连接池相关的指标。建议考虑以下优化:
- 可以将部分非关键指标(如 idle_time、pool 相关指标)设置为可配置项
- 考虑增加采样率配置,避免在高频操作场景下产生过多追踪数据
76-78
: 考虑启用被注释的追踪信息被注释的代码包含了更详细的操作描述格式,建议考虑通过配置项来控制是否启用这些额外的追踪信息,以便在需要详细调试时使用。
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 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
: 优化条件判断逻辑新增的条件判断确保了在以下情况下会提前返回:
- 当 CommandExecuted 事件类存在时,避免重复追踪
- 当 Redis 追踪功能未启用时,减少不必要的处理
这种处理方式有助于平滑过渡到新的事件监听器架构。
…for improved Redis command tracing
There was a problem hiding this 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
📒 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 命令执行的细粒度追踪和监控,进一步提升可观测性。该组合方案整体设计合理。
…cingRedisListener
…on application boot
…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]>
…
Summary by CodeRabbit
新功能
Chores