Skip to content

Conversation

@guandeng
Copy link
Collaborator

@guandeng guandeng commented Dec 13, 2025

Summary by CodeRabbit

  • Bug Fixes
    • 修正网络请求时长计算:支持空值保护并以毫秒向下取整,提升时长精度
    • 简化响应存在性判断,增强响应处理稳定性
    • 优化响应数据存储:响应负载统一保存到响应字段,保留状态码、头部和原因字段,便于后续处理

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 13, 2025

Walkthrough

修改 GuzzleHttpClientAspect::on_stats 中的响应处理:对传输时长做 null 安全并取整;将响应存在性判断从赋值表达式改为布尔检查;通过 getResponsePayload(...) 获取的响应内容现在赋入 content['response'](同时保留状态/头/原因字段)。

Changes

Cohort / File(s) Summary
Guzzle 响应处理更新
src/telescope/src/Aspect/GuzzleHttpClientAspect.php
duration 计算改为 floor(($stats->getTransferTime() ?? 0) * 1000)(null 安全并下取整);响应存在性检查由 if ($response = $stats->getResponse()) 改为 if ($response);使用 getResponsePayload($response, $options) 的结果赋给 content['response'](并继续设置 status/headers/reason 等字段)。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • 单文件修改,改动集中且易于验证
  • 关注点:duration 的 null 与取整行为;getResponsePayload 调用对大/流式响应的处理和返回格式;确保原先对 content['response_payload'] 的依赖已更新或不再需要

Possibly related PRs

Poem

🐰 我是跳跃在代码里的兔子 🥕

时长稳稳落地为整,
响应一并存入新巢里,
小改动,匀整又清晰,
代码树下,轻轻一笑。

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 拉取请求标题清晰准确地概括了主要变更,即优化 GuzzleHttpClientAspect 中的客户端请求记录功能。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

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 b3a90c6 and d80596d.

📒 Files selected for processing (1)
  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.php

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

**/*.php: All PHP files must include declare(strict_types=1) at the top
Use PSR-12 coding standards for PHP code formatting
Use 4-space indentation with short array syntax in PHP code
Use .php-cs-fixer.php configuration for PHP code formatting

All PHP files must include a strict_types declaration at the top

**/*.php: Follow project PHP coding standard enforced by php-cs-fixer with PSR-12 style, 4-space indentation, and short array syntax
Maintain type coverage by updating or adding tests when public APIs change; ensure composer test:types stays green before pushing

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
src/**/src/**/*.php

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

Use namespace pattern FriendsOfHyperf\{ComponentName} for all component classes

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
src/*/src/**/*.php

📄 CodeRabbit inference engine (CLAUDE.md)

src/*/src/**/*.php: Namespace convention for components must follow FriendsOfHyperf{ComponentName}
All code must be coroutine-safe and avoid global state without proper context management, blocking I/O operations, and non-coroutine-safe third-party libraries without wrappers
Use Hyperf's Context API for request-scoped data instead of global state
Follows PSR-12 coding standards and use PHP-CS-Fixer for automatic formatting
Use PHPStan at maximum level for static analysis
Ensure component namespace doesn't conflict with existing Hyperf components or other packages in the ecosystem
Integrate deeply with Hyperf's Dependency Injection container for service registration
Leverage Hyperf's AOP (Aspect-Oriented Programming) for cross-cutting concerns via aspects defined in ConfigProvider
Use Hyperf's Event System to register listeners for framework events in components
All code must support coroutine-based concurrency using Swoole/Swow compatibility

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
src/**/*.php

📄 CodeRabbit inference engine (AGENTS.md)

