From 473305d4b6a6ee5faebf4870c36da40d493e2c4f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 7 Sep 2025 14:55:45 +0800 Subject: [PATCH 1/2] 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. --- src/sentry/src/Tracing/SpanStarter.php | 38 ++++++++++++-------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/sentry/src/Tracing/SpanStarter.php b/src/sentry/src/Tracing/SpanStarter.php index 64034f15f..5c7d5dfe7 100644 --- a/src/sentry/src/Tracing/SpanStarter.php +++ b/src/sentry/src/Tracing/SpanStarter.php @@ -22,6 +22,7 @@ use Sentry\Tracing\SpanContext; use Sentry\Tracing\SpanStatus; use Sentry\Tracing\Transaction; +use Sentry\Tracing\TransactionContext; use Sentry\Tracing\TransactionSource; use function Hyperf\Tappable\tap; @@ -86,26 +87,23 @@ protected function continueTrace(string $sentryTrace = '', string $baggage = '', tap(clone SentrySdk::getCurrentHub(), fn (HubInterface $hub) => $hub->pushScope()) ); - $context = continueTrace($sentryTrace, $baggage); - if (isset($options['name']) && is_string($options['name'])) { - $context->setName($options['name']); - } - if (isset($options['op']) && is_string($options['op'])) { - $context->setOp($options['op']); - } - if (isset($options['description']) && is_string($options['description'])) { - $context->setDescription($options['description']); - } - if (isset($options['origin']) && is_string($options['origin'])) { - $context->setOrigin($options['origin']); - } - if (isset($options['source']) && $options['source'] instanceof TransactionSource) { - $context->setSource($options['source']); - } else { - $context->setSource(TransactionSource::custom()); - } - - $transaction = $hub->startTransaction($context) + $transaction = $hub->startTransaction( + tap( + continueTrace($sentryTrace, $baggage), + function (TransactionContext $context) use ($options) { + 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, + }; + }); + } + ) + ) ->setStartTimestamp(microtime(true)) ->setStatus(SpanStatus::ok()); From f04a4e79341b6a3f2ef40accb9b9d39993c086d2 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 7 Sep 2025 15:22:05 +0800 Subject: [PATCH 2/2] fix(tracing): improve source setting logic in SpanStarter trait --- src/sentry/src/Tracing/SpanStarter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sentry/src/Tracing/SpanStarter.php b/src/sentry/src/Tracing/SpanStarter.php index 5c7d5dfe7..1bbde5083 100644 --- a/src/sentry/src/Tracing/SpanStarter.php +++ b/src/sentry/src/Tracing/SpanStarter.php @@ -97,7 +97,9 @@ function (TransactionContext $context) use ($options) { '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()), + 'source' => $context->setSource( + $value instanceof TransactionSource ? $value : TransactionSource::custom() + ), default => null, }; });