Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Oct 13, 2025

Summary

This PR enhances the Sentry integration's console command tracing to support nested spans, enabling better trace continuity when commands are executed within an existing trace context.

Changes

  • Nested span support: Console commands now check if they're being executed within an existing span. If so, they create a child span instead of always starting a new transaction
  • Improved span management: Properly save and restore parent span context after command execution in coroutine environments
  • Unified terminology: Refactored code to consistently use span/getSpan() instead of mixing transaction/getTransaction() references

Benefits

  • Better trace continuity when commands are part of a larger trace (e.g., commands triggered by HTTP requests, queued jobs, or other traced operations)
  • Maintains backward compatibility for standalone command execution
  • More accurate distributed tracing across different execution contexts

Technical Details

The implementation checks for an active span before creating a transaction:

  • If getCurrentHub()->getSpan() returns null → creates a new transaction (existing behavior)
  • If a span exists → creates a child span with proper parent-child relationship

In coroutine contexts, the parent span is properly restored after the command span finishes to maintain correct span hierarchy.

Test Plan

  • Verify standalone console commands still create transactions correctly
  • Verify console commands executed within a traced context create child spans
  • Verify span hierarchy is maintained in coroutine environments
  • Verify exit codes and status are properly tracked

Summary by CodeRabbit

  • 新增/改进

    • 将命令与协程的追踪从事务模型切换为以 span 为核心的父子生命周期,改进跨上下文的关联与隔离。
    • 新增每协程上下文容器用于保存与恢复父 span,增强跨协程的上下文传递与作用域管理。
  • Bug 修复

    • 修正命令/协程结束时的 span 完成顺序、退出码与状态标注,以及作用域恢复与事件刷新流程,确保追踪数据一致性。

This change enables console commands to create child spans when executed
within an existing trace, rather than always creating a new transaction.

Key improvements:
- Check for existing span before creating transaction
- Create child span if parent span exists
- Properly restore parent span context after command execution
- Unified span terminology (replaced transaction-specific references)

This provides better trace continuity when commands are executed as part
of a larger trace context, while maintaining backward compatibility for
standalone command execution.
@coderabbitai
Copy link

coderabbitai bot commented Oct 13, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

将命令与协程追踪从以 Transaction 为中心切换为基于当前 Span 的管理:启动时创建根或 child Span、pushScope 并通过 CoContainer 保存父 span;结束时设置状态/标签、finish、flush 并恢复父 span,移除基于 defer 的事务完成逻辑。(≤50字)

Changes

Cohort / File(s) Summary
事件监听:Transaction -> Span
src/sentry/src/Tracing/Listener/EventHandleListener.php
将命令生命周期由 transaction-centric 改为 span-centric:启动时根据是否存在父 span 创建根或 child SpanpushScope,使用 CoContainer 保存/恢复父 span;结束时读取当前 span、设置 exit code/status/tags、finishflush 并恢复父 span;移除原先基于 defer 的 finish/restore 逻辑。
协程切面:scope/span 驱动 tracing
src/sentry/src/Tracing/Aspect/CoroutineAspect.php
协程追踪改为基于 scope 中当前 span 的 trace 封装:在有活动 span 且存在调用函数时,通过 trace 发出 coroutine.create/coroutine.execute spans 并在新的协程上下文中初始化 Sentry;移除原先手动 transaction/子 span 绑定与生命周期管理,调整上下文绑定与 imports。
新增协程上下文容器
src/sentry/src/Util/CoContainer.php
新增 FriendsOfHyperf\Sentry\Util\CoContainer:在 Hyperf Context 中按上下文保存 WeakMap,提供 set/get/pull/del 接口,用于以对象键保存并临时恢复父 span(跨 scope/协程恢复父级 span)。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller as 调用方(命令 / 协程创建)
  participant Listener as EventHandleListener / CoroutineAspect
  participant Hub as Sentry Hub / Scope
  participant ParentSpan as 当前父 Span
  participant Co as CoContainer

  Caller->>Listener: start event / create coroutine
  alt 无当前 span
    Listener->>Hub: start root span, pushScope
    Note right of Hub #D6EAF8: root span + 新 scope
  else 有当前 span
    Listener->>ParentSpan: start child span
    Listener->>Co: set(调用对象, ParentSpan)
    Listener->>Hub: pushScope 并将 child span 设为当前
    Note right of Hub #F9E79F: child span + 新 scope
  end

  Note over Caller,Listener: 执行业务/协程主体(可能产生更多子 span)

  Caller->>Listener: finish event / 协程结束
  Listener->>Hub: 设置 exit code/status/tags(若采样)
  Listener->>Hub: finish 当前 span
  Listener->>Hub: flush events
  Listener->>Hub: popScope(恢复先前 scope)
  Listener->>Co: pull(调用对象) -> 恢复父 span 到 scope/hub
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • xuanyanwow

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 标题准确简洁地概述了本次变更的核心内容,明确指出在 Sentry 集成中为控制台命令新增对嵌套 Span 的支持,与 PR 的实际改动完全一致且易于团队成员快速理解。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/sentry-console-command-nested-spans

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

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 c1f5c11 and 90bde50.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (4)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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). (8)
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • 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.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • 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.3 with Swoole 5.1.7

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 e45ec6a and c22cac4.

