From 28499d964d25150d32a09f2b068029e639366029 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 12:11:57 +0800 Subject: [PATCH 01/10] fix: improve cache key handling in CacheAspect - Fix key extraction for cache operations to use correct argument structure - Update span description to properly handle array keys by imploding them - Set asParent=true for better tracing hierarchy --- src/sentry/src/Tracing/Aspect/CacheAspect.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CacheAspect.php b/src/sentry/src/Tracing/Aspect/CacheAspect.php index e31ae7a6c..76d668b4b 100644 --- a/src/sentry/src/Tracing/Aspect/CacheAspect.php +++ b/src/sentry/src/Tracing/Aspect/CacheAspect.php @@ -65,11 +65,16 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) /** @var string|string[] $key */ $key = match ($method) { - 'set', 'get', 'delete', 'setMultiple', 'getMultiple', 'deleteMultiple' => $proceedingJoinPoint->arguments['order'][0] ?? 'unknown', + 'set', 'get', 'delete' => array_first($proceedingJoinPoint->arguments['keys']) ?? 'unknown', + 'setMultiple', 'getMultiple', 'deleteMultiple' => array_first($proceedingJoinPoint->arguments['keys']) ?? [], default => '', }; - $span = $this->startSpan(op: $op, description: $key); + $span = $this->startSpan( + op: $op, + description: implode(',', (array) $key), + asParent: true, + ); return tap($proceedingJoinPoint->process(), function ($value) use ($span, $method, $key) { match ($method) { From 54b05ed8b6e4533176db3da314929e7ba1b03747 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 13:30:42 +0800 Subject: [PATCH 02/10] fix: refactor CacheAspect to improve key handling and reduce redundancy --- src/sentry/src/Tracing/Aspect/CacheAspect.php | 101 ++++-------------- 1 file changed, 23 insertions(+), 78 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CacheAspect.php b/src/sentry/src/Tracing/Aspect/CacheAspect.php index 76d668b4b..a3c27df6d 100644 --- a/src/sentry/src/Tracing/Aspect/CacheAspect.php +++ b/src/sentry/src/Tracing/Aspect/CacheAspect.php @@ -16,7 +16,6 @@ use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use Sentry\SentrySdk; -use Sentry\Tracing\Span; use function Hyperf\Tappable\tap; @@ -64,93 +63,39 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) }; /** @var string|string[] $key */ - $key = match ($method) { - 'set', 'get', 'delete' => array_first($proceedingJoinPoint->arguments['keys']) ?? 'unknown', - 'setMultiple', 'getMultiple', 'deleteMultiple' => array_first($proceedingJoinPoint->arguments['keys']) ?? [], - default => '', + [$key, $ttl] = match ($method) { + 'set', 'get', 'delete' => [ + $proceedingJoinPoint->arguments['keys']['key'] ?? 'unknown', + $proceedingJoinPoint->arguments['keys']['ttl'] ?? null, + ], + 'setMultiple', 'getMultiple', 'deleteMultiple' => [ + $proceedingJoinPoint->arguments['keys']['keys'] ?? [], + $proceedingJoinPoint->arguments['keys']['ttl'] ?? null, + ], + default => ['', null], }; $span = $this->startSpan( op: $op, - description: implode(',', (array) $key), - asParent: true, + description: implode(', ', (array) $key), + asParent: true ); - return tap($proceedingJoinPoint->process(), function ($value) use ($span, $method, $key) { - match ($method) { - 'set', => $this->handleSet($span, $key, $value), - 'get', 'fetch' => $this->handleGet($span, $key, $value), - 'delete' => $this->handleDelete($span, $key, $value), - 'setMultiple' => $this->handleSetMultiple($span, $key, $value), - 'getMultiple' => $this->handleGetMultiple($span, $key, $value), - 'deleteMultiple' => $this->handleDeleteMultiple($span, $key, $value), - 'clear' => $this->handleClear($span), - default => null, + return tap($proceedingJoinPoint->process(), function ($result) use ($span, $method, $key, $ttl) { + $data = match ($method) { + 'set', => ['cache.key' => $key, 'cache.ttl' => $ttl], + 'get', 'fetch' => ['cache.key' => $key, 'cache.hit' => ! is_null($result)], + 'delete' => ['cache.key' => $key], + 'setMultiple' => ['cache.key' => $key, 'cache.ttl' => $ttl], + 'getMultiple' => ['cache.key' => $key, 'cache.hit' => ! empty($result)], + 'deleteMultiple' => ['cache.key' => $key], + 'clear' => [], + default => [], }; + $span->setOrigin('auto.cache')->setData($data)->finish(); }); } finally { SentrySdk::getCurrentHub()->setSpan($parent); } } - - private function handleSet(Span $span, string $key, mixed $value) - { - $span - ->setData([ - 'cache.key' => $key, - ]) - ->finish(); - } - - private function handleGet(Span $span, string $key, mixed $value) - { - $span - ->setData([ - 'cache.key' => $key, - 'cache.hit' => ! is_null($value), - ]) - ->finish(); - } - - private function handleDelete(Span $span, string $key, mixed $value) - { - $span - ->setData([ - 'cache.key' => $key, - ]) - ->finish(); - } - - private function handleSetMultiple(Span $span, array $keys, array $values) - { - $span - ->setData([ - 'cache.key' => $keys, - ]) - ->finish(); - } - - private function handleGetMultiple(Span $span, array $keys, array $values) - { - $span - ->setData([ - 'cache.key' => $keys, - 'cache.hit' => ! empty($values), - ]) - ->finish(); - } - - private function handleDeleteMultiple(Span $span, array $keys, array $values) - { - $span - ->setData([ - 'cache.key' => $keys, - ]) - ->finish(); - } - - private function handleClear(Span $span) - { - $span->finish(); - } } From 2fd067d1e1734037130817915739fd495745abcf Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 13:36:53 +0800 Subject: [PATCH 03/10] fix: consolidate cache operation handling in CacheAspect for improved clarity --- src/sentry/src/Tracing/Aspect/CacheAspect.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CacheAspect.php b/src/sentry/src/Tracing/Aspect/CacheAspect.php index a3c27df6d..b229d25a3 100644 --- a/src/sentry/src/Tracing/Aspect/CacheAspect.php +++ b/src/sentry/src/Tracing/Aspect/CacheAspect.php @@ -52,12 +52,9 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $method = $proceedingJoinPoint->methodName; $op = match ($method) { - 'set' => 'cache.put', - 'get', 'fetch' => 'cache.get', - 'delete' => 'cache.remove', - 'setMultiple' => 'cache.put', - 'getMultiple' => 'cache.get', - 'deleteMultiple' => 'cache.remove', + 'set', 'setMultiple' => 'cache.put', + 'get', 'fetch', 'getMultiple' => 'cache.get', + 'delete', 'deleteMultiple' => 'cache.remove', 'clear' => 'cache.flush', default => 'cache', }; @@ -83,13 +80,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return tap($proceedingJoinPoint->process(), function ($result) use ($span, $method, $key, $ttl) { $data = match ($method) { - 'set', => ['cache.key' => $key, 'cache.ttl' => $ttl], + 'set', 'setMultiple' => ['cache.key' => $key, 'cache.ttl' => $ttl], + 'delete', 'deleteMultiple' => ['cache.key' => $key], 'get', 'fetch' => ['cache.key' => $key, 'cache.hit' => ! is_null($result)], - 'delete' => ['cache.key' => $key], - 'setMultiple' => ['cache.key' => $key, 'cache.ttl' => $ttl], 'getMultiple' => ['cache.key' => $key, 'cache.hit' => ! empty($result)], - 'deleteMultiple' => ['cache.key' => $key], - 'clear' => [], default => [], }; $span->setOrigin('auto.cache')->setData($data)->finish(); From fc2e529e0991cb90951ab8e41474be5a1acda511 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 13:48:47 +0800 Subject: [PATCH 04/10] fix: refactor argument handling in CacheAspect for improved clarity and consistency --- src/sentry/src/Tracing/Aspect/CacheAspect.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CacheAspect.php b/src/sentry/src/Tracing/Aspect/CacheAspect.php index b229d25a3..8dff46e8b 100644 --- a/src/sentry/src/Tracing/Aspect/CacheAspect.php +++ b/src/sentry/src/Tracing/Aspect/CacheAspect.php @@ -59,15 +59,21 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) default => 'cache', }; + $arguments = $proceedingJoinPoint->arguments['keys'] ?? []; + /** @var string|string[] $key */ [$key, $ttl] = match ($method) { 'set', 'get', 'delete' => [ - $proceedingJoinPoint->arguments['keys']['key'] ?? 'unknown', - $proceedingJoinPoint->arguments['keys']['ttl'] ?? null, + $arguments['key'] ?? 'unknown', + $arguments['ttl'] ?? null, + ], + 'setMultiple' => [ + array_keys($arguments['values'] ?? []), + $arguments['ttl'] ?? null, ], - 'setMultiple', 'getMultiple', 'deleteMultiple' => [ - $proceedingJoinPoint->arguments['keys']['keys'] ?? [], - $proceedingJoinPoint->arguments['keys']['ttl'] ?? null, + 'getMultiple', 'deleteMultiple' => [ + $arguments['keys'] ?? [], + $arguments['ttl'] ?? null, ], default => ['', null], }; From 742de23fa1f8fa8277493c8fb7b1d7aaeb6d13f5 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 13:49:45 +0800 Subject: [PATCH 05/10] fix: streamline span finalization in RedisAspect for improved clarity --- src/sentry/src/Tracing/Aspect/RedisAspect.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/RedisAspect.php b/src/sentry/src/Tracing/Aspect/RedisAspect.php index 6f48b770c..2bf27cef6 100644 --- a/src/sentry/src/Tracing/Aspect/RedisAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisAspect.php @@ -110,8 +110,7 @@ class_exists(CommandExecuted::class) throw $exception; } finally { - $span->setData($data); - $span->finish(); + $span->setOrigin('auto.cache.redis')->setData($data)->finish(); } return $result; From 4fbfc32d0bbb65481e5cd3ca9c00dda71718b376 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 13:50:35 +0800 Subject: [PATCH 06/10] fix: enhance span finalization in AsyncQueueJobMessageAspect to set origin --- src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php b/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php index 39078a2a0..8b537ca76 100644 --- a/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php +++ b/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php @@ -88,7 +88,7 @@ public function handlePush(ProceedingJoinPoint $proceedingJoinPoint) return $proceedingJoinPoint->process(); } catch (Throwable) { } finally { - $span->finish(); + $span->setOrigin('auto.queue')->finish(); } } From 575cd1a913c26ab459697c5af065f7c8a321523c Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 14:06:00 +0800 Subject: [PATCH 07/10] fix: set origin for spans in various aspects for improved tracing context --- .../src/Tracing/Aspect/AmqpProducerAspect.php | 2 +- .../src/Tracing/Aspect/CoordinatorAspect.php | 3 +-- .../src/Tracing/Aspect/CoroutineAspect.php | 4 +-- src/sentry/src/Tracing/Aspect/DbAspect.php | 3 +-- .../Tracing/Aspect/ElasticsearchAspect.php | 3 +-- src/sentry/src/Tracing/Aspect/GrpcAspect.php | 3 +-- .../Tracing/Aspect/GuzzleHttpClientAspect.php | 3 +-- .../Tracing/Aspect/KafkaProducerAspect.php | 2 +- src/sentry/src/Tracing/Aspect/RpcAspect.php | 3 +-- .../Tracing/Listener/TracingAmqpListener.php | 4 +-- .../Listener/TracingAsyncQueueListener.php | 4 +-- .../Listener/TracingCommandListener.php | 9 ++++--- .../Listener/TracingCrontabListener.php | 6 +++-- .../Listener/TracingDbQueryListener.php | 10 +++---- .../Tracing/Listener/TracingKafkaListener.php | 6 +++-- .../Tracing/Listener/TracingRedisListener.php | 19 +++++++------- .../Listener/TracingRequestListener.php | 26 ++++++++++--------- 17 files changed, 57 insertions(+), 53 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php b/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php index e438266a6..fb1a04595 100644 --- a/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php +++ b/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php @@ -75,6 +75,6 @@ protected function produceMessage(ProceedingJoinPoint $proceedingJoinPoint) $this->properties['application_headers']->set(Constants::TRACE_CARRIER, $carrier); })->call($producerMessage); - return tap($proceedingJoinPoint->process(), fn () => $span->finish()); + return tap($proceedingJoinPoint->process(), fn () => $span->setOrigin('auto.amqp')->finish()); } } diff --git a/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php b/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php index b7557691e..c4e0d06e0 100644 --- a/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php @@ -61,8 +61,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } throw $exception; } finally { - $span?->setData($data); - $span?->finish(microtime(true)); + $span?->setOrigin('auto.coordinator')->setData($data)->finish(microtime(true)); } } } diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index 6875550ec..36ef72986 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -93,11 +93,11 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) throw $exception; } finally { - $transaction->setData($data); + $transaction->setOrigin('auto.coroutine')->setData($data); } }; - $parent->finish(); + $parent->setOrigin('auto.coroutine')->finish(); return $proceedingJoinPoint->process(); } diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index b61aeeb47..174959601 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -122,8 +122,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) throw $exception; } finally { - $span->setData($data); - $span->finish(); + $span->setOrigin('auto.db')->setData($data)->finish(); } return $result; diff --git a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php index cbd9c46b9..7e844e6b3 100644 --- a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php +++ b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php @@ -102,8 +102,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) throw $exception; } finally { - $span->setData($data); - $span->finish(); + $span->setOrigin('auto.elasticsearch')->setData($data)->finish(); } return $result; diff --git a/src/sentry/src/Tracing/Aspect/GrpcAspect.php b/src/sentry/src/Tracing/Aspect/GrpcAspect.php index 9aa9c637d..a57779773 100644 --- a/src/sentry/src/Tracing/Aspect/GrpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/GrpcAspect.php @@ -89,8 +89,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) throw $exception; } finally { - $span->setData($data); - $span->finish(); + $span->setOrigin('auto.grpc')->setData($data)->finish(); } return $result; } diff --git a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php index 35b7a7fc8..761f93e15 100644 --- a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php +++ b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php @@ -144,8 +144,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } } - $span->setData($data); - $span->finish(); + $span->setOrigin('auto.http.client')->setData($data)->finish(); if (is_callable($onStats)) { ($onStats)($stats); diff --git a/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php b/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php index 3bc1b829c..4fd565581 100644 --- a/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php +++ b/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php @@ -96,6 +96,6 @@ protected function sendBatchAsync(ProceedingJoinPoint $proceedingJoinPoint) )->call($message); } - return tap($proceedingJoinPoint->process(), fn () => $span->finish()); + return tap($proceedingJoinPoint->process(), fn () => $span->setOrigin('auto.kafka')->finish()); } } diff --git a/src/sentry/src/Tracing/Aspect/RpcAspect.php b/src/sentry/src/Tracing/Aspect/RpcAspect.php index 0b894d8b4..383ffc75e 100644 --- a/src/sentry/src/Tracing/Aspect/RpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/RpcAspect.php @@ -145,8 +145,7 @@ private function handleSend(ProceedingJoinPoint $proceedingJoinPoint) throw $exception; } finally { - $span->setData($data); - $span->finish(); + $span->setOrigin('auto.rpc')->setData($data)->finish(); Context::destroy(static::SPAN); Context::destroy(static::DATA); diff --git a/src/sentry/src/Tracing/Listener/TracingAmqpListener.php b/src/sentry/src/Tracing/Listener/TracingAmqpListener.php index 4e2899dbf..b2ff1aa9f 100644 --- a/src/sentry/src/Tracing/Listener/TracingAmqpListener.php +++ b/src/sentry/src/Tracing/Listener/TracingAmqpListener.php @@ -122,10 +122,10 @@ protected function finishTransaction(AfterConsume|FailToConsume $event): void } } - $transaction->setData($data); - $transaction->setTags($tags); + $transaction->setOrigin('auto.amqp')->setData($data)->setTags($tags); SentrySdk::getCurrentHub()->setSpan($transaction); + $transaction->finish(microtime(true)); } } diff --git a/src/sentry/src/Tracing/Listener/TracingAsyncQueueListener.php b/src/sentry/src/Tracing/Listener/TracingAsyncQueueListener.php index d246e4723..2ee432aa8 100644 --- a/src/sentry/src/Tracing/Listener/TracingAsyncQueueListener.php +++ b/src/sentry/src/Tracing/Listener/TracingAsyncQueueListener.php @@ -110,10 +110,10 @@ protected function finishTransaction(AfterHandle|RetryHandle|FailedHandle $event } } - $transaction->setData($data); - $transaction->setTags($tags); + $transaction->setOrigin('auto.queue')->setData($data)->setTags($tags); SentrySdk::getCurrentHub()->setSpan($transaction); + $transaction->finish(microtime(true)); } } diff --git a/src/sentry/src/Tracing/Listener/TracingCommandListener.php b/src/sentry/src/Tracing/Listener/TracingCommandListener.php index 5a17f0fb6..9ecb47bf2 100644 --- a/src/sentry/src/Tracing/Listener/TracingCommandListener.php +++ b/src/sentry/src/Tracing/Listener/TracingCommandListener.php @@ -114,11 +114,14 @@ protected function finishTransaction(AfterExecute $event): void } } - $transaction->setStatus($exitCode == SymfonyCommand::SUCCESS ? SpanStatus::ok() : SpanStatus::internalError()); - $transaction->setData($data); - $transaction->setTags($tags); + $transaction + ->setOrigin('auto.command') + ->setStatus($exitCode == SymfonyCommand::SUCCESS ? SpanStatus::ok() : SpanStatus::internalError()) + ->setData($data) + ->setTags($tags); SentrySdk::getCurrentHub()->setSpan($transaction); + $transaction->finish(microtime(true)); } } diff --git a/src/sentry/src/Tracing/Listener/TracingCrontabListener.php b/src/sentry/src/Tracing/Listener/TracingCrontabListener.php index f4190de72..2361e3adc 100644 --- a/src/sentry/src/Tracing/Listener/TracingCrontabListener.php +++ b/src/sentry/src/Tracing/Listener/TracingCrontabListener.php @@ -97,10 +97,12 @@ protected function finishTransaction(AfterExecute|FailToExecute $event): void } } - $transaction->setData($data); - $transaction->setTags($tags); + $transaction->setOrigin('auto.crontab') + ->setData($data) + ->setTags($tags); SentrySdk::getCurrentHub()->setSpan($transaction); + $transaction->finish(microtime(true)); } } diff --git a/src/sentry/src/Tracing/Listener/TracingDbQueryListener.php b/src/sentry/src/Tracing/Listener/TracingDbQueryListener.php index cba5e5ea5..75d19d9a0 100644 --- a/src/sentry/src/Tracing/Listener/TracingDbQueryListener.php +++ b/src/sentry/src/Tracing/Listener/TracingDbQueryListener.php @@ -105,10 +105,10 @@ protected function queryExecutedHandler(object $event): void $description = $event->sql; // Already check in the previous context - /** @var \Sentry\Tracing\Span $span */ - $span = $this->startSpan($op, $description); - $span->setData($data); - $span->setStartTimestamp($startTimestamp); - $span->finish($startTimestamp + $event->time / 1000); + $this->startSpan($op, $description) + ->setOrigin('auto.db') + ->setData($data) + ->setStartTimestamp($startTimestamp) + ->finish($startTimestamp + $event->time / 1000); } } diff --git a/src/sentry/src/Tracing/Listener/TracingKafkaListener.php b/src/sentry/src/Tracing/Listener/TracingKafkaListener.php index a745fb3e2..e7ad48118 100644 --- a/src/sentry/src/Tracing/Listener/TracingKafkaListener.php +++ b/src/sentry/src/Tracing/Listener/TracingKafkaListener.php @@ -116,10 +116,12 @@ protected function finishTransaction(AfterConsume|FailToConsume $event): void } } - $transaction->setData($data); - $transaction->setTags($tags); + $transaction->setOrigin('auto.kafka') + ->setData($data) + ->setTags($tags); SentrySdk::getCurrentHub()->setSpan($transaction); + $transaction->finish(microtime(true)); } } diff --git a/src/sentry/src/Tracing/Listener/TracingRedisListener.php b/src/sentry/src/Tracing/Listener/TracingRedisListener.php index ae788b590..fd2095ab2 100644 --- a/src/sentry/src/Tracing/Listener/TracingRedisListener.php +++ b/src/sentry/src/Tracing/Listener/TracingRedisListener.php @@ -82,19 +82,20 @@ public function process(object $event): void } if ($exception = $event->throwable) { - $span->setStatus(SpanStatus::internalError()); - $span->setTags([ - 'error' => true, - 'exception.class' => $exception::class, - 'exception.message' => $exception->getMessage(), - 'exception.code' => $exception->getCode(), - ]); + $span->setStatus(SpanStatus::internalError()) + ->setTags([ + 'error' => true, + 'exception.class' => $exception::class, + 'exception.message' => $exception->getMessage(), + 'exception.code' => $exception->getCode(), + ]); if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { $data['exception.stack_trace'] = (string) $exception; } } - $span->setData($data); - $span->finish(); + $span->setOrigin('auto.redis') + ->setData($data) + ->finish(); } } diff --git a/src/sentry/src/Tracing/Listener/TracingRequestListener.php b/src/sentry/src/Tracing/Listener/TracingRequestListener.php index d9ed47642..3ff3c77c9 100644 --- a/src/sentry/src/Tracing/Listener/TracingRequestListener.php +++ b/src/sentry/src/Tracing/Listener/TracingRequestListener.php @@ -126,14 +126,16 @@ private function startTransaction(RequestReceived|RpcRequestReceived $event): vo $data['rpc.context'] = $this->container->get(RpcContext::class)->getData(); } - $transaction->setData($data); + $transaction->setOrigin('auto.request')->setData($data); $span = $this->startSpan('request.received', 'request.received', true); defer(function () use ($transaction, $span) { - $span->finish(); + $span?->setOrigin('auto.request') + ->finish(); SentrySdk::getCurrentHub()->setSpan($transaction); + $transaction->finish(); }); } @@ -160,16 +162,16 @@ private function setTraceIdAndException(RequestHandled|RpcRequestHandled $event) $transaction->setHttpStatus($event->response->getStatusCode()); if ($exception = $event->getThrowable()) { - $transaction->setStatus(SpanStatus::internalError()); - $transaction->setTags([ - 'error' => true, - 'exception.class' => $exception::class, - 'exception.code' => $exception->getCode(), - 'exception.message' => $exception->getMessage(), - ]); - $transaction->setData([ - 'exception.stack_trace' => (string) $exception, - ]); + $transaction->setStatus(SpanStatus::internalError()) + ->setTags([ + 'error' => true, + 'exception.class' => $exception::class, + 'exception.code' => $exception->getCode(), + 'exception.message' => $exception->getMessage(), + ]) + ->setData([ + 'exception.stack_trace' => (string) $exception, + ]); } } From 39f1bf7ca6d9ceafb43bb1ddfe676cfa995eede5 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 14:29:28 +0800 Subject: [PATCH 08/10] fix: simplify span error handling and ensure origin is set in RpcAspect --- src/sentry/src/Tracing/Aspect/RpcAspect.php | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/RpcAspect.php b/src/sentry/src/Tracing/Aspect/RpcAspect.php index 383ffc75e..7e8523a73 100644 --- a/src/sentry/src/Tracing/Aspect/RpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/RpcAspect.php @@ -124,28 +124,24 @@ private function handleSend(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); - if (! $span) { - return $result; - } - if ($this->switcher->isTracingExtraTagEnable('rpc.result')) { $data['rpc.result'] = $result; } } catch (Throwable $exception) { - $span->setStatus(SpanStatus::internalError()); - $span->setTags([ - 'error' => true, - 'exception.class' => $exception::class, - 'exception.message' => $exception->getMessage(), - 'exception.code' => $exception->getCode(), - ]); + $span?->setStatus(SpanStatus::internalError()) + ->setTags([ + 'error' => true, + 'exception.class' => $exception::class, + 'exception.message' => $exception->getMessage(), + 'exception.code' => $exception->getCode(), + ]); if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { $data['exception.stack_trace'] = (string) $exception; } throw $exception; } finally { - $span->setOrigin('auto.rpc')->setData($data)->finish(); + $span?->setOrigin('auto.rpc')->setData($data)->finish(); Context::destroy(static::SPAN); Context::destroy(static::DATA); From a834d667a20d846c63409f8b21f60c7a962dd695 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 14:30:04 +0800 Subject: [PATCH 09/10] fix: streamline span error handling in GrpcAspect for improved clarity --- src/sentry/src/Tracing/Aspect/GrpcAspect.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/GrpcAspect.php b/src/sentry/src/Tracing/Aspect/GrpcAspect.php index a57779773..9dce0f50a 100644 --- a/src/sentry/src/Tracing/Aspect/GrpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/GrpcAspect.php @@ -76,20 +76,20 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } } } catch (Throwable $exception) { - $span->setStatus(SpanStatus::internalError()); - $span->setTags([ - 'error' => true, - 'exception.class' => $exception::class, - 'exception.message' => $exception->getMessage(), - 'exception.code' => $exception->getCode(), - ]); + $span?->setStatus(SpanStatus::internalError()) + ->setTags([ + 'error' => true, + 'exception.class' => $exception::class, + 'exception.message' => $exception->getMessage(), + 'exception.code' => $exception->getCode(), + ]); if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { $data['exception.stack_trace'] = (string) $exception; } throw $exception; } finally { - $span->setOrigin('auto.grpc')->setData($data)->finish(); + $span?->setOrigin('auto.grpc')->setData($data)->finish(); } return $result; } From 0c50834cc8fb76b7d5ae43a34bba6f3dcd8af59f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 2 Sep 2025 14:31:18 +0800 Subject: [PATCH 10/10] fix: set origin for spans in KafkaProducerAspect for improved tracing context --- src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php b/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php index 4fd565581..41a544c8f 100644 --- a/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php +++ b/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php @@ -70,7 +70,7 @@ protected function sendAsync(ProceedingJoinPoint $proceedingJoinPoint) ->setValue($carrier); $proceedingJoinPoint->arguments['keys']['headers'] = $headers; - return tap($proceedingJoinPoint->process(), fn () => $span->finish()); + return tap($proceedingJoinPoint->process(), fn () => $span->setOrigin('auto.kafka')->finish()); } protected function sendBatchAsync(ProceedingJoinPoint $proceedingJoinPoint)