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 #833

Merged
merged 4 commits into from
Feb 7, 2025

Conversation

huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Feb 6, 2025

Summary by CodeRabbit

  • 新功能
    • 新增 Redis 命令执行事件监听功能,实现对 Redis 操作的实时跟踪和记录。
  • 改进
    • 优化了 Redis 操作记录的判断逻辑,提高了监控数据的准确性。
    • 更新了多个事件监听器的参数类型,增强了错误处理机制。
  • Chores
    • 部分旧监控组件已标记为弃用,并计划在后续版本中移除。

@huangdijia huangdijia requested a review from guandeng February 6, 2025 14:03
Copy link

coderabbitai bot commented Feb 6, 2025

Walkthrough

该更改主要涉及多个文件。首先,在 RedisAspect 类中添加了弃用通知,并调整了 process 方法的条件判断逻辑,引入了对 Hyperf\Redis\Event\CommandExecuted 类的存在性检查。其次,在 ConfigProvider 类中对 listeners 数组进行了调整,包括重新定位 CronEventListener 和新增 RedisCommandExecutedListener。最后,新增了 RedisCommandExecutedListener 类,用于捕获并处理 Redis 命令执行事件,同时执行命令格式化和事件记录。此外,多个监听器类的 process 方法的参数类型被更新,以支持更广泛的事件类型。

Changes

文件路径 变更摘要
src/telescope/src/Aspect/RedisAspect

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


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

🧹 Nitpick comments (1)
src/telescope/src/ConfigProvider.php (1)

49-49: Redis 命令执行监听器的集成符合最佳实践

新增的 RedisCommandExecutedListener 与现有的监听器体系完美融合,扩展了 Telescope 的监控能力。

建议确保在项目文档中添加关于此新功能的使用说明。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c861049 and 8d310ed.

📒 Files selected for processing (3)
  • src/telescope/src/Aspect/RedisAspect.php (2 hunks)
  • src/telescope/src/ConfigProvider.php (1 hunks)
  • src/telescope/src/Listener/RedisCommandExecutedListener.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 (2)
src/telescope/src/Aspect/RedisAspect.php (1)

53-56: 请确认条件逻辑变更的意图

在第53-56行,条件判断增加了 class_exists('Hyperf\Redis\Event\CommandExecuted'),导致当该类存在时,process 方法将直接返回,不再执行后续逻辑。请确认这是否与您的预期一致,以确保在适当的情况下记录 Redis 命令。

src/telescope/src/ConfigProvider.php (1)

44-51: 监听器配置更改看起来合理!

监听器的排序和新增都符合预期:

  • CronEventListener 的位置调整不会影响功能
  • RedisCommandExecutedListener 的添加完善了 Redis 命令执行的监控能力

Comment on lines +92 to +101
try {
$unpacked = $packer->unpack((string) $parameter);
$parameter = match (true) {
is_null($unpacked) => 'null',
is_array($unpacked) => json_encode($unpacked),
default => $unpacked,
};
} catch (Throwable $e) {
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

在 catch 块中处理异常以避免潜在问题

在第92-101行的 catch 块中,捕获的异常被忽略,没有任何处理。这可能会导致潜在的问题被隐藏。建议在 catch 块中记录异常信息或采取适当的处理措施,以提高代码的可维护性和调试能力。

应用以下修改来处理异常:

+use Psr\Log\LoggerInterface;

 class RedisCommandExecutedListener implements ListenerInterface
 {
     public function __construct(
         private ContainerInterface $container,
         private ConfigInterface $config,
         private TelescopeConfig $telescopeConfig,
+        private LoggerInterface $logger,
     ) {
         $this->setRedisEventEnable();
     }

     // ...

     } catch (Throwable $e) {
+        $this->logger->error('解包参数时发生异常', ['exception' => $e]);
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
$unpacked = $packer->unpack((string) $parameter);
$parameter = match (true) {
is_null($unpacked) => 'null',
is_array($unpacked) => json_encode($unpacked),
default => $unpacked,
};
} catch (Throwable $e) {
}
}
<?php
// ... Other imports
use Psr\Log\LoggerInterface;
class RedisCommandExecutedListener implements ListenerInterface
{
public function __construct(
private ContainerInterface $container,
private ConfigInterface $config,
private TelescopeConfig $telescopeConfig,
private LoggerInterface $logger,
) {
$this->setRedisEventEnable();
}
// ... Other methods and code
public function someMethod() // Assuming the try-catch block is inside a method
{
// ... Some preceding code
try {
$unpacked = $packer->unpack((string) $parameter);
$parameter = match (true) {
is_null($unpacked) => 'null',
is_array($unpacked) => json_encode($unpacked),
default => $unpacked,
};
} catch (Throwable $e) {
$this->logger->error('解包参数时发生异常', ['exception' => $e]);
}
// ... Some following code
}
}

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

