Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Nov 18, 2025

Summary

This PR enhances the Sentry tracing capabilities for RPC calls by adding socket-level tracing support that captures server address and port information.

Changes

  • SocketFactoryAspect: New aspect that intercepts socket creation to capture socket options
  • SocketOptionContainer: Utility class using WeakMap to store socket options efficiently
  • RpcAspect Enhancement: Modified to extract and include server address and port information in span data
  • ConfigProvider: Registered the new SocketFactoryAspect

Benefits

  • Provides better visibility into RPC calls by capturing target server details
  • Uses WeakMap for memory-efficient storage of socket options
  • Maintains backward compatibility while adding new tracing capabilities
  • Follows existing patterns in the codebase for aspect implementation

Technical Details

The implementation works by:

  1. Intercepting socket creation via SocketFactoryAspect
  2. Storing socket options in a WeakMap-based container
  3. Retrieving socket information during RPC calls in RpcAspect
  4. Adding server.address and server.port to the span data

This enhancement will help developers better understand their RPC call patterns and server connectivity in distributed systems.

Summary by CodeRabbit

发行说明

  • 新增功能

    • 新增 RPC 端点跟踪能力,支持捕获 RPC 端点的主机和端口信息
    • 新增两个配置常量用于 RPC 追踪配置
  • 依赖更新

    • 添加 hyperf/rpc-multiplex 依赖包

@coderabbitai
Copy link

coderabbitai bot commented Nov 18, 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 6 minutes and 44 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 2f6e219 and 7beca13.

📒 Files selected for processing (4)
  • src/sentry/composer.json (1 hunks)
  • src/sentry/src/Constants.php (1 hunks)
  • src/sentry/src/Tracing/Aspect/RpcAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php (1 hunks)

概览

该PR为Sentry添加了RPC端点追踪支持。通过引入新的Hyperf AOP切面来拦截RPC套接字操作,捕获端点主机和端口信息,并将其存储在Sentry追踪上下文中,同时添加了相应的配置和常量定义。

变更

项目 / 文件 变更摘要
依赖管理
composer.json
新增 hyperf/rpc-multiplex 依赖(版本 ~3.1.0)至 require 和 require-dev 部分
追踪常量
src/sentry/src/Constants.php
添加两个新的公开常量:TRACE_RPC_ENDPOINT_HOSTTRACE_RPC_ENDPOINT_PORT,用于存储RPC端点的主机和端口信息
配置注册
src/sentry/src/ConfigProvider.php
在 aspects 数组中注册 Tracing\Aspect\RpcEndpointAspect::class
RPC追踪切面
src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php
新增Hyperf AOP切面类,拦截 Hyperf\RpcMultiplex\SocketFactory::get() 调用,从返回的Socket对象提取端点信息并存储到Sentry追踪上下文

序列图

sequenceDiagram
    participant Client
    participant RpcEndpointAspect
    participant SocketFactory
    participant Context as Sentry Context

    Client->>RpcEndpointAspect: 调用 SocketFactory::get()
    activate RpcEndpointAspect
    RpcEndpointAspect->>SocketFactory: process() 继续原始调用
    activate SocketFactory
    SocketFactory-->>RpcEndpointAspect: 返回 Socket 对象
    deactivate SocketFactory
    
    rect rgb(200, 220, 255)
        Note over RpcEndpointAspect,Context: 追踪信息捕获
        RpcEndpointAspect->>Context: 存储 TRACE_RPC_ENDPOINT_HOST
        RpcEndpointAspect->>Context: 存储 TRACE_RPC_ENDPOINT_PORT
    end
    
    RpcEndpointAspect-->>Client: 返回 Socket
    deactivate RpcEndpointAspect
Loading

代码审查工作量评估

🎯 2 (Simple) | ⏱️ ~10 分钟

  • 关键审查点
    • 验证 RpcEndpointAspect 中的切面拦截逻辑是否正确,特别是 Socket 对象属性的访问方式
    • 确认常量命名与追踪上下文的键名一致
    • 检查依赖版本号是否与项目兼容性要求一致

诗歌

🐰 RPC的秘密我来守护,
端点主机与端口一起捕捉,
切面的魔法轻轻拨动,
Sentry的眼睛看得更清楚,
链路追踪闪闪发光✨

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 PR 标题准确地总结了主要变更:为 RPC 调用添加套接字跟踪支持,这与提交的所有代码更改相符。

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

