-
-
Notifications
You must be signed in to change notification settings - Fork 27
refactor(tracing): streamline transaction context configuration in SpanStarter #902
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
Conversation
…anStarter Replace multiple conditional statements with functional approach using tap, array_walk, and match expressions for cleaner and more maintainable transaction context setup.
Walkthrough重构 SpanStarter 在启动事务时应用选项的方式:通过 continueTrace 的回调以类型化 TransactionContext 批量设置 name/op/description/origin/source,并新增 TransactionContext 引入;默认 source 的设定策略随之改变(不再无条件设为 custom())。 Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller as 调用方
participant SpanStarter as SpanStarter
participant Tracing as Tracing/Hub
participant Ctx as TransactionContext
Caller->>SpanStarter: startTransaction(options)
SpanStarter->>Tracing: continueTrace()
activate Tracing
Tracing-->>SpanStarter: Ctx (可包含既有上下文)
deactivate Tracing
Note over SpanStarter,Ctx: 在回调中按选项批量设置字段<br/>name/op/description/origin/source(带类型检查)
SpanStarter->>Tracing: startTransaction(Ctx after options)
Tracing-->>Caller: Transaction
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 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.1.17)At least one path must be specified to analyse. 📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ 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)
✨ Finishing Touches
🧪 Generate unit tests
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. Comment |
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 (2)
src/sentry/src/Tracing/SpanStarter.php (2)
90-106: 潜在行为变化:未提供 source 时不再显式设置为 custom()当前仅在传入的 source 不是 TransactionSource 实例时回退 custom();若调用方完全未提供 source,将沿用 continueTrace 返回的上下文默认值。请确认这是否与既有行为一致(PR 目标声明“无破坏性变更”)。为避免意外差异,可在未提供 source 时显式设置为 custom()(保留你现在的函数式风格):
tap( continueTrace($sentryTrace, $baggage), function (TransactionContext $context) use ($options) { + if (!array_key_exists('source', $options)) { + $context->setSource(TransactionSource::custom()); + } array_walk($options, function ($value, $key) use ($context) { match ($key) { 'name' => is_string($value) && $context->setName($value), 'op' => is_string($value) && $context->setOp($value), 'description' => is_string($value) && $context->setDescription($value), 'origin' => is_string($value) && $context->setOrigin($value), 'source' => $value instanceof TransactionSource ? $context->setSource($value) : $context->setSource(TransactionSource::custom()), default => null, }; }); } )
94-103: 可读性与健壮性:避免写入空字符串,并简化遍历逻辑(提供两种改法,二选一)
- 现有 is_string(...) && setter(...) 会允许空字符串,可能造成意外的空 name/op/description/origin。
- array_walk + match + “&&” 短路可读性一般,不利于静态分析。
方案 A(保持现有风格,仅增强校验):
- 'name' => is_string($value) && $context->setName($value), - 'op' => is_string($value) && $context->setOp($value), - 'description' => is_string($value) && $context->setDescription($value), - 'origin' => is_string($value) && $context->setOrigin($value), + 'name' => is_string($value) && $value !== '' && $context->setName($value), + 'op' => is_string($value) && $value !== '' && $context->setOp($value), + 'description' => is_string($value) && $value !== '' && $context->setDescription($value), + 'origin' => is_string($value) && $value !== '' && $context->setOrigin($value),方案 B(更直观,便于 IDE/静态分析;同时包含“默认 source=custom()”逻辑):
- array_walk($options, function ($value, $key) use ($context) { - match ($key) { - 'name' => is_string($value) && $context->setName($value), - 'op' => is_string($value) && $context->setOp($value), - 'description' => is_string($value) && $context->setDescription($value), - 'origin' => is_string($value) && $context->setOrigin($value), - 'source' => $value instanceof TransactionSource ? $context->setSource($value) : $context->setSource(TransactionSource::custom()), - default => null, - }; - }); + if (!array_key_exists('source', $options)) { + $context->setSource(TransactionSource::custom()); + } + foreach ($options as $key => $value) { + switch ($key) { + case 'name': + if (is_string($value) && $value !== '') { + $context->setName($value); + } + break; + case 'op': + if (is_string($value) && $value !== '') { + $context->setOp($value); + } + break; + case 'description': + if (is_string($value) && $value !== '') { + $context->setDescription($value); + } + break; + case 'origin': + if (is_string($value) && $value !== '') { + $context->setOrigin($value); + } + break; + case 'source': + if ($value instanceof TransactionSource) { + $context->setSource($value); + } else { + $context->setSource(TransactionSource::custom()); + } + break; + } + }
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/sentry/src/Tracing/SpanStarter.php(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/sentry/src/Tracing/SpanStarter.php (6)
src/sentry/src/Tracing/Listener/TracingAmqpListener.php (1)
startTransaction(64-89)src/sentry/src/Tracing/Listener/TracingAsyncQueueListener.php (1)
startTransaction(64-80)src/sentry/src/Tracing/Listener/TracingCommandListener.php (1)
startTransaction(73-84)src/sentry/src/Tracing/Listener/TracingCrontabListener.php (1)
startTransaction(57-68)src/sentry/src/Tracing/Listener/TracingKafkaListener.php (1)
startTransaction(62-87)src/sentry/src/Tracing/Listener/TracingRequestListener.php (1)
startTransaction(70-146)
⏰ 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). (12)
- 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.1 with Swoole 6.0.2
- GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
- GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
- GitHub Check: Test on PHP 8.3 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.3 with Swoole 5.1.7
- GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
- GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
- GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
🔇 Additional comments (2)
src/sentry/src/Tracing/SpanStarter.php (2)
25-25: 引入 TransactionContext 的 use 声明正确且必要为 tap 回调的强类型提示提供了依赖,避免 FQN,没问题。
94-103: 环境已声明 PHP ≥8.1,支持命名参数与可变参数键名保留
根目录 composer.json 要求"php": ">=8.1",PHP 8.1 完全支持将命名参数收集至...$options时保留键名,因此无需额外处理。
huangdijia
left a comment
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.
代码审查报告
✅ 优点
- 成功将命令式代码重构为函数式风格,提高了代码的可读性和可维护性
- 使用 tap() 和 array_walk() 配合 match 表达式,代码更加简洁
- 正确添加了 TransactionContext 导入
⚠️ 需要关注的问题
1. 逻辑行为变更风险
关键问题: source 字段的处理逻辑发生了变化:
- 原代码: 只有当 source 选项存在且不是 TransactionSource 实例时才设置为 custom()
- 新代码: source 字段总是会被处理,即使选项不存在也会设为 custom()
这可能改变了当 source 选项不存在时的默认行为。
2. 建议的修复方案
建议修改 source 处理逻辑,保持原有行为:
只有当 source 选项真正存在时才进行处理。
📋 建议补充的测试用例
- 测试不传入 source 选项的情况(确保默认行为不变)
- 测试传入无效 source 类型的情况
- 测试传入有效 TransactionSource 实例的情况
- 测试字符串类型选项的边界情况(如 null、空字符串等)
💡 性能优化建议
当前 array_walk() 会遍历所有选项,可以考虑使用 array_intersect_key() 优化,只处理实际存在的选项。
总结
整体是一个很好的重构,主要需要确认 source 字段的行为变更是否符合预期。建议在合并前补充相关测试用例。
…anStarter (#902) * refactor(tracing): streamline transaction context configuration in SpanStarter Replace multiple conditional statements with functional approach using tap, array_walk, and match expressions for cleaner and more maintainable transaction context setup. * fix(tracing): improve source setting logic in SpanStarter trait --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Summary
tap,array_walk, andmatchexpressionsTransactionContextimportChanges
SpanStarter.phpfrom imperative style with multiple if statements to functional styletap()function for cleaner fluent configurationarray_walk()withmatchexpression for option processingTest plan
Summary by CodeRabbit