🔭 Outside diff range comments (1)
src/telescope/src/Listener/DbQueryListener.php (1)

49-50: 移除冗余的类型检查

由于在第42行已经进行了类型检查,这里的 if ($event instanceof QueryExecuted) 检查是冗余的。

建议移除冗余检查,简化代码:

-        if ($event instanceof QueryExecuted) {
-            $sql = $event->sql;
+        $sql = $event->sql;
♻️ Duplicate comments (1)
src/telescope/src/Listener/RedisCommandExecutedListener.php (1)

92-101: ⚠️ Potential issue

需要在 catch 块中处理异常

在 catch 块中忽略异常可能会隐藏潜在问题。建议添加日志记录或适当的异常处理逻辑。

🧹 Nitpick comments (3)
src/telescope/src/Listener/RedisCommandExecutedListener.php (3)

36-41: 建议添加返回值类型提示

为了提高代码的类型安全性,建议为 listen 方法添加返回值类型提示。

-    public function listen(): array
+    public function listen(): array

43-67: 优化参数类型提示以提高代码可读性

虽然 @param 注释中指明了参数类型,但建议直接在参数声明中使用更具体的类型。这样可以提供更好的 IDE 支持和类型安全性。

-    public function process(object $event): void
+    public function process(CommandExecuted $event): void

111-116: 建议优化方法名称以更好地表达其功能

当前方法名称 setRedisEventEnable 不够清晰。建议修改为更具描述性的名称,如 enableRedisEventsForAllConnections

-    private function setRedisEventEnable()
+    private function enableRedisEventsForAllConnections(): void
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8d310ed and 88194e9.

📒 Files selected for processing (5)
  • src/telescope/src/Listener/CommandListener.php (1 hunks)
  • src/telescope/src/Listener/CronEventListener.php (1 hunks)
  • src/telescope/src/Listener/DbQueryListener.php (1 hunks)
  • src/telescope/src/Listener/ExceptionHandlerListener.php (5 hunks)
  • src/telescope/src/Listener/RedisCommandExecutedListener.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 (4)
src/telescope/src/Listener/CronEventListener.php (1)

41-44: 类型检查逻辑优化

条件判断的重构提高了代码的类型安全性,通过显式检查事件类型来确保只处理预期的事件类型。

src/telescope/src/Listener/CommandListener.php (1)

39-46: 类型安全性增强

通过添加实例类型检查和更新参数类型注解,确保了事件处理的类型安全性,同时保持了接口的灵活性。

src/telescope/src/Listener/ExceptionHandlerListener.php (1)

55-57: 代码现代化和类型安全性改进

  1. 使用箭头函数提高了代码的可读性
  2. 添加了 Throwable 类型提示增强了类型安全性
  3. Collection 的使用更加简洁和优雅

Also applies to: 81-84

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

26-34: 类的声明和构造函数实现得当!

构造函数的依赖注入和初始化逻辑清晰,类型提示完整,符合单一职责原则。

@huangdijia huangdijia marked this pull request as ready for review February 7, 2025 02:45
@huangdijia huangdijia merged commit 3e9c2ed into main Feb 7, 2025
16 checks passed
@huangdijia huangdijia deleted the add-redis-command-executed-listener branch February 7, 2025 02:45
huangdijia added a commit that referenced this pull request Feb 7, 2025
* feat: Add RedisCommandExecutedListener

* fix: 更新事件处理逻辑以支持多种事件类型

* fix: 修复 CronEventListener 中的默认输出逻辑

* fix: 优化 RedisCommandExecutedListener 中的事件启用逻辑

---------

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.

2 participants