Each component lives in its own subdirectory with PSR-4 namespaces matching FriendsOfHyperf\*

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
🧠 Learnings (1)
📚 Learning: 2025-12-12T05:33:43.947Z
Learnt from: CR
Repo: friendsofhyperf/components PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-12T05:33:43.947Z
Learning: Applies to src/*/composer.json : Use Carbon for date/time handling and Guzzle for HTTP operations (via hyperf/guzzle) as common dependencies

Applied to files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
🧬 Code graph analysis (1)
src/telescope/src/Aspect/GuzzleHttpClientAspect.php (1)
src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (1)
  • getResponsePayload (150-202)
⏰ 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). (10)
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Seer Code Review
🔇 Additional comments (2)
src/telescope/src/Aspect/GuzzleHttpClientAspect.php (2)

73-73: LGTM!

将响应赋值和条件判断分离是一个很好的重构,提高了代码可读性。由于 $response 已经在第 66 行赋值,这里只需要检查其真实性即可。


71-71: getTransferTime() 返回类型确为 float|null,空值合并运算符是必需的。

根据 Guzzle 官方文档,TransferStats::getTransferTime() 确实可能返回 null。当 HTTP 处理器(如非 cURL 处理器)未提供时间值、请求在完成前失败,或模拟处理器未设置 transfer_time 选项时,都会返回 null。因此,添加空值合并运算符 (?? 0) 和 round() 函数是必要的防御性编程实践,确保持续时间始终是有效的整数毫秒值。此改动正确且合理。

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 (1)
src/telescope/src/Aspect/GuzzleHttpClientAspect.php (1)

113-116: 建议改进流位置处理。

当前实现在 finally 块中仅调用 rewind(),这会将流重置到起始位置,但如果流在读取前已经处于某个位置,则无法恢复到原始位置。参考 src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php 的实现模式,建议保存并恢复原始流位置。

应用以下差异以改进流位置处理:

     $stream = $response->getBody();
+    $pos = null;
 
     try {
         if ($stream->isSeekable()) {
+            $pos = $stream->tell();
             $stream->rewind();
         }
 
         $content = $stream->getContents();
         
         // ... rest of the logic ...
         
     } catch (Throwable $e) {
         return 'Purged By Hyperf Telescope: ' . $e->getMessage();
     } finally {
-        $stream->isSeekable() && $stream->rewind();
+        if ($pos !== null) {
+            try {
+                $stream->seek($pos);
+            } catch (Throwable) {
+                // ignore: must not break the request flow
+            }
+        }
     }

Also applies to: 148-148

📜 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 c1318b7 and e8e484e.

📒 Files selected for processing (1)
  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.php

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

**/*.php: All PHP files must include declare(strict_types=1) at the top
Use PSR-12 coding standards for PHP code formatting
Use 4-space indentation with short array syntax in PHP code
Use .php-cs-fixer.php configuration for PHP code formatting

All PHP files must include a strict_types declaration at the top

**/*.php: Follow project PHP coding standard enforced by php-cs-fixer with PSR-12 style, 4-space indentation, and short array syntax
Maintain type coverage by updating or adding tests when public APIs change; ensure composer test:types stays green before pushing

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
src/**/src/**/*.php

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

Use namespace pattern FriendsOfHyperf\{ComponentName} for all component classes

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
src/*/src/**/*.php

📄 CodeRabbit inference engine (CLAUDE.md)

src/*/src/**/*.php: Namespace convention for components must follow FriendsOfHyperf{ComponentName}
All code must be coroutine-safe and avoid global state without proper context management, blocking I/O operations, and non-coroutine-safe third-party libraries without wrappers
Use Hyperf's Context API for request-scoped data instead of global state
Follows PSR-12 coding standards and use PHP-CS-Fixer for automatic formatting
Use PHPStan at maximum level for static analysis
Ensure component namespace doesn't conflict with existing Hyperf components or other packages in the ecosystem
Integrate deeply with Hyperf's Dependency Injection container for service registration
Leverage Hyperf's AOP (Aspect-Oriented Programming) for cross-cutting concerns via aspects defined in ConfigProvider
Use Hyperf's Event System to register listeners for framework events in components
All code must support coroutine-based concurrency using Swoole/Swow compatibility

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
src/**/*.php

📄 CodeRabbit inference engine (AGENTS.md)

Each component lives in its own subdirectory with PSR-4 namespaces matching FriendsOfHyperf\*

Files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
🧠 Learnings (1)
📚 Learning: 2025-12-12T05:33:43.947Z
Learnt from: CR
Repo: friendsofhyperf/components PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-12T05:33:43.947Z
Learning: Applies to src/*/composer.json : Use Carbon for date/time handling and Guzzle for HTTP operations (via hyperf/guzzle) as common dependencies

Applied to files:

  • src/telescope/src/Aspect/GuzzleHttpClientAspect.php
🧬 Code graph analysis (1)
src/telescope/src/Aspect/GuzzleHttpClientAspect.php (1)
src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (1)
  • getResponsePayload (150-202)
⏰ 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). (9)
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.3
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
🔇 Additional comments (3)
src/telescope/src/Aspect/GuzzleHttpClientAspect.php (3)

71-71: 持续时间计算改进合理。

添加空值安全检查(?? 0)并使用 floor() 取整是良好的防御性编程实践,确保了即使传输时间为 null 时也能返回有效的整数值。


73-73: 响应检查简化正确。

由于 $response 已在第 66 行赋值,将条件从赋值表达式改为简单的布尔检查避免了冗余的 getResponse() 调用,代码更清晰。


77-77: 移除冗余的 response_payload 字段已完成验证。

代码库中不存在对 response_payload 字段的任何引用。$content['response'] 字段由 getResponsePayload() 方法正确赋值,该方法已妥善处理流响应和二进制内容的情况。此更改符合先前的审查意见,无须进一步调整。

@huangdijia huangdijia merged commit 364db04 into friendsofhyperf:main Dec 14, 2025
12 checks passed
huangdijia pushed a commit that referenced this pull request Dec 14, 2025
…ording (#1044)

* optimize(telescope): refine GuzzleHttpClientAspect client request recording

* change duration calculation to use floor instead of round in GuzzleHttpClientAspect

* refactor(telescope): streamline response handling in GuzzleHttpClientAspect
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