🧹 Nitpick comments (1)
src/sentry/src/Tracing/Aspect/RpcAspect.php (1)

128-132: 可考虑在存在 socketOptions 时才写入 server.address / server.port

当前使用 nullsafe 操作符,即使 $socketOptionsnull,也会向 span data 中写入 'server.address' => null'server.port' => null。如果希望只在明确解析到目标地址时才打点,可以按条件构建 data 数组,例如:

$data = [
    'rpc.context' => $rpcCtx->getData(),
];

if ($socketOptions) {
    $data['server.address'] = $socketOptions->getHost();
    $data['server.port'] = $socketOptions->getPort();
}

$span->setData($data);

这属于埋点细节的小优化,可根据实际上报习惯自行取舍。

📜 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 5b1d22e and 66fe865.

📒 Files selected for processing (4)
  • src/sentry/src/ConfigProvider.php (1 hunks)
  • src/sentry/src/Tracing/Aspect/RpcAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/SocketFactoryAspect.php (1 hunks)
  • src/sentry/src/Util/SocketOptionContainer.php (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
src/sentry/src/ConfigProvider.php (1)
src/sentry/src/Tracing/Aspect/SocketFactoryAspect.php (1)
  • SocketFactoryAspect (22-39)
src/sentry/src/Tracing/Aspect/RpcAspect.php (2)
src/sentry/src/Util/SocketOptionContainer.php (2)
  • SocketOptionContainer (18-36)
  • get (31-35)
src/sentry/src/Util/Carrier.php (4)
  • get (118-121)
  • has (113-116)
  • Carrier (25-141)
  • fromSpan (55-61)
src/sentry/src/Tracing/Aspect/SocketFactoryAspect.php (1)
src/sentry/src/Util/SocketOptionContainer.php (2)
  • SocketOptionContainer (18-36)
  • set (25-29)
⏰ 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). (16)
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.2
🔇 Additional comments (4)
src/sentry/src/ConfigProvider.php (1)

41-43: 在 ConfigProvider 中注册 SocketFactoryAspect 的方式看起来合理

Tracing\Aspect\SocketFactoryAspect::class 加入 aspects 列表且紧跟在 RpcAspect 之后,符合本次 PR 的目标,也不会影响已有配置逻辑。

src/sentry/src/Tracing/Aspect/RpcAspect.php (1)

14-35: 为 RPC trace 引入 SocketOption 相关依赖的整体方向是合理的

在 RpcAspect 中引入 SocketOptionContainer 以及 SocketOptionInterface / SocketInterface,并保留原有 span 构建逻辑不变,仅附加网络维度数据,改动范围可控且契合“增强追踪而不改变业务语义”的目标。

src/sentry/src/Util/SocketOptionContainer.php (1)

18-35: 基于 WeakMap 的 SocketOptionContainer 实现简洁且符合使用场景

通过静态 WeakMap<SocketInterface, SocketOptionInterface> 关联 socket 与其选项,并在 set/get 中 lazy 初始化,既避免了显式清理,又保持了 API 简单明了;当前实现没有明显逻辑问题。

如果项目后续需要兼容更低版本 PHP,请额外确认运行环境对 WeakMap 的支持情况。

src/sentry/src/Tracing/Aspect/SocketFactoryAspect.php (1)

22-37: SocketFactoryAspect 拦截 socket 创建并记录选项的实现是非侵入且安全的

process() 中复用原有 SocketFactory::make 结果,仅在 $optionsSocketOptionInterface 且返回值为 SocketInterface 时写入 SocketOptionContainer,既保证了类型安全,也不改变工厂方法原有行为,对后续在 RpcAspect 中按 socket 取回选项的设计是个不错的支撑点。

@huangdijia huangdijia force-pushed the feature/sentry-rpc-socket-tracing branch from 9a623fc to e217702 Compare November 19, 2025 00:24
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: 3

🧹 Nitpick comments (1)
src/sentry/src/Tracing/Aspect/RpcSocketFactoryAspect.php (1)

23-28: 考虑设置 aspect 优先级

Aspect 属性未指定优先级。如果有其他 aspect 也拦截 socket 创建,执行顺序可能不确定。建议考虑是否需要设置明确的优先级。

另外,空括号可以省略:#[Aspect] 即可。

-#[Aspect()]
+#[Aspect(priority: 100)]
 class RpcSocketFactoryAspect extends AbstractAspect
📜 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 66fe865 and 7890608.