📒 Files selected for processing (2)
  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php (4 hunks)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
🧬 Code graph analysis (2)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (4)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Tracing/SpanStarter.php (2)
  • startTransaction (39-44)
  • continueTrace (114-143)
src/sentry/src/Tracing/Listener/EventHandleListener.php (4)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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.1 with Swoole 5.1.7
  • 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.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • 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.2 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: Test on PHP 8.3 with Swoole 6.0.2

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

📜 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 c22cac4 and 6403027.

📒 Files selected for processing (2)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (3 hunks)
  • src/sentry/src/Util/CoContainer.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
  • src/sentry/src/Util/CoContainer.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
  • src/sentry/src/Util/CoContainer.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (5)
src/sentry/src/Util/CoContainer.php (3)
  • CoContainer (21-50)
  • set (39-44)
  • get (29-32)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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.1 with Swoole 6.0.2
  • 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.2 with Swoole 5.1.7
  • 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.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.1 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 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2

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

📜 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 6403027 and 4d38552.

📒 Files selected for processing (2)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (3 hunks)
  • src/sentry/src/Util/CoContainer.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
  • src/sentry/src/Util/CoContainer.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
  • src/sentry/src/Util/CoContainer.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (4)
src/sentry/src/Util/CoContainer.php (3)
  • CoContainer (21-70)
  • set (30-35)
  • pull (52-64)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
⏰ 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.1 with Swoole 5.1.7
  • 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.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 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 5.1.7
  • 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.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

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 6403027 and a822835.

📒 Files selected for processing (2)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (3 hunks)
  • src/sentry/src/Util/CoContainer.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Util/CoContainer.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Util/CoContainer.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (5)
src/sentry/src/Util/CoContainer.php (3)
  • CoContainer (21-70)
  • set (30-35)
  • pull (52-64)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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). (11)
  • 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.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 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 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7

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

♻️ Duplicate comments (1)
src/sentry/src/Util/CoContainer.php (1)

48-64: 修正 pull 的参数类型以避免运行时错误

WeakMap 的键必须是对象,但当前签名 pull(?object $key = null) 允许调用方省略参数或传入 null。一旦发生这种调用,isset($container[$key]) 将直接抛出 TypeError: WeakMap key must be object,导致运行时崩溃。此外,可空签名也违背了 PHPDoc 中 @param TKey $key 的约束(TKey 被限定为 object)。

应用以下修复移除可空签名和默认值,强制调用方传入有效的对象键:

     /**
      * @param TKey $key
      * @return null|TValue
      */
