Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Sep 26, 2025

Summary

This PR refactors the Sentry integration by introducing a new Feature class to replace the existing Switcher class. The changes improve code clarity and naming conventions while maintaining full backward compatibility.

Changes Made:

  • New Feature class: Introduces a new class with the same functionality as Switcher but with a more descriptive name
  • Backward compatibility: The Switcher class now extends Feature and maintains all existing methods with proper deprecation warnings
  • Updated dependencies: All aspect classes, listeners, and tracers now use the Feature class instead of Switcher
  • Test updates: Updated the test suite to use the new Feature class
  • Proper deprecation: All methods in Switcher are marked as deprecated with clear migration paths

Breaking Changes:

⚠️ Deprecation Notice: The Switcher class is deprecated as of v3.1 and will be removed in v3.2. Users should migrate to the Feature class.

Migration Guide:

// Old way (deprecated)
use FriendsOfHyperf\Sentry\Switcher;

public function __construct(protected Switcher $switcher) {}

// New way (recommended)  
use FriendsOfHyperf\Sentry\Feature;

public function __construct(protected Feature $switcher) {}

All method calls remain the same - only the class name changes.

Test Plan

  • All existing tests pass with the new Feature class
  • Backward compatibility maintained - existing code using Switcher continues to work
  • Deprecation warnings are properly shown when using deprecated methods
  • All aspect classes and listeners successfully use the new Feature class
  • Static methods on Switcher continue to work through inheritance

Summary by CodeRabbit

  • 新功能
    • 新增“Feature”用于统一控制 Sentry 功能开关:启用、面包屑、Tracing、Span、额外标签、Crons,并支持禁用协程追踪标记。
  • 重构
    • 全面将各 Aspect/监听器/Tracer 依赖从 Switcher 切换为 Feature,保持现有行为与配置兼容。
  • 维护
    • 标记 Switcher 为弃用,保留向后兼容包装,后续版本将移除。
  • 测试
    • 更新测试用例以覆盖 Feature 的开关逻辑与配置读取。

- Introduce new Feature class with the same functionality as Switcher
- Update all aspect classes and listeners to use Feature instead of Switcher
- Convert Switcher to extend Feature for backward compatibility with deprecation warnings
- Add appropriate @deprecated annotations for all Switcher methods
- Update test to use Feature class instead of Switcher
- This change improves code clarity while maintaining backward compatibility

Breaking change: Switcher class is deprecated and will be removed in v3.2
@coderabbitai
Copy link

coderabbitai bot commented Sep 26, 2025

Walkthrough

将所有依赖 Switcher 的类与测试替换为依赖新引入的 Feature 类。新增 Feature 并调整 Switcher 继承自 Feature(标记为弃用)。更新各 Aspect、Listener、Tracer 中的构造函数与方法调用为 Feature 风格的开关检查;测试同步更新。

Changes

