Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Sep 4, 2025

Summary

  • Add content-type detection to only read textual responses
  • Limit binary/non-textual content to '[binary omitted]' to prevent memory issues
  • Set 8KB limit for response body content reading to avoid performance impact
  • Use Guzzle's Utils::copyToString for safer stream handling

Test plan

  • Test with textual responses (JSON, XML, HTML, plain text)
  • Test with binary responses (images, PDFs, etc.)
  • Verify memory usage with large response bodies
  • Confirm tracing data accuracy

Summary by CodeRabbit

  • 新功能

  • Bug Fixes

    • 按内容类型捕获HTTP响应体:仅记录文本内容(如JSON/XML/表单)且最多8KB,二进制以“已省略”表示,避免日志膨胀与乱码。
    • 继续遵循响应体捕获配置,并在可回绕时复位流,减少对后续处理的影响。
  • Refactor

    • 优化响应体读取逻辑以提升稳定性与性能。

…tpClientAspect

- Add content-type detection to only read textual responses
- Limit binary/non-textual content to '[binary omitted]' to prevent memory issues
- Set 8KB limit for response body content reading to avoid performance impact
- Use Guzzle's Utils::copyToString for safer stream handling
@coderabbitai
Copy link

coderabbitai bot commented Sep 4, 2025

Warning

Rate limit exceeded

@huangdijia has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 3 minutes and 27 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between f7f7916 and be31ce8.

📒 Files selected for processing (2)
  • src/sentry/src/Aspect/GuzzleHttpClientAspect.php (1 hunks)
  • src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (1 hunks)

Walkthrough

在 GuzzleHttpClientAspect 中调整响应体抓取逻辑:依据 Content-Type 仅截取最多 8KB 的文本类响应体(text/*、application/json|xml|x-www-form-urlencoded),否则记录 "[binary omitted]";保留原有是否捕获 body 的开关与可回绕流的 rewind 行为。

Changes

Cohort / File(s) Change Summary
HTTP 响应体截取策略更新
src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php
引入基于 Content-Type 的文本判定;使用 GuzzleHttp\Psr7\Utils::copyToString 限制读取至 8KB;非文本标记为 "[binary omitted]";读取后在可 seek 情况下 rewind;保持原有捕获开关逻辑不变。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App as Application
  participant Guzzle as Guzzle Client
  participant Aspect as GuzzleHttpClientAspect
  participant Resp as HTTP Response

  App->>Guzzle: send(request)
  Guzzle->>Resp: execute
  Guzzle-->>App: response
  App->>Aspect: onResponse(response)
  Aspect->>Resp: getHeader("Content-Type")
  alt capture enabled
    alt Content-Type 为文本类
      Aspect->>Resp: copyToString(max 8KB)
      Aspect->>Aspect: store body snippet
      opt 可回绕
        Aspect->>Resp: rewind()
      end
    else 非文本/未知
      Aspect->>Aspect: store "[binary omitted]"
    end
  else 未启用捕获
    Aspect->>Aspect: skip body capture
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

小兔敲键月色温,
细嗅报文辨其真。
文本八千轻裁剪,
二进制就免沾唇。
耳动一拨流回卷,
日志清爽跃屏尘。

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/guzzle-response-body-tracing-optimization

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
🪧 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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 (2)
src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (2)

129-129: 放宽文本类型判定:支持 +json/+xml、NDJSON、GraphQL、SVG 等

很多文本响应使用结构化语法后缀(如 application/problem+jsonapplication/vnd.api+jsonimage/svg+xml),或常见的 application/x-ndjsonapplication/graphql。当前正则会误判为二进制。建议扩展匹配:

-                    $isTextual = \preg_match('/^(text\/|application\/(json|xml|x-www-form-urlencoded))/i', $contentType) === 1;
+                    $isTextual = \preg_match(
+                        '/^(?:text\/|application\/(?:json|xml|x-www-form-urlencoded|[\w.+-]+\+json|[\w.+-]+\+xml|javascript|graphql|x-ndjson)|image\/svg\+xml)/i',
+                        $contentType
+                    ) === 1;

131-131: 将 8KB 魔数提取为类常量/可配置项

便于后续统一调整与 A/B 验证。

在类中添加常量(示例放置于 use SpanStarter; 之后):

 class GuzzleHttpClientAspect extends AbstractAspect
 {
     use SpanStarter;
 
+    private const RESPONSE_BODY_PREVIEW_LIMIT = 8192; // 8KB

并在读取处使用该常量(已在上一个 diff 中示例)。

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 5b5ad83 and f7f7916.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (1 hunks)
⏰ 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.2 with Swoole 5.1.7
  • 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.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
  • 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.2 with Swoole 5.1.7
  • GitHub Check: git-mirror
🔇 Additional comments (1)
src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (1)

131-133: 请确认项目中未覆盖 Guzzle 7.x 默认的 decode_content 选项
Guzzle 7.x 默认 decode_content=true,会自动解压 gzip/deflate 响应,仅在显式设置为 false 时才会保留压缩字节流。
如无覆盖默认值,可关闭此检查;若项目中有禁用自动解码的场景,建议在无解码时统一使用 "[compressed omitted]" 防止误导。

@huangdijia huangdijia merged commit b9bc273 into main Sep 4, 2025
16 checks passed
@huangdijia huangdijia deleted the fix/guzzle-response-body-tracing-optimization branch September 4, 2025 12:46
huangdijia added a commit that referenced this pull request Sep 4, 2025
…tpClientAspect (#900)

* fix(tracing): optimize HTTP response body content reading in GuzzleHttpClientAspect

- Add content-type detection to only read textual responses
- Limit binary/non-textual content to '[binary omitted]' to prevent memory issues
- Set 8KB limit for response body content reading to avoid performance impact
- Use Guzzle's Utils::copyToString for safer stream handling

* fix(tracing): improve handling of HTTP response body content in GuzzleHttpClientAspect

* fix(tracing): standardize request and response data keys in GuzzleHttpClientAspect

* fix(tracing): add HTTP response body size to breadcrumb data in GuzzleHttpClientAspect

* fix(tracing): add comment to clarify on_stats option override in GuzzleHttpClientAspect

* fix(tracing): add additional response header fields for content length in GuzzleHttpClientAspect

* fix(tracing): improve handling of HTTP response body content in GuzzleHttpClientAspect

* fix(tracing): preserve position of response body stream in GuzzleHttpClientAspect

---------

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