-    public static function pull(?object $key = null): ?object
+    public static function pull(object $key): ?object
     {
         $container = self::getContainer();
 
         if (! isset($container[$key])) {
             return null;
         }
 
         $value = $container[$key];
         unset($container[$key]);
 
         return $value;
     }
📜 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 4d38552 and bd15825.

📒 Files selected for processing (2)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (3 hunks)
  • src/sentry/src/Util/CoContainer.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Util/CoContainer.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Util/CoContainer.php
  • src/sentry/src/Tracing/Listener/EventHandleListener.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (4)
src/sentry/src/Util/CoContainer.php (3)
  • CoContainer (21-75)
  • set (30-35)
  • pull (52-64)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
⏰ 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.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: Test on PHP 8.3 with Swoole 6.0.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 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 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 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
🔇 Additional comments (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (1)

370-420: LGTM!

命令启动处理逻辑清晰:无父 span 时创建根事务,有父 span 时推入新作用域并创建子 span。代码正确地在存在父 span 但未采样时提前返回(line 399-401),避免不必要的 span 创建。父 span 通过 CoContainer 保存以便后续恢复(line 418)。

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 472d6b3 and 2d62130.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (4)
src/sentry/src/Tracing/Tracer.php (2)
  • trace (70-93)
  • startTransaction (32-59)
src/sentry/src/Function.php (2)
  • trace (48-53)
  • startTransaction (32-37)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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 6.0.2
  • 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.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7

@huangdijia huangdijia requested a review from Copilot October 13, 2025 06:19
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the Sentry integration's console command tracing to support nested spans, enabling better trace continuity when commands are executed within an existing trace context. The changes move from a transaction-only model to a span-centric approach that creates proper parent-child relationships.

Key Changes:

  • Nested span support: Console commands now detect existing spans and create child spans instead of always starting new transactions
  • Context management: Added CoContainer utility for managing parent span references across coroutine boundaries
  • Unified span terminology: Refactored code to consistently use span/getSpan() instead of mixing transaction references

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/sentry/src/Util/CoContainer.php New utility class providing WeakMap-based context storage for managing parent spans in coroutine environments
src/sentry/src/Tracing/Listener/EventHandleListener.php Enhanced command tracing to support nested spans with proper parent-child relationships and context restoration
src/sentry/src/Tracing/Aspect/CoroutineAspect.php Refactored coroutine tracing to use span-centric approach with simplified execution flow

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 9c1fbff and f4c0470.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (5)
src/sentry/src/Tracing/Tracer.php (2)
  • trace (70-93)
  • startTransaction (32-59)
src/sentry/src/Function.php (2)
  • trace (48-53)
  • startTransaction (32-37)
src/sentry/src/Tracing/SpanStarter.php (3)
  • trace (57-62)
  • startTransaction (39-44)
  • continueTrace (114-143)
src/sentry/class_map/SentrySdk.php (3)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
  • init (42-45)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • 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.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.3 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 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 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2

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/CoroutineAspect.php (1)

63-95: 考虑改进变量命名和代码组织

当前的嵌套事务创建逻辑在功能上是正确的,使用 continueTrace() 来维护跨协程的分布式追踪链路。不过,可以考虑以下改进:

  1. 变量命名:第 75 行的 $transaction 可以重命名为 $coroutineTransaction 以更清楚地表明它是子协程中的新事务,区别于可能存在的父 span 上下文。

  2. 代码组织:第 63-95 行的 callable 包装逻辑较为复杂,包含了上下文传播、事务创建、defer 清理和内层 trace 调用。可以考虑将这部分逻辑提取到一个独立的辅助方法中,例如 wrapCallableForCoroutineTracing(),以提高可读性和可维护性。

📜 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 f4c0470 and 3c0c510.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (4)
src/sentry/src/Tracing/Tracer.php (2)
  • startTransaction (32-59)
  • trace (70-93)
src/sentry/src/Function.php (2)
  • startTransaction (32-37)
  • trace (48-53)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • init (42-45)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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.3 with Swoole 6.0.2
  • 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 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: 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.1 with Swoole 6.0.2
🔇 Additional comments (2)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (2)

21-21: LGTM!

导入的 Scopetrace 函数在重构后的代码中正确使用。

Also applies to: 25-25


88-94: LGTM!

内层 trace() 调用正确地将 callable 包装在不接受参数的闭包中(第 89 行),避免了之前版本中的 ArgumentCountError 问题。这为协程的实际执行创建了一个 coroutine.execute 操作的子 span。

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

♻️ Duplicate comments (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (1)

423-453: 修复清理与收尾:避免提前返回导致 Scope 泄漏,并在根场景正确结束事务

  • 提前在 Line 425 返回会跳过 finally,导致已 pushScope() 的子作用域不被 pop,父级上下文不恢复,且可能遗留未完成的事务。
  • 根命令路径中创建的事务未在 AfterExecute 被结束,导致事务悬挂与数据不完整。
  • 仅在拉取到父级时 popScope() 会遗漏根场景中为子 span 推入的作用域,造成泄漏。

修复要点:

  • 不在采样判断处提前返回;将采样布尔值缓存为 $sampled,仅在采样为真时设置标签/状态并 finish()flush()
  • 无论是否嵌套,均在采样为真时 popScope()(因为子 span 只会在采样时创建/设置到 scope)。
  • 当无父级 span 被拉取时(根场景),在子 span 完成后结束当前事务。

应用如下修改:

-        $span = SentrySdk::getCurrentHub()->getSpan();
-
-        if (! $span?->getSampled()) {
-            return;
-        }
-
-        $command = $event->getCommand();
+        $span = SentrySdk::getCurrentHub()->getSpan();
+        if (! $span instanceof Span) {
+            return;
+        }
+        $command = $event->getCommand();
+        $sampled = $span->getSampled();
 
         try {
-            /** @var int $exitCode */
-            $exitCode = (fn () => $this->exitCode ?? SymfonyCommand::SUCCESS)->call($command);
-
-            $span->setTags([
-                'command.exit_code' => (string) $exitCode,
-            ])->setStatus(
-                $event->getThrowable() || $exitCode !== SymfonyCommand::SUCCESS
-                    ? SpanStatus::internalError()
-                    : SpanStatus::ok()
-            );
+            if ($sampled) {
+                /** @var int $exitCode */
+                $exitCode = (fn () => $this->exitCode ?? SymfonyCommand::SUCCESS)->call($command);
+
+                $span->setTags([
+                    'command.exit_code' => (string) $exitCode,
+                ])->setStatus(
+                    $event->getThrowable() || $exitCode !== SymfonyCommand::SUCCESS
+                        ? SpanStatus::internalError()
+                        : SpanStatus::ok()
+                );
+            }
         } finally {
-            $span->finish();
-
-            Integration::flushEvents();
-
-            $parentSpan = CoContainer::pull($command);
+            if ($sampled) {
+                $span->finish();
+            }
+
+            $parentSpan = CoContainer::pull($command);
 
-            if ($parentSpan instanceof Span) {
-                SentrySdk::getCurrentHub()->setSpan($parentSpan);
-                SentrySdk::getCurrentHub()->popScope();
-            }
+            if ($parentSpan instanceof Span) {
+                // 嵌套:恢复父级上下文
+                SentrySdk::getCurrentHub()->setSpan($parentSpan);
+            } else {
+                // 根:结束事务
+                if ($sampled) {
+                    $transaction = SentrySdk::getCurrentHub()->getTransaction();
+                    if ($transaction) {
+                        $transaction->finish();
+                    }
+                }
+            }
+
+            if ($sampled) {
+                // 始终弹出为子 span 推入的作用域(仅在采样为真时创建)
+                SentrySdk::getCurrentHub()->popScope();
+                Integration::flushEvents();
+            }
         }
📜 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 3c0c510 and f91fa26.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (5)
src/sentry/src/Util/CoContainer.php (3)
  • CoContainer (21-75)
  • set (30-35)
  • pull (52-64)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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). (11)
  • GitHub Check: Test on PHP 8.1 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: Test on PHP 8.3 with Swoole 6.0.2
  • 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.1 with Swoole 5.1.7
  • 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.2 with Swoole 5.1.7

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

📜 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 f91fa26 and 306fd08.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (5)
src/sentry/src/Util/CoContainer.php (3)
  • CoContainer (21-75)
  • set (30-35)
  • pull (52-64)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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.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: 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.2 with Swoole 6.0.2
  • 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.3 with Swoole 5.1.7

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

♻️ Duplicate comments (2)
src/sentry/src/Tracing/Listener/EventHandleListener.php (2)

457-457: 移除对父级 span 的 finish() 调用

第 457 行错误地终止了父级 span。父级 span 应在命令执行完毕后继续存在,以便后续操作使用。只有当前命令创建的子 span(第 450 行)应该被 finish。

应用以下修复:

         $parentSpan = CoContainer::pull($command);
 
         if ($parentSpan instanceof Span) {
-            $parentSpan->finish();
             SentrySdk::getCurrentHub()->setSpan($parentSpan);
             SentrySdk::getCurrentHub()->popScope();
         }

381-420: 根命令与嵌套命令的处理逻辑存在严重错误

当前实现存在两个关键问题:

  1. 根命令创建了多余的子 span 层级

    • 第 381-398 行:无父 span 时创建根事务并存储
    • 第 406-419 行:无条件地从该事务创建子 span
    • 问题:根命令不应创建额外的子 span,根事务本身就是应被追踪的 span
  2. 嵌套命令的父 span 未被存储

    • getSpan() 返回非空时(嵌套场景),第 381 行条件为 false
    • 第 398 行的 CoContainer::set() 位于 if 块内,不会执行
    • 后果handleCommandFinished 中第 454 行的 pull 将返回 null,无法恢复父 span,导致作用域泄漏且链路断裂

正确的逻辑应该是:

-        if (! $parentSpan = SentrySdk::getCurrentHub()->getSpan()) {
-            $parentSpan = startTransaction(
+        $parentSpan = SentrySdk::getCurrentHub()->getSpan();
+
+        if ($parentSpan === null) {
+            // 根命令:创建根事务并直接返回
+            startTransaction(
                 TransactionContext::make()
                     ->setName($command->getName() ?: '<unnamed command>')
                     ->setOp('console.command')
                     ->setDescription($command->getDescription())
                     ->setOrigin('auto.console')
                     ->setData([
                         'command.arguments' => (fn () => $this->input->getArguments())->call($command),
                         'command.options' => (fn () => $this->input->getOptions())->call($command),
                     ])
                     ->setTags([
                         'command.name' => $command->getName(),
                     ])
                     ->setSource(TransactionSource::task())
             );
-
-            CoContainer::set($command, $parentSpan);
+            return;
         }
 
+        // 嵌套命令:检查采样并存储父 span
         if (! $parentSpan->getSampled()) {
-            CoContainer::del($command);
             return;
         }
 
+        CoContainer::set($command, $parentSpan);
+
         $scope = SentrySdk::getCurrentHub()->pushScope();
         $span = $parentSpan->startChild(
             SpanContext::make()
                 ->setOp('console.command.execute')
                 ->setDescription($command->getName() ?: $command->getDescription())
                 ->setData([
                     'command.arguments' => (fn () => $this->input->getArguments())->call($command),
                     'command.options' => (fn () => $this->input->getOptions())->call($command),
                 ])
                 ->setTags([
                     'command.name' => $command->getName(),
                 ])
         );
         $scope->setSpan($span);
📜 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 a831503 and 386369a.

📒 Files selected for processing (1)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/*/src/**/*.php

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

All component PHP code must use the namespace pattern FriendsOfHyperf{ComponentName}

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
**/*.php

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

**/*.php: Code style must follow PSR-12 and be formatted by PHP-CS-Fixer per .php-cs-fixer.php
Run PHPStan and keep the codebase passing per phpstan.neon.dist

Files:

  • src/sentry/src/Tracing/Listener/EventHandleListener.php
🧬 Code graph analysis (1)
src/sentry/src/Tracing/Listener/EventHandleListener.php (5)
src/sentry/src/Util/CoContainer.php (4)
  • CoContainer (21-75)
  • set (30-35)
  • del (66-69)
  • pull (52-64)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Tracer.php (1)
  • startTransaction (32-59)
src/sentry/src/Function.php (1)
  • startTransaction (32-37)
src/sentry/src/Integration.php (2)
  • Integration (28-158)
  • flushEvents (106-115)
⏰ 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 6.0.2
  • GitHub Check: Test on PHP 8.1 with Swoole 6.0.2
  • GitHub Check: Test on PHP 8.2 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.3 with Swoole 6.0.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.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7

@huangdijia huangdijia merged commit fcc19b1 into main Oct 13, 2025
16 checks passed
@huangdijia huangdijia deleted the feat/sentry-console-command-nested-spans branch October 13, 2025 08:14
huangdijia added a commit that referenced this pull request Oct 13, 2025
* feat(sentry): support nested spans for console commands

This change enables console commands to create child spans when executed
within an existing trace, rather than always creating a new transaction.

Key improvements:
- Check for existing span before creating transaction
- Create child span if parent span exists
- Properly restore parent span context after command execution
- Unified span terminology (replaced transaction-specific references)

This provides better trace continuity when commands are executed as part
of a larger trace context, while maintaining backward compatibility for
standalone command execution.

* fix(sentry): improve scope management for console command spans

* fix(sentry): update coroutine tracing to use spans instead of transactions

* fix(sentry): update span handling in CoroutineAspect and EventHandleListener for improved tracing

* fix(sentry): refactor span handling in CoroutineAspect for improved clarity and consistency

* fix(sentry): clarify purpose of commented span handling in EventHandleListener

* feat(sentry): add CoContainer for managing Sentry context in a weak reference container

* fix(sentry): refactor CoContainer method to improve span retrieval logic

* fix(sentry): refactor CoContainer methods for improved clarity and organization

* fix(sentry): add sampling check for parent span in EventHandleListener

* fix(sentry): add del method to CoContainer for removing items from the container

* fix(sentry): update getContainer method to use getOrSet for better context management

* fix(sentry): refactor coroutine execution to use trace function for better span management

* fix(sentry): adjust scope management in EventHandleListener for improved span handling

* fix(sentry): ensure parent span is restored after coroutine processing

* fix(sentry): streamline coroutine span creation by removing unnecessary scope management

* fix(sentry): refactor callable in trace function to use static closure for coroutine execution

* fix(CoContainer): update pull method to require a non-null object key

* fix(EventHandleListener): streamline span tag and status setting for improved readability

* fix(EventHandleListener): remove unnecessary parent span sampling check for streamlined processing

* fix(CoroutineAspect): initialize Sentry SDK before transferring context to new coroutine

* fix(CoroutineAspect): ensure Sentry SDK initialization in new coroutine context transfer

* fix(CoroutineAspect): update operation name to 'coroutine.create' for accurate tracing

* fix(CoroutineAspect): update operation names for better tracing accuracy

* fix(EventHandleListener): streamline span handling by consolidating parent span checks

* fix(EventHandleListener): add check for parent span sampling before proceeding

* fix(EventHandleListener): ensure proper handling of parent span lifecycle in command execution

* fix(EventHandleListener): update operation name to 'console.command.execute' for accurate command tracing

* fix(EventHandleListener): ensure parent span is set in CoContainer only if sampled

* fix(EventHandleListener): improve parent span handling by checking span instance and sampling before finishing

* fix(EventHandleListener): ensure parent span is finished before setting in CoContainer

* fix(EventHandleListener): remove command from CoContainer if parent span is not sampled

---------

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