Cohort / File(s) Summary
Feature 引入与 Switcher 调整
src/sentry/src/Feature.php, src/sentry/src/Switcher.php
新增 Feature,提供 isEnabled/isBreadcrumbEnabled/isTracing* 等检查与协程追踪禁用标记;Switcher 改为继承 Feature、标记弃用并移除/改造原方法以委托给 Feature。
非追踪 Aspects(Breadcrumb/普通功能)
src/sentry/src/Aspect/CacheAspect.php, .../FilesystemAspect.php, .../GuzzleHttpClientAspect.php, .../LoggerAspect.php, .../RedisAspect.php
构造函数依赖由 Switcher→Feature;内部 $this->switcher 调用改为 $this->feature(如 isBreadcrumbEnabled('cache'
Tracing Aspects
src/sentry/src/Tracing/Aspect/*(含 AmqpProducerAspect.php, AsyncQueueJobMessageAspect.php, CacheAspect.php, CoordinatorAspect.php, CoroutineAspect.php, DbAspect.php, ElasticsearchAspect.php, FilesystemAspect.php, GrpcAspect.php, GuzzleHttpClientAspect.php, KafkaProducerAspect.php, RedisAspect.php, RpcAspect.php, TraceAnnotationAspect.php
统一将构造参数和属性由 Switcher→Feature;所有 isTracingEnabled/isTracingSpanEnabled/isTracingExtraTagEnabled 等调用替换为 $this->feature->...CoroutineAspect 处静态检查改为 Feature::isDisableCoroutineTracing()
Listeners(事件处理)
src/sentry/src/Listener/EventHandleListener.php, src/sentry/src/Crons/Listener/EventHandleListener.php, src/sentry/src/Tracing/Listener/EventHandleListener.php
构造函数参数由 Switcher→Feature;内部所有开关检查改为使用 Feature(isEnabled/isBreadcrumbEnabled/isTracing* 等)。
Tracer
src/sentry/src/Tracing/Tracer.php
构造依赖改为 Feature;trace 流程中的 extra tag 开关检查改用 $this->feature
Tests
tests/Sentry/FeatureTest.php
测试从使用 Switcher 改为使用 Feature,并调整断言调用到 Feature。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant A as Aspect/Listener/Tracer
  participant F as Feature
  participant C as ConfigInterface
  participant S as SentrySdk

  A->>F: isEnabled/isBreadcrumbEnabled/isTracing* (key)
  F->>C: 读取配置项(sentry.*)
  Note right of F: 若为 isTracingSpanEnabled<br/>检查是否存在活跃 Span
  F->>S: 获取当前 Hub/Span(仅 tracing 场景)
  F-->>A: 返回布尔结果
  alt 结果为真
    A->>A: 执行既有逻辑(记录面包屑/启动Span/附加标签)
  else 结果为假
    A-->>A: 跳过与 Sentry 相关处理
  end
Loading

Estimated code review effort

🎯 3 (中等) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

Poem

小兔掀耳听风声,
开关一换更分明。
Feature 轻点如春雨,
追踪与面包屑自成经。
旧友 Switcher 挥手别,
代码田里新苗青。 🐰🌱

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.93% 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 的主要变更:将 Switcher 类替换为 Feature 类,并包含了作用域 (sentry) 和变更类型 (refactor),能让团队成员快速了解此 PR 的要点。
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/replace-switcher-with-feature

📜 Recent 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 074cec5 and 3e3bc9d.

📒 Files selected for processing (24)
  • src/sentry/src/Aspect/CacheAspect.php (3 hunks)
  • src/sentry/src/Aspect/FilesystemAspect.php (2 hunks)
  • src/sentry/src/Aspect/GuzzleHttpClientAspect.php (2 hunks)
  • src/sentry/src/Aspect/LoggerAspect.php (2 hunks)
  • src/sentry/src/Aspect/RedisAspect.php (3 hunks)
  • src/sentry/src/Crons/Listener/EventHandleListener.php (3 hunks)
  • src/sentry/src/Listener/EventHandleListener.php (25 hunks)
  • src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/CacheAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/CoordinatorAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php (3 hunks)
  • src/sentry/src/Tracing/Aspect/DbAspect.php (4 hunks)
  • src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/FilesystemAspect.php (1 hunks)
  • src/sentry/src/Tracing/Aspect/GrpcAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (4 hunks)
  • src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/RedisAspect.php (3 hunks)
  • src/sentry/src/Tracing/Aspect/RpcAspect.php (3 hunks)
  • src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php (3 hunks)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (19 hunks)
  • src/sentry/src/Tracing/Tracer.php (3 hunks)
  • tests/Sentry/FeatureTest.php (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (11)
  • src/sentry/src/Tracing/Aspect/CoordinatorAspect.php
  • src/sentry/src/Tracing/Aspect/GrpcAspect.php
  • src/sentry/src/Listener/EventHandleListener.php
  • src/sentry/src/Aspect/CacheAspect.php
  • src/sentry/src/Tracing/Tracer.php
  • src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php
  • src/sentry/src/Aspect/RedisAspect.php
  • src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php
  • src/sentry/src/Crons/Listener/EventHandleListener.php
  • src/sentry/src/Tracing/Aspect/DbAspect.php
  • src/sentry/src/Aspect/GuzzleHttpClientAspect.php
🧰 Additional context used
🧬 Code graph analysis (13)
src/sentry/src/Tracing/Aspect/RpcAspect.php (1)
src/sentry/src/Feature.php (3)
  • Feature (18-71)
  • isTracingSpanEnabled (43-50)
  • isTracingExtraTagEnabled (52-55)
src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php (2)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • isTracingEnabled (34-41)
src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php (2)
  • __construct (38-40)
  • process (42-53)
src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php (1)
src/sentry/src/Feature.php (3)
  • Feature (18-71)
  • isTracingSpanEnabled (43-50)
  • isTracingExtraTagEnabled (52-55)
src/sentry/src/Tracing/Listener/EventHandleListener.php (1)
src/sentry/src/Feature.php (4)
  • Feature (18-71)
  • isTracingSpanEnabled (43-50)
  • isTracingExtraTagEnabled (52-55)
  • isTracingEnabled (34-41)
src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (1)
src/sentry/src/Feature.php (3)
  • Feature (18-71)
  • isTracingSpanEnabled (43-50)
  • isTracingExtraTagEnabled (52-55)
src/sentry/src/Aspect/FilesystemAspect.php (1)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • isBreadcrumbEnabled (29-32)
src/sentry/src/Tracing/Aspect/FilesystemAspect.php (1)
src/sentry/src/Feature.php (1)
  • isTracingEnabled (34-41)
src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php (1)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • isTracingExtraTagEnabled (52-55)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (1)
src/sentry/src/Feature.php (4)
  • Feature (18-71)
  • isTracingSpanEnabled (43-50)
  • isDisableCoroutineTracing (67-70)
  • isTracingExtraTagEnabled (52-55)
src/sentry/src/Tracing/Aspect/RedisAspect.php (1)
src/sentry/src/Feature.php (3)
  • Feature (18-71)
  • isTracingSpanEnabled (43-50)
  • isTracingExtraTagEnabled (52-55)
tests/Sentry/FeatureTest.php (1)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • isTracingExtraTagEnabled (52-55)
src/sentry/src/Tracing/Aspect/CacheAspect.php (1)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • isTracingSpanEnabled (43-50)
src/sentry/src/Aspect/LoggerAspect.php (4)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • isBreadcrumbEnabled (29-32)
src/sentry/src/Aspect/CacheAspect.php (1)
  • __construct (35-37)
src/sentry/src/Aspect/GuzzleHttpClientAspect.php (1)
  • __construct (32-34)
src/sentry/src/Aspect/RedisAspect.php (1)
  • __construct (33-35)
🪛 PHPMD (2.15.0)
src/sentry/src/Aspect/LoggerAspect.php

41-41: Avoid unused parameters such as '$result'. (undefined)

(UnusedFormalParameter)

⏰ 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.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.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 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.3 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
🔇 Additional comments (23)
src/sentry/src/Tracing/Aspect/FilesystemAspect.php (1)

25-28: Trace 开关检查保持一致

使用 $this->feature 读取 tracing 开关与基类的依赖改动对齐,行为保持原样。

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

41-84: Redis 结果标签守卫合理

在写入 span 数据前通过新 Feature 检查额外标签,与新开关语义保持一致,逻辑清晰。

tests/Sentry/FeatureTest.php (1)

29-34: 测试覆盖调整到位

改用 Feature 并验证 isTracingExtraTagEnabled 的布尔返回值,成功跟随主代码重构。

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

43-115: RPC 特性检查迁移妥当

RPC 相关的 span 开关与结果标签均切换到 Feature,与其他 Aspect 一致且无额外副作用。

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

30-58: 注解切面与 Feature 对齐

对注解结果的附加数据加入 Feature 检查,延续了原有的可配置行为。

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

53-67: Elasticsearch span 行为保持一致

Feature 控制 span 建立及结果附加,保证与新特性类的统一入口。

src/sentry/src/Aspect/FilesystemAspect.php (1)

44-51: Breadcrumb 开关迁移顺畅

文件系统面包屑由 Feature 承担开关,符合整体重构方向,逻辑未变。

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

39-46: 确认依赖注入别名是否同步 在 src/sentry/src/ConfigProvider.php 中未检测到 Feature::class 或 Switcher::class 的绑定,可能导致基于 Switcher::class 的自定义覆写被绕过,需要在 ConfigProvider 或自定义 bindings 中添加 alias。

src/sentry/src/Tracing/Aspect/CoroutineAspect.php (2)

39-49: Feature 注入改造保持协程追踪开关逻辑一致

构造函数切换到 Feature 并在入口统一用 Feature::isDisableCoroutineTracing() 判定,行为与原 Switcher 保持一致,兼容旧的上下文禁用标记。


118-121: 异常扩展标签判断逻辑迁移正确

isTracingExtraTagEnabled('exception.stack_trace') 直接复用 Feature 配置读取,维持与原实现一致的扩展标签开关。

src/sentry/src/Aspect/LoggerAspect.php (1)

35-43: Logger 面包屑开关迁移到 Feature 正常

依赖注入和判定逻辑完整迁移到 Feature,确保日志面包屑的开关配置继续生效。

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

37-44: AMQP 追踪开关切换到 Feature 无副作用

构造器与 isTracingEnabled('amqp') 判定同步替换为 Feature,保持原有追踪开关语义。

src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (2)

41-50: Guzzle 追踪 Span 开关迁移得当

AOP 构造参数与 isTracingSpanEnabled('guzzle') 使用统一到 Feature,与原 Switcher 行为对齐。


125-172: HTTP 响应体与异常堆栈扩展标签判定正确迁移

isTracingExtraTagEnabled 的多处调用全面改为 Feature,确保自定义标签开关仍按配置生效。

src/sentry/src/Tracing/Listener/EventHandleListener.php (9)

67-173: 事件监听主入口成功切换到 Feature

构造依赖和各类 tracing/breadcrumb 开关全部迁移至 Feature,主流程的开关逻辑保持不变。


195-203: 数据库绑定扩展标签默认值保持一致

isTracingExtraTagEnabled('db.sql.bindings', true) 继续沿用原有默认启用策略,兼顾配置向后兼容。


360-375: 请求异常堆栈额外标签迁移无误

对异常堆栈的可选记录依旧通过 Feature 控制,逻辑与旧实现一致。


414-451: 命令执行结果追踪保持 Feature 化逻辑

命令执行完成时的状态、堆栈与标签依然由 Feature 控制,确保可选信息采集策略不变。


470-487: Redis 结果与异常标签判定迁移正确

Redis 结果与异常堆栈的可选数据采集均改为 Feature 控制,维持原配置兼容。


551-564: Crontab 异常堆栈开关迁移保持一致

定时任务异常堆栈采集继续通过 Feature 开关控制,逻辑无变化。


633-646: AMQP 消费异常标签开关迁移正确

消费异常的数据采集改用 Feature,与原 Switcher 配置读取保持一致。


709-722: Kafka 消费异常堆栈开关迁移良好

Kafka 相关的异常堆栈附加信息继续受 Feature 控制,兼容原行为。


772-785: 异步队列异常堆栈开关保持一致

异步队列处理的异常堆栈采集同样迁移至 Feature,延续原有配置语义。

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

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (4)
src/sentry/src/Aspect/GuzzleHttpClientAspect.php (1)

62-80: 敏感信息泄露风险:记录了完整 URL 与原始请求/响应头

  • http.request.full_url 包含原始 query,可能携带 token/secret。
  • http.request.headers、http.response.headers 未脱敏,可能泄露 Authorization/Cookie 等敏感头。

建议最小化修复,脱敏敏感头并移除 URL query:

-            $uri = $request->getUri()->__toString();
-            $data = [
+            $uriWithoutQuery = (string) $request->getUri()->withQuery('');
+            $sensitiveHeaders = ['authorization', 'proxy-authorization', 'cookie', 'set-cookie', 'x-api-key', 'x-auth-token'];
+            $requestHeaders = array_change_key_case($request->getHeaders(), CASE_LOWER);
+            foreach ($requestHeaders as $name => &$values) {
+                if (in_array($name, $sensitiveHeaders, true)) {
+                    $values = ['[REDACTED]'];
+                }
+            }
+            unset($values);
+            $responseHeaders = array_change_key_case($response?->getHeaders() ?? [], CASE_LOWER);
+            foreach ($responseHeaders as $name => &$values) {
+                if (in_array($name, ['set-cookie', 'authorization'], true)) {
+                    $values = ['[REDACTED]'];
+                }
+            }
+            unset($values);
+            $data = [
                 'config' => $guzzleConfig,
                 // request
                 'http.request.method' => $request->getMethod(),
-                'http.request.body.size' => strlen($options['body'] ?? ''),
-                'http.request.full_url' => (string) $request->getUri(),
+                'http.request.body.size' => isset($options['body']) ? strlen((string) $options['body']) : 0,
+                'http.request.full_url' => $uriWithoutQuery,
                 'http.request.path' => $request->getUri()->getPath(),
                 'http.request.scheme' => $request->getUri()->getScheme(),
                 'http.request.host' => $request->getUri()->getHost(),
                 'http.request.port' => $request->getUri()->getPort(),
                 'http.request.user_agent' => $request->getHeaderLine('User-Agent'), // updated key for consistency
-                'http.request.headers' => $request->getHeaders(),
+                'http.request.headers' => $requestHeaders,
                 // response
                 'http.response.status_code' => $response?->getStatusCode(),
                 'http.response.body.size' => $response?->getBody()->getSize() ?? 0,
                 'http.response.reason' => $response?->getReasonPhrase(),
-                'http.response.headers' => $response?->getHeaders(),
+                'http.response.headers' => $responseHeaders,
                 'duration' => $stats->getTransferTime() * 1000,
             ];
@@
-            Integration::addBreadcrumb(new Breadcrumb(
+            Integration::addBreadcrumb(new Breadcrumb(
                 Breadcrumb::LEVEL_INFO,
                 Breadcrumb::TYPE_DEFAULT,
                 'guzzle',
-                $uri,
+                $uriWithoutQuery,
                 $data
             ));

此外,如需保留部分 query,可引入白名单方式仅上报非敏感参数。

Also applies to: 82-88

src/sentry/src/Aspect/FilesystemAspect.php (1)

76-92: match 语句存在语法问题(可能导致解析失败)

分支列表里 'mimeType', 前多余逗号,导致 => 前出现尾随逗号。应去掉该逗号,维持多条件合并的合法语法。

建议修复:

         $description = match ($method) {
             'move', 'copy' => sprintf(
                 'from "%s" to "%s"',
                 $arguments['source'] ?? '',
                 $arguments['destination'] ?? ''
             ),
             'write', 'writeStream',
             'read', 'readStream',
             'setVisibility', 'visibility',
             'delete', 'deleteDirectory', 'createDirectory',
             'fileExists', 'directoryExists',
             'listContents',
             'lastModified',
             'fileSize',
-            'mimeType', => $arguments['path'] ?? '',
+            'mimeType' => $arguments['path'] ?? '',
             default => '',
         };
src/sentry/src/Tracing/Aspect/CacheAspect.php (1)

96-101: 潜在运行时错误:json_encode 失败时 strlen 参数类型不匹配

在 item_size 计算中,json_encode 可能返回 false,strlen(false) 将抛 TypeError(PHP 8+)。

建议为 json_encode 结果添加安全转换:

                 ->setData([
                     'cache.key' => $keys,
                     'cache.ttl' => $arguments['ttl'] ?? null,
                     'item_size' => match (true) {
-                        isset($arguments['value']) => strlen(json_encode($arguments['value'])),
-                        isset($arguments['values']) && is_array($arguments['values']) => strlen(json_encode(array_values($arguments['values']))),
+                        isset($arguments['value']) => strlen((string) (json_encode($arguments['value']) ?: '')),
+                        isset($arguments['values']) && is_array($arguments['values']) => strlen((string) (json_encode(array_values($arguments['values'])) ?: '')),
                         default => 0,
                     },
                 ])
src/sentry/src/Switcher.php (1)

62-76: 缺少 Feature::isExceptionIgnored 导致迁移后致命错误

根据 PR 说明,使用方会把类型提示从 Switcher 切到 Feature,方法调用保持不变。但当前 Feature 并未实现 isExceptionIgnored()(见 src/sentry/src/Feature.php),因此一旦代码改为注入 Feature,调用该方法会直接触发 “Call to undefined method Feature::isExceptionIgnored()” 的致命错误。请把该实现上移到 Feature(并在 Feature 引入 use Throwable;),Switcher 只需继承即可,从而保证迁移安全。

建议补充的实现:

+use Throwable;
+
 class Feature
 {
@@
+    public function isExceptionIgnored(string|Throwable $exception): bool
+    {
+        $ignoreExceptions = (array) $this->config->get('sentry.ignore_exceptions', []);
+
+        foreach ($ignoreExceptions as $ignoreException) {
+            if (is_a($exception, $ignoreException, true)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
🧹 Nitpick comments (1)
src/sentry/src/Feature.php (1)

67-70: 命名可读性微调建议

isDisableCoroutineTracing 命名略显别扭,建议新增更自然的等价别名 isCoroutineTracingDisabled(保留现有方法并标注 @deprecated 以兼容既有代码)。

📜 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 6a7a602 and 074cec5.

📒 Files selected for processing (25)
  • src/sentry/src/Aspect/CacheAspect.php (2 hunks)
  • src/sentry/src/Aspect/FilesystemAspect.php (2 hunks)
  • src/sentry/src/Aspect/GuzzleHttpClientAspect.php (2 hunks)
  • src/sentry/src/Aspect/LoggerAspect.php (2 hunks)
  • src/sentry/src/Aspect/RedisAspect.php (2 hunks)
  • src/sentry/src/Crons/Listener/EventHandleListener.php (2 hunks)
  • src/sentry/src/Feature.php (1 hunks)
  • src/sentry/src/Listener/EventHandleListener.php (2 hunks)
  • src/sentry/src/Switcher.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/CacheAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/CoordinatorAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/CoroutineAspect.php (3 hunks)
  • src/sentry/src/Tracing/Aspect/DbAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/GrpcAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/RedisAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/RpcAspect.php (2 hunks)
  • src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php (2 hunks)
  • src/sentry/src/Tracing/Listener/EventHandleListener.php (2 hunks)
  • src/sentry/src/Tracing/Tracer.php (2 hunks)
  • tests/Sentry/SwitcherTest.php (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (25)
src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
tests/Sentry/SwitcherTest.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Aspect/LoggerAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Aspect/CacheAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/CoordinatorAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Listener/EventHandleListener.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php (2)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php (1)
  • __construct (38-40)
src/sentry/src/Tracing/Aspect/CacheAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/DbAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php (2)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Aspect/LoggerAspect.php (1)
  • __construct (35-37)
src/sentry/src/Aspect/GuzzleHttpClientAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Aspect/FilesystemAspect.php (1)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • __construct (20-22)
src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Tracer.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/GrpcAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/RpcAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Aspect/RedisAspect.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Aspect/RedisAspect.php (3)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Aspect/CacheAspect.php (1)
  • __construct (35-37)
src/sentry/src/Tracing/Aspect/RedisAspect.php (1)
  • __construct (41-45)
src/sentry/src/Feature.php (1)
src/sentry/class_map/SentrySdk.php (2)
  • SentrySdk (24-65)
  • getCurrentHub (51-54)
src/sentry/src/Tracing/Aspect/CoroutineAspect.php (1)
src/sentry/src/Feature.php (2)
  • Feature (18-71)
  • isDisableCoroutineTracing (67-70)
src/sentry/src/Crons/Listener/EventHandleListener.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Tracing/Listener/EventHandleListener.php (1)
src/sentry/src/Feature.php (1)
  • Feature (18-71)
src/sentry/src/Switcher.php (1)
src/sentry/src/Feature.php (5)
  • Feature (18-71)
  • isEnabled (24-27)
  • isBreadcrumbEnabled (29-32)
  • isTracingEnabled (34-41)
  • isTracingSpanEnabled (43-50)
⏰ 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 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.2 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 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
🔇 Additional comments (24)
src/sentry/src/Feature.php (1)

24-61: 整体实现稳健,接口清晰且默认值合理

  • 配置读取与分级开关设计清晰(全局开关优先于子开关)。
  • isTracingSpanEnabled 在无当前 Span 时短路,避免空指针,👍
src/sentry/src/Aspect/CacheAspect.php (1)

15-15: 从 Switcher 切换到 Feature 的依赖注入变更正确

类型与 use 更新一致,运行时行为不变。

Also applies to: 35-35

src/sentry/src/Aspect/GuzzleHttpClientAspect.php (1)

14-14: 依赖注入从 Switcher → Feature 的迁移正确

构造参数与 use 一致更新,无行为回归。

Also applies to: 32-32

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

16-16: 从 Switcher → Feature 的迁移正确

类型与用法一致,追踪开关行为未改变。

Also applies to: 43-44

src/sentry/src/Aspect/FilesystemAspect.php (1)

14-14: 从 Switcher → Feature 的替换正确

构造函数与 use 更新一致。

Also applies to: 44-44

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

14-14: 依赖注入类型替换正确

与 Feature 的 API 对齐,无行为变化。

Also applies to: 39-39

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

14-14: 从 Switcher → Feature 的替换正确

切换到 isTracingSpanEnabled('coordinator') 的用法一致,逻辑无回归。

Also applies to: 29-29

src/sentry/src/Aspect/LoggerAspect.php (1)

16-16: 依赖注入替换正确

日志面包屑开关逻辑保持不变。

Also applies to: 35-35

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

14-70: 替换 Feature 类型无行为差异

构造函数改为注入 Feature 与新类保持一致,后续逻辑保持不变。

tests/Sentry/SwitcherTest.php (1)

14-39: 测试对 Feature 的覆盖保持完整

用例切换到 Feature 后依旧验证相同的配置读取路径,覆盖面未受影响。

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

15-128: Kafka 追踪开关迁移到 Feature 合理

构造参数与注入点同步更新为 Feature,其余追踪逻辑维持原状。

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

14-72: gRPC 追踪依赖调整一致

依赖改为 Feature 后仍按原机制判断 span 开关,行为保持稳定。

src/sentry/src/Tracing/Tracer.php (1)

14-116: Tracer 改注入 Feature 符合整体重构

更换依赖类型后仍能沿用相同的追踪配置与异常标记逻辑。

src/sentry/src/Crons/Listener/EventHandleListener.php (1)

15-155: Crons 监听器依赖更新与 Feature 匹配

构造函数改为 Feature 后,定时任务开关判断与之前保持一致。

src/sentry/src/Listener/EventHandleListener.php (1)

14-624: 事件监听器使用 Feature 继续覆盖全部开关

注入类型调换后,所有 isEnabled/isBreadcrumbEnabled 调用无须改动,逻辑未受影响。

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

15-116: AMQP 生产者追踪改依赖 Feature 一致

追踪开关继续通过相同接口读取,功能与重构目标对齐。

src/sentry/src/Tracing/Listener/EventHandleListener.php (1)

16-16: 依赖类型替换确认无误

导入与构造函数参数同步改为 Feature,与新特性类保持一致,同时维持 $switcher 属性命名不会影响现有逻辑。

Also applies to: 70-70

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

14-30: 注入类型更新保持一致性

构造函数改用 Feature,确保注解切面使用新的特性开关实现,行为保持稳定。

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

15-45: RPC 切面依赖调整合理

依赖类型替换为 Feature 后其余逻辑无需改动,现有特性开关调用继续生效。

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

14-49: 协程切面同步至新特性类

构造注入和静态方法调用统一指向 Feature,覆盖到协程追踪禁用判断,逻辑闭环完整。

src/sentry/src/Aspect/RedisAspect.php (1)

14-34: Redis 面包屑切面迁移确认

引入 Feature 后依旧复用 $switcher 属性,面包屑开关判定无需额外调整。

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

14-38: 数据库切面依赖替换正确

构造参数更新为 Feature 与新类接口匹配,其余追踪逻辑保持原样。

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

14-44: Redis 追踪切面适配完成

依赖改为 Feature 后相关配置读取与特性开关判定无需改动,兼容新命名。

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

15-44: HTTP 客户端切面更新一致

构造函数与属性导入同步指向 Feature,其余调用链保持稳定。

- Updated the constructor parameter name from `switcher` to `feature` in various aspects including CacheAspect, FilesystemAspect, GuzzleHttpClientAspect, LoggerAspect, RedisAspect, and others for consistency and clarity.
- Adjusted the corresponding method calls to use the new `feature` property instead of `switcher`.
- Removed the obsolete SwitcherTest file and replaced it with a new FeatureTest file to reflect the changes in the Feature class.
@huangdijia huangdijia merged commit 530eb62 into main Sep 26, 2025
15 of 16 checks passed
@huangdijia huangdijia deleted the refactor/replace-switcher-with-feature branch September 26, 2025 00:57
huangdijia added a commit that referenced this pull request Sep 26, 2025
* refactor(sentry): replace Switcher class with Feature class

- Introduce new Feature class with the same functionality as Switcher
- Update all aspect classes and listeners to use Feature instead of Switcher
- Convert Switcher to extend Feature for backward compatibility with deprecation warnings
- Add appropriate @deprecated annotations for all Switcher methods
- Update test to use Feature class instead of Switcher
- This change improves code clarity while maintaining backward compatibility

Breaking change: Switcher class is deprecated and will be removed in v3.2

* refactor: rename switcher to feature in Sentry components

- Updated the constructor parameter name from `switcher` to `feature` in various aspects including CacheAspect, FilesystemAspect, GuzzleHttpClientAspect, LoggerAspect, RedisAspect, and others for consistency and clarity.
- Adjusted the corresponding method calls to use the new `feature` property instead of `switcher`.
- Removed the obsolete SwitcherTest file and replaced it with a new FeatureTest file to reflect the changes in the Feature class.

---------

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