📒 Files selected for processing (3)
  • composer.json (1 hunks)
  • src/sentry/src/Constants.php (1 hunks)
  • src/sentry/src/Tracing/Aspect/RpcSocketFactoryAspect.php (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Aspect/RpcSocketFactoryAspect.php (1)
src/sentry/src/Constants.php (1)
  • Constants (14-31)
⏰ 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). (18)
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 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.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • 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: Test on PHP 8.2 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
🔇 Additional comments (2)
src/sentry/src/Constants.php (1)

18-20: 常量定义正确

新增的常量遵循现有命名约定,值与常量名称保持一致,将用于存储 RPC 端点的主机和端口信息。

src/sentry/src/Tracing/Aspect/RpcSocketFactoryAspect.php (1)

32-35: 实现与 PR 描述不一致

PR 描述提到使用 WeakMap 和 SocketOptionContainer 工具类来存储 socket 选项,但实际实现直接使用 Context 存储数据。这种不一致可能会造成理解上的困惑。

如果 Context 是有意的设计选择,建议更新 PR 描述以反映实际实现。

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/sentry/src/Tracing/Aspect/RpcEndpointAspect.php (1)

28-28: 建议添加返回类型声明。

process() 方法添加返回类型声明可以提高代码的类型安全性和可读性。

应用此差异添加返回类型:

-    public function process(ProceedingJoinPoint $proceedingJoinPoint)
+    public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
📜 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 7890608 and 2f6e219.

📒 Files selected for processing (2)
  • src/sentry/src/ConfigProvider.php (1 hunks)
  • src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/sentry/src/ConfigProvider.php (1)
src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php (1)
  • RpcEndpointAspect (22-35)
⏰ 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). (16)
  • 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.3 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.1.2
  • 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: Test on PHP 8.2 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.3 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.2
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.8
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 with Swoole 6.1.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.1.2
🔇 Additional comments (5)
src/sentry/src/ConfigProvider.php (1)

42-42: 切面注册正确。

新切面的注册位置合理,与 RpcAspect 相邻,符合模块组织原则。

src/sentry/src/Tracing/Aspect/RpcEndpointAspect.php (4)

12-23: 导入和类声明正确。

所有必要的依赖已正确导入,类结构遵循现有切面实现模式。


24-26: 切面目标配置正确。

正确指定了要拦截的方法,符合 Hyperf AOP 的配置约定。


28-34: PR 描述与实现不一致。

PR 目标中提到使用 WeakMap 和 SocketOptionContainer 来存储 socket 选项,但实际实现使用的是 Context::set()。虽然使用协程上下文存储是合理的做法(数据会在协程结束时自动清理),但这与 PR 描述不符,可能会让未来的维护者感到困惑。

建议更新 PR 描述以反映实际的实现方式,或者如果确实需要 WeakMap 方案,请调整实现。


30-33: 原始审查注释不准确 - 代码类型安全性已验证。

代码实际上是安全的:

  1. SocketFactory::get() 方法有明确的返回类型声明:public function get(): Socket
  2. Socket 类继承自 Client 类(在 vendor/multiplex/socket/src/Client.php 中)
  3. Client 类定义了:
    • public function getName(): string(第 184 行)
    • public function getPort(): int(第 189 行)
  4. tap() 回调中的类型提示 Socket $socket$proceedingJoinPoint->process() 的返回类型相符

代码已通过类型声明进行了充分保护,不存在运行时类型错误的风险。

Likely an incorrect or invalid review comment.

@huangdijia huangdijia merged commit 65f4f1f into main Nov 19, 2025
4 checks passed
@huangdijia huangdijia deleted the feature/sentry-rpc-socket-tracing branch November 19, 2025 00:48
huangdijia added a commit that referenced this pull request Nov 19, 2025
* fix: allow null socket parameter in SocketOptionContainer get method

* Revert "fix: allow null socket parameter in SocketOptionContainer get method"

This reverts commit e217702.

* feat: add RpcSocketFactoryAspect for tracing RPC socket endpoints

* feat: add RpcEndpointAspect for tracing RPC endpoint details and remove RpcSocketFactoryAspect

* feat: enhance RPC tracing by adding server address and port to span data

* feat: add reference link for RPC span semantic conventions in RpcAspect

* feat: update RPC tracing constants to use server address and port

* feat: add requirement for hyperf/rpc-multiplex to enable RPC tracing

* feat: improve type hinting for socket in RpcEndpointAspect process method

---------

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