From 457f497e1080ff8aedef374ac944291b55d8e2a3 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 22:43:09 +0800 Subject: [PATCH 01/18] refactor(sentry): improve span lifecycle management and code clarity - Remove manual timestamp passing to span.finish() method, letting Sentry handle timing automatically - Improve span parent/child relationship management in TraceAnnotationAspect - Clean up commented code and improve readability in DbAspect - Simplify transaction creation flow in SpanStarter with better structure - Add proper comments for span restoration and transaction finishing - Ensure consistent span finishing patterns across all aspects and listeners This refactoring improves the reliability of span timing and makes the code more maintainable. --- .../src/Tracing/Aspect/CoordinatorAspect.php | 2 +- src/sentry/src/Tracing/Aspect/DbAspect.php | 6 --- .../Tracing/Aspect/TraceAnnotationAspect.php | 8 +-- .../Tracing/Listener/EventHandleListener.php | 13 +++-- src/sentry/src/Tracing/SpanStarter.php | 54 +++++++++---------- 5 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php b/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php index 921294829..890c72268 100644 --- a/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php @@ -64,7 +64,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } throw $exception; } finally { - $span?->finish(microtime(true)); + $span?->finish(); } } } diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index e27362352..e45ccf362 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -70,12 +70,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $table = $sqlParse['table']; $operation = $sqlParse['operation']; - // rule: operation db.table - // $op = sprintf( - // '%s%s', - // $operation ? $operation . ' ' : '', - // implode('.', array_filter([$database, $table])) - // ); $span = $this->startSpan( op: 'db.sql.query', description: $sql, diff --git a/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php b/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php index cb08ee720..329b7f313 100644 --- a/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php +++ b/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php @@ -69,6 +69,8 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) if ($this->switcher->isTracingExtraTagEnable('annotation.result')) { $span?->setData(['annotation.result' => $result]); } + + return $result; } catch (Throwable $exception) { $span?->setStatus(SpanStatus::internalError()) ->setTags([ @@ -82,12 +84,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } throw $exception; } finally { - $span?->finish(microtime(true)); + $span?->finish(); - // Reset root span + // Restore parent span SentrySdk::getCurrentHub()->setSpan($parent); } - - return $result; } } diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index d8a98ca9d..6cbb0d04a 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -268,10 +268,13 @@ private function handleRequestReceived(HttpEvent\RequestReceived|RpcEvent\Reques ); defer(function () use ($transaction, $span) { + // Make sure the span is finished after the request is handled $span?->finish(); + // Make sure the transaction is finished after the request is handled SentrySdk::getCurrentHub()->setSpan($transaction); + // Finish transaction $transaction->finish(); }); } @@ -373,7 +376,7 @@ private function handleCommandFinished(CommandEvent\AfterExecute $event): void SentrySdk::getCurrentHub()->setSpan($transaction); - $transaction->finish(microtime(true)); + $transaction->finish(); } private function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event): void @@ -479,7 +482,7 @@ private function handleCrontabTaskFinished(CrontabEvent\FailToExecute|CrontabEve SentrySdk::getCurrentHub()->setSpan($transaction); - $transaction->finish(microtime(true)); + $transaction->finish(); } private function handleAmqpMessageProcessing(AmqpEvent\BeforeConsume $event): void @@ -557,7 +560,7 @@ private function handleAmqpMessageProcessed(AmqpEvent\AfterConsume|AmqpEvent\Fai SentrySdk::getCurrentHub()->setSpan($transaction); - $transaction->finish(microtime(true)); + $transaction->finish(); } private function handleKafkaMessageProcessing(KafkaEvent\BeforeConsume $event): void @@ -629,7 +632,7 @@ private function handleKafkaMessageProcessed(KafkaEvent\AfterConsume|KafkaEvent\ SentrySdk::getCurrentHub()->setSpan($transaction); - $transaction->finish(microtime(true)); + $transaction->finish(); } private function handleAsyncQueueJobProcessing(AsyncQueueEvent\BeforeHandle $event): void @@ -688,7 +691,7 @@ private function handleAsyncQueueJobProcessed(AsyncQueueEvent\AfterHandle|AsyncQ SentrySdk::getCurrentHub()->setSpan($transaction); - $transaction->finish(microtime(true)); + $transaction->finish(); } private function parseRoute(Dispatched $dispatched): array diff --git a/src/sentry/src/Tracing/SpanStarter.php b/src/sentry/src/Tracing/SpanStarter.php index 3fd0a7396..fad2f3398 100644 --- a/src/sentry/src/Tracing/SpanStarter.php +++ b/src/sentry/src/Tracing/SpanStarter.php @@ -22,7 +22,6 @@ 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; @@ -40,13 +39,14 @@ protected function startSpan( return null; } + $spanContext = SpanContext::make()->setOp($op) + ->setDescription($description) + ->setOrigin($origin) + ->setStatus(SpanStatus::ok()) + ->setStartTimestamp(microtime(true)); + return tap( - $parent->startChild(new SpanContext()) - ->setOp($op) - ->setDescription($description) - ->setOrigin($origin) - ->setStatus(SpanStatus::ok()) - ->setStartTimestamp(microtime(true)), + $parent->startChild($spanContext), fn (Span $span) => $asParent && SentrySdk::getCurrentHub()->setSpan($span) ); } @@ -87,30 +87,28 @@ protected function continueTrace(string $sentryTrace = '', string $baggage = '', tap(clone SentrySdk::getCurrentHub(), fn (HubInterface $hub) => $hub->pushScope()) ); - $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' => $context->setSource( - $value instanceof TransactionSource ? $value : TransactionSource::custom() - ), - default => null, - }; - }); - } - ) - ) + // Build transaction context + $transactionContext = continueTrace($sentryTrace, $baggage) ->setStartTimestamp(microtime(true)) ->setStatus(SpanStatus::ok()); - $hub->setSpan($transaction); + // Set additional options + array_walk($options, function ($value, $key) use ($transactionContext) { + match ($key) { + 'name' => is_string($value) && $transactionContext->setName($value), + 'op' => is_string($value) && $transactionContext->setOp($value), + 'description' => is_string($value) && $transactionContext->setDescription($value), + 'origin' => is_string($value) && $transactionContext->setOrigin($value), + 'source' => $transactionContext->setSource( + $value instanceof TransactionSource ? $value : TransactionSource::custom() + ), + default => null, + }; + }); - return $transaction; + return tap( + $hub->startTransaction($transactionContext), + fn ($transaction) => $hub->setSpan($transaction) + ); } } From 8204c8d6708f05485a2ba8d9fd33081d8b802542 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:02:22 +0800 Subject: [PATCH 02/18] feat(sentry): enhance database event handling with transaction support and SQL binding options --- src/sentry/publish/sentry.php | 1 + src/sentry/src/Switcher.php | 20 ++--- .../Tracing/Listener/EventHandleListener.php | 75 ++++++++++++++++--- 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index e812aa95e..237aa6794 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -113,6 +113,7 @@ 'exception.stack_trace' => true, 'amqp.result' => false, 'annotation.result' => false, + 'db.sql.bindings' => true, 'db.result' => false, 'elasticsearch.result' => false, 'http.response.body.contents' => false, diff --git a/src/sentry/src/Switcher.php b/src/sentry/src/Switcher.php index 013e9524c..b062b0725 100644 --- a/src/sentry/src/Switcher.php +++ b/src/sentry/src/Switcher.php @@ -22,37 +22,37 @@ public function __construct(protected ConfigInterface $config) { } - public function isEnable(string $key): bool + public function isEnable(string $key, bool $default = true): bool { - return (bool) $this->config->get('sentry.enable.' . $key, true); + return (bool) $this->config->get('sentry.enable.' . $key, $default); } - public function isBreadcrumbEnable(string $key): bool + public function isBreadcrumbEnable(string $key, bool $default = true): bool { - return (bool) $this->config->get('sentry.breadcrumbs.' . $key, true); + return (bool) $this->config->get('sentry.breadcrumbs.' . $key, $default); } - public function isTracingEnable(string $key): bool + public function isTracingEnable(string $key, bool $default = true): bool { if (! $this->config->get('sentry.enable_tracing', true)) { return false; } - return (bool) $this->config->get('sentry.tracing.enable.' . $key, true); + return (bool) $this->config->get('sentry.tracing.enable.' . $key, $default); } - public function isTracingSpanEnable(string $key): bool + public function isTracingSpanEnable(string $key, bool $default = true): bool { if (! SentrySdk::getCurrentHub()->getSpan()) { return false; } - return (bool) $this->config->get('sentry.tracing.spans.' . $key, true); + return (bool) $this->config->get('sentry.tracing.spans.' . $key, $default); } - public function isTracingExtraTagEnable(string $key): bool + public function isTracingExtraTagEnable(string $key, bool $default = false): bool { - return (bool) ($this->config->get('sentry.tracing.extra_tags', [])[$key] ?? false); + return (bool) ($this->config->get('sentry.tracing.extra_tags', [])[$key] ?? $default); } public function isExceptionIgnored(string|Throwable $exception): bool diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 6cbb0d04a..a2009a29d 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -26,7 +26,7 @@ use Hyperf\Contract\ConfigInterface; use Hyperf\Coroutine\Coroutine; use Hyperf\Crontab\Event as CrontabEvent; -use Hyperf\Database\Events\QueryExecuted; +use Hyperf\Database\Events as DbEvent; use Hyperf\DbConnection\Pool\PoolFactory; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\HttpServer\Event as HttpEvent; @@ -73,7 +73,10 @@ public function listen(): array { return [ // Database events - QueryExecuted::class, + DbEvent\QueryExecuted::class, + DbEvent\TransactionBeginning::class, + DbEvent\TransactionCommitted::class, + DbEvent\TransactionRolledBack::class, // Request events HttpEvent\RequestReceived::class, @@ -115,7 +118,10 @@ public function process(object $event): void { match ($event::class) { // Database - QueryExecuted::class => $this->handleDbQueryExecuted($event), + DbEvent\QueryExecuted::class => $this->handleDbQueryExecuted($event), + DbEvent\TransactionBeginning::class => $this->handleDbTransactionBeginning($event), + DbEvent\TransactionCommitted::class => $this->handleDbTransactionCommitted($event), + DbEvent\TransactionRolledBack::class => $this->handleDbTransactionRolledBack($event), // Request HttpEvent\RequestReceived::class, @@ -155,7 +161,7 @@ public function process(object $event): void }; } - private function handleDbQueryExecuted(QueryExecuted $event): void + private function handleDbQueryExecuted(DbEvent\QueryExecuted $event): void { if (! $this->switcher->isTracingSpanEnable('sql_queries')) { return; @@ -188,23 +194,72 @@ private function handleDbQueryExecuted(QueryExecuted $event): void 'db.pool.max_idle_time' => $pool->getOption()->getMaxIdleTime(), 'db.pool.idle' => $pool->getConnectionsInChannel(), 'db.pool.using' => $pool->getCurrentConnections(), - 'db.sql.bindings' => $event->bindings, ]; - $startTimestamp = microtime(true) - $event->time / 1000; + if ($this->switcher->isTracingExtraTagEnable('db.sql.bindings', true)) { + $data += ['db.sql.bindings' => $event->bindings]; + } - $op = 'db.sql.query'; - $description = $event->sql; + $startTimestamp = microtime(true) - $event->time / 1000; $this->startSpan( - op: $op, - description: $description, + op: 'db.sql.query', + description: $event->sql, origin: 'auto.db' )?->setData($data) ->setStartTimestamp($startTimestamp) ->finish($startTimestamp + $event->time / 1000); } + private function handleDbTransactionBeginning(DbEvent\TransactionBeginning $event): void + { + if (! $this->switcher->isTracingSpanEnable('sql_transactions')) { + return; + } + if (! SentrySdk::getCurrentHub()->getSpan()) { + return; + } + + $this->startSpan( + op: 'db.transaction', + description: 'BEGIN', + origin: 'auto.db' + )->setData([ + 'coroutine.id' => Coroutine::id(), + 'db.system' => $event->connection->getDriverName(), + 'db.name' => $event->connection->getDatabaseName(), + 'db.pool.name' => $event->connectionName, + ]); + } + + private function handleDbTransactionCommitted(DbEvent\TransactionCommitted $event): void + { + if (! $this->switcher->isTracingSpanEnable('sql_transactions')) { + return; + } + if (! $span = SentrySdk::getCurrentHub()->getSpan()) { + return; + } + + $span->setStatus(SpanStatus::ok()) + ->setDescription('COMMIT') + ->finish(); + } + + private function handleDbTransactionRolledBack(DbEvent\TransactionRolledBack $event): void + { + if (! $this->switcher->isTracingSpanEnable('sql_transactions')) { + return; + } + if (! $span = SentrySdk::getCurrentHub()->getSpan()) { + return; + } + + $span->setStatus(SpanStatus::internalError()) + ->setDescription('ROLLBACK') + ->finish(); + } + private function handleRequestReceived(HttpEvent\RequestReceived|RpcEvent\RequestReceived $event): void { if (! $this->switcher->isTracingEnable('request')) { From 92e052b8208c8b504d4d098c7e26924f016aea90 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:04:39 +0800 Subject: [PATCH 03/18] feat(sentry): add conditional handling for SQL bindings in DbAspect and EventHandleListener --- src/sentry/src/Tracing/Aspect/DbAspect.php | 5 ++++- src/sentry/src/Tracing/Listener/EventHandleListener.php | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index e45ccf362..dddfd0e89 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -89,9 +89,12 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'db.pool.using' => $pool->getCurrentConnections(), // 'server.host' => '', // 'server.port' => '', - 'db.sql.bindings' => $arguments['arguments']['bindings'] ?? [], ]; + if ($this->switcher->isTracingExtraTagEnable('db.sql.bindings', true)) { + $data['db.sql.bindings'] = $arguments['arguments']['bindings'] ?? []; + } + foreach ($arguments['arguments']['bindings'] as $key => $value) { $data['db.parameter.' . $key] = $value; } diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index a2009a29d..7dd020a2b 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -183,9 +183,6 @@ private function handleDbQueryExecuted(DbEvent\QueryExecuted $event): void if (! empty($sqlParse['table'])) { $data['db.collection.name'] = $sqlParse['table']; } - foreach ($event->bindings as $key => $value) { - $data['db.parameter.' . $key] = $value; - } $pool = $this->container->get(PoolFactory::class)->getPool($event->connectionName); $data += [ @@ -197,7 +194,10 @@ private function handleDbQueryExecuted(DbEvent\QueryExecuted $event): void ]; if ($this->switcher->isTracingExtraTagEnable('db.sql.bindings', true)) { - $data += ['db.sql.bindings' => $event->bindings]; + $data['db.sql.bindings'] = $event->bindings; + foreach ($event->bindings as $key => $value) { + $data['db.parameter.' . $key] = $value; + } } $startTimestamp = microtime(true) - $event->time / 1000; From 5a4c1443053184b18197bd24c48fefe0df74161c Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:04:53 +0800 Subject: [PATCH 04/18] fix(sentry): correct data assignment for SQL bindings in DbAspect --- src/sentry/src/Tracing/Aspect/DbAspect.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index dddfd0e89..561b35d0e 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -93,10 +93,10 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) if ($this->switcher->isTracingExtraTagEnable('db.sql.bindings', true)) { $data['db.sql.bindings'] = $arguments['arguments']['bindings'] ?? []; - } - foreach ($arguments['arguments']['bindings'] as $key => $value) { - $data['db.parameter.' . $key] = $value; + foreach ($arguments['arguments']['bindings'] as $key => $value) { + $data['db.parameter.' . $key] = $value; + } } $span?->setData($data); From e059f40a589453fc4192c2451f9639e7d01aa61c Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:05:02 +0800 Subject: [PATCH 05/18] fix(sentry): remove redundant line in SQL bindings data assignment in DbAspect --- src/sentry/src/Tracing/Aspect/DbAspect.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index 561b35d0e..dd987ca40 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -93,7 +93,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) if ($this->switcher->isTracingExtraTagEnable('db.sql.bindings', true)) { $data['db.sql.bindings'] = $arguments['arguments']['bindings'] ?? []; - foreach ($arguments['arguments']['bindings'] as $key => $value) { $data['db.parameter.' . $key] = $value; } From 9faeb94d45621717f33e3ad08735f3b72b2baad7 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:30:26 +0800 Subject: [PATCH 06/18] fix(sentry): improve conditional checks and data assignment in tracing aspects --- .../src/Tracing/Aspect/CoroutineAspect.php | 13 ++++---- .../Tracing/Aspect/ElasticsearchAspect.php | 1 - src/sentry/src/Tracing/Aspect/GrpcAspect.php | 31 ++++++++++++------- .../Tracing/Aspect/TraceAnnotationAspect.php | 3 +- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index 9f475af0d..88a1a86b6 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -39,7 +39,10 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingSpanEnable('coroutine') || Switcher::isDisableCoroutineTracing()) { + if ( + ! $this->switcher->isTracingSpanEnable('coroutine') + || Switcher::isDisableCoroutineTracing() + ) { return $proceedingJoinPoint->process(); } @@ -60,9 +63,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return $proceedingJoinPoint->process(); } - $parent->setData([ - 'coroutine.id' => Co::id(), - ]); + $parent->setData(['coroutine.id' => Co::id()]); $proceedingJoinPoint->arguments['keys']['callable'] = function () use ($callable, $parent, $callingOnFunction) { $transaction = $this->startCoroutineTransaction( @@ -71,9 +72,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) op: 'coroutine.execute', description: $callingOnFunction, origin: 'auto.coroutine', - )->setData([ - 'coroutine.id' => Co::id(), - ]); + )->setData(['coroutine.id' => Co::id()]); defer(function () use ($transaction) { SentrySdk::getCurrentHub()->setSpan($transaction); diff --git a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php index d480d40d3..b870c1187 100644 --- a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php +++ b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php @@ -62,7 +62,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return $proceedingJoinPoint->process(); } - // TODO 规则: operate dbName.tableName $span = $this->startSpan( op: 'db.elasticsearch', description: sprintf('%s::%s()', $proceedingJoinPoint->className, $proceedingJoinPoint->methodName), diff --git a/src/sentry/src/Tracing/Aspect/GrpcAspect.php b/src/sentry/src/Tracing/Aspect/GrpcAspect.php index 3bb765d39..dc2dbabe9 100644 --- a/src/sentry/src/Tracing/Aspect/GrpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/GrpcAspect.php @@ -40,21 +40,32 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return $proceedingJoinPoint->process(); } + $method = $proceedingJoinPoint->arguments['keys']['method']; $options = $proceedingJoinPoint->arguments['keys']['options']; $data = [ - 'grpc.method' => $method = $proceedingJoinPoint->arguments['keys']['method'], + 'grpc.method' => $method, 'grpc.options' => $options, 'coroutine.id' => Coroutine::id(), ]; $parent = SentrySdk::getCurrentHub()->getSpan(); + + // No parent span or not sampled, skip tracing + if (! $parent || ! $parent->getSampled()) { + return $proceedingJoinPoint->process(); + } + + // Inject sentry-trace header for distributed tracing $options['headers'] = $options['headers'] ?? [] + [ 'sentry-trace' => $parent->toTraceparent(), 'baggage' => $parent->toBaggage(), 'traceparent' => $parent->toW3CTraceparent(), ]; + + // Inject tracing headers $proceedingJoinPoint->arguments['keys']['options'] = $options; + // Start gRPC client span $span = $this->startSpan( op: 'grpc.client', description: $method, @@ -64,22 +75,22 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); - if (! $span) { - return $result; - } - [$message, $code, $response] = $result; if ($response instanceof Http2Response) { - $span->setData([ + $span?->setData([ 'response.status' => $code, 'response.reason' => $message, 'response.headers' => $response->headers, ]); - $this->switcher->isTracingExtraTagEnable('response.body') && $span->setData([ - 'response.body' => $response->data, - ]); + if ($this->switcher->isTracingExtraTagEnable('response.body')) { + $span?->setData([ + 'response.body' => $response->data, + ]); + } } + + return $result; } catch (Throwable $exception) { $span?->setStatus(SpanStatus::internalError()) ->setTags([ @@ -98,7 +109,5 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } finally { $span?->finish(); } - - return $result; } } diff --git a/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php b/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php index 329b7f313..96fcbaafa 100644 --- a/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php +++ b/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php @@ -38,8 +38,9 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $metadata = $proceedingJoinPoint->getAnnotationMetadata(); /** @var null|Trace $annotation */ $annotation = $metadata->method[Trace::class] ?? null; + $parent = SentrySdk::getCurrentHub()->getSpan(); - if (! $annotation || ! $parent = SentrySdk::getCurrentHub()->getSpan()) { + if (! $annotation || ! $parent || ! $parent->getSampled()) { return $proceedingJoinPoint->process(); } From 1e53c41d4b71179f3ac2acfacd430c25b0018232 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:32:23 +0800 Subject: [PATCH 07/18] fix(sentry): inject additional trace context headers in GuzzleHttpClientAspect --- src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php index d5896819d..b358c1347 100644 --- a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php +++ b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php @@ -76,18 +76,19 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) description: $request->getMethod() . ' ' . (string) $request->getUri(), origin: 'auto.http.client', ); + // Inject trace context $options['headers'] = array_replace($options['headers'] ?? [], [ 'sentry-trace' => $span->toTraceparent(), 'baggage' => $span->toBaggage(), 'traceparent' => $span->toW3CTraceparent(), ]); - // Override the headers - $proceedingJoinPoint->arguments['keys']['options']['headers'] = $options['headers']; if (! $span) { return $proceedingJoinPoint->process(); } + // Override the headers + $proceedingJoinPoint->arguments['keys']['options']['headers'] = $options['headers']; $onStats = $options['on_stats'] ?? null; // Add or override the on_stats option to record the request duration. From 8955190bdfe1151f9784e96954e9df5c426749e7 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:34:13 +0800 Subject: [PATCH 08/18] fix(sentry): rename span name for batch sending in KafkaProducerAspect --- 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 038548462..cd0f9d263 100644 --- a/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php +++ b/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php @@ -98,7 +98,7 @@ protected function sendBatchAsync(ProceedingJoinPoint $proceedingJoinPoint) /** @var ProduceMessage[] $messages */ $messages = $proceedingJoinPoint->arguments['keys']['messages'] ?? []; $span = $this->startSpan( - 'kafka.send_batch', + 'queue.publish', sprintf('%s::%s', $proceedingJoinPoint->className, $proceedingJoinPoint->methodName), origin: 'auto.kafka' ); From 1f6ff3d322865980758d66d00ff8de49028c5b93 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:35:15 +0800 Subject: [PATCH 09/18] fix(sentry): simplify span result handling in RedisAspect --- src/sentry/src/Tracing/Aspect/RedisAspect.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/RedisAspect.php b/src/sentry/src/Tracing/Aspect/RedisAspect.php index 667d6149a..46c6ee6c0 100644 --- a/src/sentry/src/Tracing/Aspect/RedisAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisAspect.php @@ -90,13 +90,11 @@ class_exists(CommandExecuted::class) try { $result = $proceedingJoinPoint->process(); - if (! $span) { - return $result; - } - if ($this->switcher->isTracingExtraTagEnable('redis.result')) { - $span->setData(['redis.result' => $result]); + $span?->setData(['redis.result' => $result]); } + + return $result; } catch (Throwable $e) { $span?->setStatus(SpanStatus::internalError()) ->setTags([ @@ -106,14 +104,12 @@ class_exists(CommandExecuted::class) 'exception.code' => (string) $e->getCode(), ]); if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { - $span->setData(['exception.stack_trace' => (string) $e]); + $span?->setData(['exception.stack_trace' => (string) $e]); } throw $e; } finally { $span?->finish(); } - - return $result; } } From e57e28601536e146a18d865f635ce9d43b600d53 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:35:39 +0800 Subject: [PATCH 10/18] fix(sentry): ensure result is returned in RpcAspect processing --- src/sentry/src/Tracing/Aspect/RpcAspect.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/RpcAspect.php b/src/sentry/src/Tracing/Aspect/RpcAspect.php index 2a4b025f4..c1d1e4fbb 100644 --- a/src/sentry/src/Tracing/Aspect/RpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/RpcAspect.php @@ -132,6 +132,8 @@ private function handleSend(ProceedingJoinPoint $proceedingJoinPoint) if ($this->switcher->isTracingExtraTagEnable('rpc.result')) { $span?->setData(['rpc.result' => $result]); } + + return $result; } catch (Throwable $exception) { $span?->setStatus(SpanStatus::internalError()) ->setTags([ @@ -151,7 +153,5 @@ private function handleSend(ProceedingJoinPoint $proceedingJoinPoint) Context::destroy(static::SPAN); Context::destroy(static::DATA); } - - return $result; } } From 9ce7c5abdc4d1a960a5740570baf8fb729f1029d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:59:35 +0800 Subject: [PATCH 11/18] fix(sentry): enhance command event handling with breadcrumbs and scope configuration --- .../src/Listener/EventHandleListener.php | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/sentry/src/Listener/EventHandleListener.php b/src/sentry/src/Listener/EventHandleListener.php index 25561ceb4..cae031f7f 100644 --- a/src/sentry/src/Listener/EventHandleListener.php +++ b/src/sentry/src/Listener/EventHandleListener.php @@ -33,8 +33,15 @@ use Psr\Container\ContainerInterface; use Sentry\Breadcrumb; use Sentry\SentrySdk; +use Sentry\State\Scope; +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Input\InputInterface; use Throwable; +/** + * @property InputInterface $input + * @property int $exitCode + */ class EventHandleListener implements ListenerInterface { public const HUB = 'sentry.context.hub'; @@ -156,7 +163,6 @@ public function process(object $event): void // Command events CommandEvent\BeforeHandle::class => $this->handleCommandStarting($event), - CommandEvent\FailToHandle::class => $this->handleCommandFailed($event), CommandEvent\AfterExecute::class => $this->handleCommandFinished($event), // Async Queue events @@ -377,19 +383,32 @@ protected function handleCommandStarting(object $event): void } $this->setupSentrySdk(); + + Integration::addBreadcrumb(new Breadcrumb( + Breadcrumb::LEVEL_INFO, + Breadcrumb::TYPE_DEFAULT, + 'command', + 'Starting command: ' . $event->getCommand()->getName(), + [ + 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), + ] + )); + + Integration::configureScope(static function (Scope $scope) use ($event): void { + $scope->setTag('command', $event->getCommand()->getName()); + }); } /** - * @param CommandEvent\FailToHandle $event + * Extract the command input arguments if possible. */ - protected function handleCommandFailed(object $event): void + protected function extractConsoleCommandInput(?InputInterface $input): ?string { - if (! $this->switcher->isEnable('command')) { - return; + if ($input instanceof ArgvInput) { + return (string) $input; } - $this->captureException($event->getThrowable()); - $this->flushEvents(); + return null; } /** @@ -401,7 +420,23 @@ protected function handleCommandFinished(object $event): void return; } + $this->captureException($event->getThrowable()); $this->flushEvents(); + + Integration::addBreadcrumb(new Breadcrumb( + Breadcrumb::LEVEL_INFO, + Breadcrumb::TYPE_DEFAULT, + 'command', + 'Finished command: ' . $event->getCommand()->getName(), + [ + 'exit' => (fn () => $this->exitCode)->call($event->getCommand()), + 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), + ] + )); + + Integration::configureScope(static function (Scope $scope): void { + $scope->removeTag('command'); + }); } /** From e4ff4bba4a2aaa83569c50daec85f2caec354400 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:09:25 +0800 Subject: [PATCH 12/18] fix(sentry): add configuration options for breadcrumbs in async queue and command events --- src/sentry/publish/sentry.php | 2 + .../src/Listener/EventHandleListener.php | 81 ++++++++++++------- 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index 237aa6794..3848a4df9 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -59,7 +59,9 @@ ], 'breadcrumbs' => [ + 'async_queue' => env('SENTRY_BREADCRUMBS_ASYNC_QUEUE', true), 'cache' => env('SENTRY_BREADCRUMBS_CACHE', true), + 'command' => env('SENTRY_BREADCRUMBS_COMMAND', true), 'sql_queries' => env('SENTRY_BREADCRUMBS_SQL_QUERIES', true), 'sql_bindings' => env('SENTRY_BREADCRUMBS_SQL_BINDINGS', true), 'sql_transaction' => env('SENTRY_BREADCRUMBS_SQL_TRANSACTION', true), diff --git a/src/sentry/src/Listener/EventHandleListener.php b/src/sentry/src/Listener/EventHandleListener.php index cae031f7f..61b4261df 100644 --- a/src/sentry/src/Listener/EventHandleListener.php +++ b/src/sentry/src/Listener/EventHandleListener.php @@ -384,31 +384,21 @@ protected function handleCommandStarting(object $event): void $this->setupSentrySdk(); - Integration::addBreadcrumb(new Breadcrumb( - Breadcrumb::LEVEL_INFO, - Breadcrumb::TYPE_DEFAULT, - 'command', - 'Starting command: ' . $event->getCommand()->getName(), - [ - 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), - ] - )); - Integration::configureScope(static function (Scope $scope) use ($event): void { $scope->setTag('command', $event->getCommand()->getName()); }); - } - /** - * Extract the command input arguments if possible. - */ - protected function extractConsoleCommandInput(?InputInterface $input): ?string - { - if ($input instanceof ArgvInput) { - return (string) $input; + if ($this->switcher->isBreadcrumbEnable('command')) { + Integration::addBreadcrumb(new Breadcrumb( + Breadcrumb::LEVEL_INFO, + Breadcrumb::TYPE_DEFAULT, + 'command', + 'Starting command: ' . $event->getCommand()->getName(), + [ + 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), + ] + )); } - - return null; } /** @@ -420,20 +410,22 @@ protected function handleCommandFinished(object $event): void return; } + if ($this->switcher->isBreadcrumbEnable('command')) { + Integration::addBreadcrumb(new Breadcrumb( + Breadcrumb::LEVEL_INFO, + Breadcrumb::TYPE_DEFAULT, + 'command', + 'Finished command: ' . $event->getCommand()->getName(), + [ + 'exit' => (fn () => $this->exitCode)->call($event->getCommand()), + 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), + ] + )); + } + $this->captureException($event->getThrowable()); $this->flushEvents(); - Integration::addBreadcrumb(new Breadcrumb( - Breadcrumb::LEVEL_INFO, - Breadcrumb::TYPE_DEFAULT, - 'command', - 'Finished command: ' . $event->getCommand()->getName(), - [ - 'exit' => (fn () => $this->exitCode)->call($event->getCommand()), - 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), - ] - )); - Integration::configureScope(static function (Scope $scope): void { $scope->removeTag('command'); }); @@ -449,6 +441,21 @@ protected function handleAsyncQueueJobProcessing(object $event): void } $this->setupSentrySdk(); + + if ($this->switcher->isBreadcrumbEnable('async_queue')) { + $job = [ + 'job' => $event->getMessage()->job()::class, + 'attempts' => $event->getMessage()->getAttempts(), + ]; + + Integration::addBreadcrumb(new Breadcrumb( + Breadcrumb::LEVEL_INFO, + Breadcrumb::TYPE_DEFAULT, + 'queue.job', + 'Processing async_queue job', + $job + )); + } } /** @@ -586,4 +593,16 @@ protected function handleKafkaMessageFailed(object $event): void $this->captureException($event->getThrowable()); $this->flushEvents(); } + + /** + * Extract the command input arguments if possible. + */ + private function extractConsoleCommandInput(?InputInterface $input): ?string + { + if ($input instanceof ArgvInput) { + return (string) $input; + } + + return null; + } } From 98cb5384285a25414b0d4622767c965836a4adab Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:16:53 +0800 Subject: [PATCH 13/18] fix(sentry): improve header injection for distributed tracing in GrpcAspect and add nullsafe operator in EventHandleListener --- src/sentry/src/Tracing/Aspect/GrpcAspect.php | 2 +- src/sentry/src/Tracing/Listener/EventHandleListener.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/GrpcAspect.php b/src/sentry/src/Tracing/Aspect/GrpcAspect.php index dc2dbabe9..cc83ad258 100644 --- a/src/sentry/src/Tracing/Aspect/GrpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/GrpcAspect.php @@ -56,7 +56,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) } // Inject sentry-trace header for distributed tracing - $options['headers'] = $options['headers'] ?? [] + [ + $options['headers'] = ($options['headers'] ?? []) + [ 'sentry-trace' => $parent->toTraceparent(), 'baggage' => $parent->toBaggage(), 'traceparent' => $parent->toW3CTraceparent(), diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 7dd020a2b..8464a8282 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -224,7 +224,7 @@ private function handleDbTransactionBeginning(DbEvent\TransactionBeginning $even op: 'db.transaction', description: 'BEGIN', origin: 'auto.db' - )->setData([ + )?->setData([ 'coroutine.id' => Coroutine::id(), 'db.system' => $event->connection->getDriverName(), 'db.name' => $event->connection->getDatabaseName(), From 8013f32a298443c7ad4f03ac20582797da9589a4 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:18:19 +0800 Subject: [PATCH 14/18] fix(sentry): ensure span is valid before injecting trace context in GuzzleHttpClientAspect --- src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php index b358c1347..abbec732d 100644 --- a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php +++ b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php @@ -76,6 +76,11 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) description: $request->getMethod() . ' ' . (string) $request->getUri(), origin: 'auto.http.client', ); + + if (! $span) { + return $proceedingJoinPoint->process(); + } + // Inject trace context $options['headers'] = array_replace($options['headers'] ?? [], [ 'sentry-trace' => $span->toTraceparent(), @@ -83,10 +88,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'traceparent' => $span->toW3CTraceparent(), ]); - if (! $span) { - return $proceedingJoinPoint->process(); - } - // Override the headers $proceedingJoinPoint->arguments['keys']['options']['headers'] = $options['headers']; $onStats = $options['on_stats'] ?? null; From 8d1b5bb989bda00824cff56e807620185f424608 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:25:23 +0800 Subject: [PATCH 15/18] fix(sentry): enhance command breadcrumb logging with input and exit code details --- .../src/Listener/EventHandleListener.php | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/sentry/src/Listener/EventHandleListener.php b/src/sentry/src/Listener/EventHandleListener.php index 61b4261df..2a909bb73 100644 --- a/src/sentry/src/Listener/EventHandleListener.php +++ b/src/sentry/src/Listener/EventHandleListener.php @@ -389,14 +389,19 @@ protected function handleCommandStarting(object $event): void }); if ($this->switcher->isBreadcrumbEnable('command')) { + $data = []; + if ($this->switcher->isBreadcrumbEnable('command_input')) { + /** @var InputInterface $input */ + $input = (fn () => $this->input)->call($event->getCommand()); + $data['input'] = $this->extractConsoleCommandInput($input); + } + Integration::addBreadcrumb(new Breadcrumb( Breadcrumb::LEVEL_INFO, Breadcrumb::TYPE_DEFAULT, 'command', 'Starting command: ' . $event->getCommand()->getName(), - [ - 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), - ] + $data )); } } @@ -411,15 +416,26 @@ protected function handleCommandFinished(object $event): void } if ($this->switcher->isBreadcrumbEnable('command')) { + /** @var InputInterface $input */ + /** @var int $exitCode */ + [$input, $exitCode] = (function () { + return [ + $this->input, + $this->exitCode, + ]; + })->call($event->getCommand()); + $data = ['exit' => $exitCode]; + + if ($this->switcher->isBreadcrumbEnable('command_input')) { + $data['input'] = $this->extractConsoleCommandInput($input); + } + Integration::addBreadcrumb(new Breadcrumb( Breadcrumb::LEVEL_INFO, Breadcrumb::TYPE_DEFAULT, 'command', 'Finished command: ' . $event->getCommand()->getName(), - [ - 'exit' => (fn () => $this->exitCode)->call($event->getCommand()), - 'input' => $this->extractConsoleCommandInput((fn () => $this->input)->call($event->getCommand())), - ] + $data )); } From 29f58b57d3a18740130ef93a83e69f3e7e3066c6 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:25:49 +0800 Subject: [PATCH 16/18] fix(sentry): add command input option for breadcrumbs configuration --- src/sentry/publish/sentry.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sentry/publish/sentry.php b/src/sentry/publish/sentry.php index 3848a4df9..a6069a138 100644 --- a/src/sentry/publish/sentry.php +++ b/src/sentry/publish/sentry.php @@ -62,6 +62,7 @@ 'async_queue' => env('SENTRY_BREADCRUMBS_ASYNC_QUEUE', true), 'cache' => env('SENTRY_BREADCRUMBS_CACHE', true), 'command' => env('SENTRY_BREADCRUMBS_COMMAND', true), + 'command_input' => env('SENTRY_BREADCRUMBS_COMMAND_INPUT', true), 'sql_queries' => env('SENTRY_BREADCRUMBS_SQL_QUERIES', true), 'sql_bindings' => env('SENTRY_BREADCRUMBS_SQL_BINDINGS', true), 'sql_transaction' => env('SENTRY_BREADCRUMBS_SQL_TRANSACTION', true), From 1d3f9e309803084a69c1407b49a84c8da2a40d37 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:36:40 +0800 Subject: [PATCH 17/18] fix(switcher): rename breadcrumb enable methods for consistency and deprecate old methods fix(aspects): update tracing span enable methods for consistency across all aspects fix(listener): standardize tracing enable checks in EventHandleListener for various events --- src/sentry/src/Aspect/CacheAspect.php | 2 +- .../src/Aspect/GuzzleHttpClientAspect.php | 2 +- src/sentry/src/Aspect/LoggerAspect.php | 2 +- src/sentry/src/Aspect/RedisAspect.php | 2 +- .../Crons/Listener/EventHandleListener.php | 2 +- .../src/Listener/EventHandleListener.php | 54 ++++++++--------- src/sentry/src/Switcher.php | 60 +++++++++++++++++-- .../src/Tracing/Aspect/AmqpProducerAspect.php | 2 +- .../Aspect/AsyncQueueJobMessageAspect.php | 2 +- src/sentry/src/Tracing/Aspect/CacheAspect.php | 2 +- .../src/Tracing/Aspect/CoordinatorAspect.php | 2 +- .../src/Tracing/Aspect/CoroutineAspect.php | 4 +- src/sentry/src/Tracing/Aspect/DbAspect.php | 8 +-- .../Tracing/Aspect/ElasticsearchAspect.php | 6 +- src/sentry/src/Tracing/Aspect/GrpcAspect.php | 6 +- .../Tracing/Aspect/GuzzleHttpClientAspect.php | 6 +- .../Tracing/Aspect/KafkaProducerAspect.php | 2 +- src/sentry/src/Tracing/Aspect/RedisAspect.php | 6 +- src/sentry/src/Tracing/Aspect/RpcAspect.php | 6 +- .../Tracing/Aspect/TraceAnnotationAspect.php | 4 +- .../Tracing/Listener/EventHandleListener.php | 42 ++++++------- 21 files changed, 135 insertions(+), 87 deletions(-) diff --git a/src/sentry/src/Aspect/CacheAspect.php b/src/sentry/src/Aspect/CacheAspect.php index 4b988cb89..657adc592 100644 --- a/src/sentry/src/Aspect/CacheAspect.php +++ b/src/sentry/src/Aspect/CacheAspect.php @@ -40,7 +40,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $startTime = microtime(true); return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint, $startTime) { - if (! $this->switcher->isBreadcrumbEnable('cache')) { + if (! $this->switcher->isBreadcrumbEnabled('cache')) { return; } $arguments = $proceedingJoinPoint->arguments['keys']; diff --git a/src/sentry/src/Aspect/GuzzleHttpClientAspect.php b/src/sentry/src/Aspect/GuzzleHttpClientAspect.php index edf94cbec..7f3045139 100644 --- a/src/sentry/src/Aspect/GuzzleHttpClientAspect.php +++ b/src/sentry/src/Aspect/GuzzleHttpClientAspect.php @@ -36,7 +36,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { // If the guzzle aspect is disabled, we will not record the request. - if (! $this->switcher->isBreadcrumbEnable('guzzle')) { + if (! $this->switcher->isBreadcrumbEnabled('guzzle')) { return $proceedingJoinPoint->process(); } diff --git a/src/sentry/src/Aspect/LoggerAspect.php b/src/sentry/src/Aspect/LoggerAspect.php index e0689ea5e..9db407f02 100644 --- a/src/sentry/src/Aspect/LoggerAspect.php +++ b/src/sentry/src/Aspect/LoggerAspect.php @@ -39,7 +39,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { - if (! $this->switcher->isBreadcrumbEnable('logs')) { + if (! $this->switcher->isBreadcrumbEnabled('logs')) { return; } diff --git a/src/sentry/src/Aspect/RedisAspect.php b/src/sentry/src/Aspect/RedisAspect.php index cff3593eb..922efcaa9 100644 --- a/src/sentry/src/Aspect/RedisAspect.php +++ b/src/sentry/src/Aspect/RedisAspect.php @@ -42,7 +42,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return tap($proceedingJoinPoint->process(), function ($result) use ($arguments, $startTime) { if ( class_exists(CommandExecuted::class) - || ! $this->switcher->isBreadcrumbEnable('redis') + || ! $this->switcher->isBreadcrumbEnabled('redis') ) { return; } diff --git a/src/sentry/src/Crons/Listener/EventHandleListener.php b/src/sentry/src/Crons/Listener/EventHandleListener.php index 583708c37..b819d4281 100644 --- a/src/sentry/src/Crons/Listener/EventHandleListener.php +++ b/src/sentry/src/Crons/Listener/EventHandleListener.php @@ -44,7 +44,7 @@ public function listen(): array */ public function process(object $event): void { - if (! $this->switcher->isCronsEnable()) { + if (! $this->switcher->isCronsEnabled()) { return; } diff --git a/src/sentry/src/Listener/EventHandleListener.php b/src/sentry/src/Listener/EventHandleListener.php index 2a909bb73..77922a3eb 100644 --- a/src/sentry/src/Listener/EventHandleListener.php +++ b/src/sentry/src/Listener/EventHandleListener.php @@ -239,8 +239,8 @@ protected function setupRequestLifecycle(): void } if ( - ! $this->switcher->isEnable('request') - && ! $this->switcher->isTracingEnable('request') + ! $this->switcher->isEnabled('request') + && ! $this->switcher->isTracingEnabled('request') ) { return; } @@ -283,7 +283,7 @@ protected function setupRedisEventEnable(): void */ protected function handleDbQueryExecuted(object $event): void { - if (! $this->switcher->isBreadcrumbEnable('sql_queries')) { + if (! $this->switcher->isBreadcrumbEnabled('sql_queries')) { return; } @@ -293,7 +293,7 @@ protected function handleDbQueryExecuted(object $event): void $data['executionTimeMs'] = $event->time; } - if ($this->switcher->isBreadcrumbEnable('sql_bindings')) { + if ($this->switcher->isBreadcrumbEnabled('sql_bindings')) { $data['bindings'] = $event->bindings; } @@ -311,7 +311,7 @@ protected function handleDbQueryExecuted(object $event): void */ protected function handleDbTransaction(object $event): void { - if (! $this->switcher->isBreadcrumbEnable('sql_transaction')) { + if (! $this->switcher->isBreadcrumbEnabled('sql_transaction')) { return; } @@ -331,7 +331,7 @@ protected function handleDbTransaction(object $event): void */ protected function handleRedisCommandExecuted(object $event): void { - if (! $this->switcher->isBreadcrumbEnable('redis')) { + if (! $this->switcher->isBreadcrumbEnabled('redis')) { return; } @@ -353,7 +353,7 @@ protected function handleRedisCommandExecuted(object $event): void */ protected function handleRequestReceived(object $event): void { - if (! $this->switcher->isEnable('request')) { + if (! $this->switcher->isEnabled('request')) { return; } @@ -365,7 +365,7 @@ protected function handleRequestReceived(object $event): void */ protected function handleRequestTerminated(object $event): void { - if (! $this->switcher->isEnable('request')) { + if (! $this->switcher->isEnabled('request')) { return; } @@ -378,7 +378,7 @@ protected function handleRequestTerminated(object $event): void */ protected function handleCommandStarting(object $event): void { - if (! $this->switcher->isEnable('command')) { + if (! $this->switcher->isEnabled('command')) { return; } @@ -388,9 +388,9 @@ protected function handleCommandStarting(object $event): void $scope->setTag('command', $event->getCommand()->getName()); }); - if ($this->switcher->isBreadcrumbEnable('command')) { + if ($this->switcher->isBreadcrumbEnabled('command')) { $data = []; - if ($this->switcher->isBreadcrumbEnable('command_input')) { + if ($this->switcher->isBreadcrumbEnabled('command_input')) { /** @var InputInterface $input */ $input = (fn () => $this->input)->call($event->getCommand()); $data['input'] = $this->extractConsoleCommandInput($input); @@ -411,11 +411,11 @@ protected function handleCommandStarting(object $event): void */ protected function handleCommandFinished(object $event): void { - if (! $this->switcher->isEnable('command')) { + if (! $this->switcher->isEnabled('command')) { return; } - if ($this->switcher->isBreadcrumbEnable('command')) { + if ($this->switcher->isBreadcrumbEnabled('command')) { /** @var InputInterface $input */ /** @var int $exitCode */ [$input, $exitCode] = (function () { @@ -426,7 +426,7 @@ protected function handleCommandFinished(object $event): void })->call($event->getCommand()); $data = ['exit' => $exitCode]; - if ($this->switcher->isBreadcrumbEnable('command_input')) { + if ($this->switcher->isBreadcrumbEnabled('command_input')) { $data['input'] = $this->extractConsoleCommandInput($input); } @@ -452,13 +452,13 @@ protected function handleCommandFinished(object $event): void */ protected function handleAsyncQueueJobProcessing(object $event): void { - if (! $this->switcher->isEnable('async_queue')) { + if (! $this->switcher->isEnabled('async_queue')) { return; } $this->setupSentrySdk(); - if ($this->switcher->isBreadcrumbEnable('async_queue')) { + if ($this->switcher->isBreadcrumbEnabled('async_queue')) { $job = [ 'job' => $event->getMessage()->job()::class, 'attempts' => $event->getMessage()->getAttempts(), @@ -479,7 +479,7 @@ protected function handleAsyncQueueJobProcessing(object $event): void */ protected function handleAsyncQueueJobProcessed(object $event): void { - if (! $this->switcher->isEnable('async_queue')) { + if (! $this->switcher->isEnabled('async_queue')) { return; } @@ -491,7 +491,7 @@ protected function handleAsyncQueueJobProcessed(object $event): void */ protected function handleAsyncQueueJobRetryOrFailed(object $event): void { - if (! $this->switcher->isEnable('async_queue')) { + if (! $this->switcher->isEnabled('async_queue')) { return; } @@ -504,7 +504,7 @@ protected function handleAsyncQueueJobRetryOrFailed(object $event): void */ protected function handleCrontabTaskStarting(object $event): void { - if (! $this->switcher->isEnable('crontab')) { + if (! $this->switcher->isEnabled('crontab')) { return; } @@ -516,7 +516,7 @@ protected function handleCrontabTaskStarting(object $event): void */ protected function handleCrontabTaskFinished(object $event): void { - if (! $this->switcher->isEnable('crontab')) { + if (! $this->switcher->isEnabled('crontab')) { return; } @@ -528,7 +528,7 @@ protected function handleCrontabTaskFinished(object $event): void */ protected function handleCrontabTaskFailed(object $event): void { - if (! $this->switcher->isEnable('crontab')) { + if (! $this->switcher->isEnabled('crontab')) { return; } @@ -541,7 +541,7 @@ protected function handleCrontabTaskFailed(object $event): void */ protected function handleAmqpMessageProcessing(object $event): void { - if (! $this->switcher->isEnable('amqp')) { + if (! $this->switcher->isEnabled('amqp')) { return; } @@ -553,7 +553,7 @@ protected function handleAmqpMessageProcessing(object $event): void */ protected function handleAmqpMessageProcessed(object $event): void { - if (! $this->switcher->isEnable('amqp')) { + if (! $this->switcher->isEnabled('amqp')) { return; } @@ -565,7 +565,7 @@ protected function handleAmqpMessageProcessed(object $event): void */ protected function handleAmqpMessageFailed(object $event): void { - if (! $this->switcher->isEnable('amqp')) { + if (! $this->switcher->isEnabled('amqp')) { return; } @@ -578,7 +578,7 @@ protected function handleAmqpMessageFailed(object $event): void */ protected function handleKafkaMessageProcessing(object $event): void { - if (! $this->switcher->isEnable('kafka')) { + if (! $this->switcher->isEnabled('kafka')) { return; } @@ -590,7 +590,7 @@ protected function handleKafkaMessageProcessing(object $event): void */ protected function handleKafkaMessageProcessed(object $event): void { - if (! $this->switcher->isEnable('kafka')) { + if (! $this->switcher->isEnabled('kafka')) { return; } @@ -602,7 +602,7 @@ protected function handleKafkaMessageProcessed(object $event): void */ protected function handleKafkaMessageFailed(object $event): void { - if (! $this->switcher->isEnable('kafka')) { + if (! $this->switcher->isEnabled('kafka')) { return; } diff --git a/src/sentry/src/Switcher.php b/src/sentry/src/Switcher.php index b062b0725..4f5523d02 100644 --- a/src/sentry/src/Switcher.php +++ b/src/sentry/src/Switcher.php @@ -22,17 +22,33 @@ public function __construct(protected ConfigInterface $config) { } - public function isEnable(string $key, bool $default = true): bool + public function isEnabled(string $key, bool $default = true): bool { return (bool) $this->config->get('sentry.enable.' . $key, $default); } - public function isBreadcrumbEnable(string $key, bool $default = true): bool + /** + * @deprecated since v3.1, use isEnabled instead, will be removed in v3.2 + */ + public function isEnable(string $key, bool $default = true): bool + { + return $this->isEnabled($key, $default); + } + + public function isBreadcrumbEnabled(string $key, bool $default = true): bool { return (bool) $this->config->get('sentry.breadcrumbs.' . $key, $default); } - public function isTracingEnable(string $key, bool $default = true): bool + /** + * @deprecated since v3.1, use isBreadcrumbEnabled instead, will be removed in v3.2 + */ + public function isBreadcrumbEnable(string $key, bool $default = true): bool + { + return $this->isBreadcrumbEnabled($key, $default); + } + + public function isTracingEnabled(string $key, bool $default = true): bool { if (! $this->config->get('sentry.enable_tracing', true)) { return false; @@ -41,7 +57,15 @@ public function isTracingEnable(string $key, bool $default = true): bool return (bool) $this->config->get('sentry.tracing.enable.' . $key, $default); } - public function isTracingSpanEnable(string $key, bool $default = true): bool + /** + * @deprecated since v3.1, use isTracingEnabled instead, will be removed in v3.2 + */ + public function isTracingEnable(string $key, bool $default = true): bool + { + return $this->isTracingEnabled($key, $default); + } + + public function isTracingSpanEnabled(string $key, bool $default = true): bool { if (! SentrySdk::getCurrentHub()->getSpan()) { return false; @@ -50,11 +74,27 @@ public function isTracingSpanEnable(string $key, bool $default = true): bool return (bool) $this->config->get('sentry.tracing.spans.' . $key, $default); } - public function isTracingExtraTagEnable(string $key, bool $default = false): bool + /** + * @deprecated since v3.1, use isTracingSpanEnabled instead, will be removed in v3.2 + */ + public function isTracingSpanEnable(string $key, bool $default = true): bool + { + return $this->isTracingSpanEnabled($key, $default); + } + + public function isTracingExtraTagEnabled(string $key, bool $default = false): bool { return (bool) ($this->config->get('sentry.tracing.extra_tags', [])[$key] ?? $default); } + /** + * @deprecated since v3.1, use isTracingExtraTagEnabled instead, will be removed in v3.2 + */ + public function isTracingExtraTagEnable(string $key, bool $default = false): bool + { + return $this->isTracingExtraTagEnabled($key, $default); + } + public function isExceptionIgnored(string|Throwable $exception): bool { $ignoreExceptions = (array) $this->config->get('sentry.ignore_exceptions', []); @@ -68,11 +108,19 @@ public function isExceptionIgnored(string|Throwable $exception): bool return false; } - public function isCronsEnable(): bool + public function isCronsEnabled(): bool { return (bool) $this->config->get('sentry.crons.enable', true); } + /** + * @deprecated since v3.1, use isCronsEnabled instead, will be removed in v3.2 + */ + public function isCronsEnable(): bool + { + return $this->isCronsEnabled(); + } + public static function disableCoroutineTracing(): void { Context::set(Constants::DISABLE_COROUTINE_TRACING, true); diff --git a/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php b/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php index 6aadd0b41..54b8ab647 100644 --- a/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php +++ b/src/sentry/src/Tracing/Aspect/AmqpProducerAspect.php @@ -41,7 +41,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingEnable('amqp')) { + if (! $this->switcher->isTracingEnabled('amqp')) { return $proceedingJoinPoint->process(); } diff --git a/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php b/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php index c96d72b50..5f3e6a5a4 100644 --- a/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php +++ b/src/sentry/src/Tracing/Aspect/AsyncQueueJobMessageAspect.php @@ -46,7 +46,7 @@ public function __construct( public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingEnable('async_queue')) { + if (! $this->switcher->isTracingEnabled('async_queue')) { return $proceedingJoinPoint->process(); } diff --git a/src/sentry/src/Tracing/Aspect/CacheAspect.php b/src/sentry/src/Tracing/Aspect/CacheAspect.php index 76f553301..11363280c 100644 --- a/src/sentry/src/Tracing/Aspect/CacheAspect.php +++ b/src/sentry/src/Tracing/Aspect/CacheAspect.php @@ -43,7 +43,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingSpanEnable('cache') || Switcher::isDisableCoroutineTracing()) { + if (! $this->switcher->isTracingSpanEnabled('cache') || Switcher::isDisableCoroutineTracing()) { return $proceedingJoinPoint->process(); } diff --git a/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php b/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php index 890c72268..174be3b8c 100644 --- a/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoordinatorAspect.php @@ -57,7 +57,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span?->setData([ 'exception.stack_trace' => (string) $exception, ]); diff --git a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php index 88a1a86b6..c2b190d95 100644 --- a/src/sentry/src/Tracing/Aspect/CoroutineAspect.php +++ b/src/sentry/src/Tracing/Aspect/CoroutineAspect.php @@ -40,7 +40,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { if ( - ! $this->switcher->isTracingSpanEnable('coroutine') + ! $this->switcher->isTracingSpanEnabled('coroutine') || Switcher::isDisableCoroutineTracing() ) { return $proceedingJoinPoint->process(); @@ -89,7 +89,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $transaction->setData([ 'exception.stack_trace' => (string) $exception, ]); diff --git a/src/sentry/src/Tracing/Aspect/DbAspect.php b/src/sentry/src/Tracing/Aspect/DbAspect.php index dd987ca40..e1bf16304 100644 --- a/src/sentry/src/Tracing/Aspect/DbAspect.php +++ b/src/sentry/src/Tracing/Aspect/DbAspect.php @@ -43,7 +43,7 @@ public function __construct( public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingSpanEnable('db')) { + if (! $this->switcher->isTracingSpanEnabled('db')) { return $proceedingJoinPoint->process(); } @@ -91,7 +91,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) // 'server.port' => '', ]; - if ($this->switcher->isTracingExtraTagEnable('db.sql.bindings', true)) { + if ($this->switcher->isTracingExtraTagEnabled('db.sql.bindings', true)) { $data['db.sql.bindings'] = $arguments['arguments']['bindings'] ?? []; foreach ($arguments['arguments']['bindings'] as $key => $value) { $data['db.parameter.' . $key] = $value; @@ -102,7 +102,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); - if ($this->switcher->isTracingExtraTagEnable('db.result')) { + if ($this->switcher->isTracingExtraTagEnabled('db.result')) { $span?->setData([ 'db.result' => json_encode($result, JSON_UNESCAPED_UNICODE), ]); @@ -115,7 +115,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span?->setData([ 'exception.stack_trace' => (string) $exception, ]); diff --git a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php index b870c1187..802bab758 100644 --- a/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php +++ b/src/sentry/src/Tracing/Aspect/ElasticsearchAspect.php @@ -58,7 +58,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingSpanEnable('elasticsearch')) { + if (! $this->switcher->isTracingSpanEnabled('elasticsearch')) { return $proceedingJoinPoint->process(); } @@ -85,7 +85,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); - if ($this->switcher->isTracingExtraTagEnable('elasticsearch.result')) { + if ($this->switcher->isTracingExtraTagEnabled('elasticsearch.result')) { $span->setData([ 'elasticsearch.result' => json_encode($result, JSON_UNESCAPED_UNICODE), ]); @@ -98,7 +98,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span->setData([ 'exception.stack_trace' => (string) $exception, ]); diff --git a/src/sentry/src/Tracing/Aspect/GrpcAspect.php b/src/sentry/src/Tracing/Aspect/GrpcAspect.php index cc83ad258..29e5b17f0 100644 --- a/src/sentry/src/Tracing/Aspect/GrpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/GrpcAspect.php @@ -36,7 +36,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingSpanEnable('grpc')) { + if (! $this->switcher->isTracingSpanEnabled('grpc')) { return $proceedingJoinPoint->process(); } @@ -83,7 +83,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'response.reason' => $message, 'response.headers' => $response->headers, ]); - if ($this->switcher->isTracingExtraTagEnable('response.body')) { + if ($this->switcher->isTracingExtraTagEnabled('response.body')) { $span?->setData([ 'response.body' => $response->data, ]); @@ -99,7 +99,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span?->setData([ 'exception.stack_trace' => (string) $exception, ]); diff --git a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php index abbec732d..f75c35627 100644 --- a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php +++ b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php @@ -46,7 +46,7 @@ public function __construct( public function process(ProceedingJoinPoint $proceedingJoinPoint) { if ( - ! $this->switcher->isTracingSpanEnable('guzzle') + ! $this->switcher->isTracingSpanEnabled('guzzle') || Context::get(RpcAspect::SPAN) // If the parent span is not exists or the parent span is belongs to rpc, then skip. ) { return $proceedingJoinPoint->process(); @@ -128,7 +128,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'http.response_transfer_size' => $response->getHeaderLine('Content-Length'), ]); - if ($this->switcher->isTracingExtraTagEnable('http.response.body.contents')) { + if ($this->switcher->isTracingExtraTagEnabled('http.response.body.contents')) { $isTextual = \preg_match( '/^(text\/|application\/(json|xml|x-www-form-urlencoded))/i', $response->getHeaderLine('Content-Type') @@ -166,7 +166,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'exception.class' => $exception::class, 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span->setData([ 'exception.message' => $exception->getMessage(), 'exception.stack_trace' => (string) $exception, diff --git a/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php b/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php index cd0f9d263..7538e8139 100644 --- a/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php +++ b/src/sentry/src/Tracing/Aspect/KafkaProducerAspect.php @@ -42,7 +42,7 @@ public function __construct(protected Switcher $switcher) public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingEnable('kafka')) { + if (! $this->switcher->isTracingEnabled('kafka')) { return $proceedingJoinPoint->process(); } diff --git a/src/sentry/src/Tracing/Aspect/RedisAspect.php b/src/sentry/src/Tracing/Aspect/RedisAspect.php index 46c6ee6c0..b92b9ec8b 100644 --- a/src/sentry/src/Tracing/Aspect/RedisAspect.php +++ b/src/sentry/src/Tracing/Aspect/RedisAspect.php @@ -49,7 +49,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) { if ( class_exists(CommandExecuted::class) - || ! $this->switcher->isTracingSpanEnable('redis') + || ! $this->switcher->isTracingSpanEnabled('redis') ) { return $proceedingJoinPoint->process(); } @@ -90,7 +90,7 @@ class_exists(CommandExecuted::class) try { $result = $proceedingJoinPoint->process(); - if ($this->switcher->isTracingExtraTagEnable('redis.result')) { + if ($this->switcher->isTracingExtraTagEnabled('redis.result')) { $span?->setData(['redis.result' => $result]); } @@ -103,7 +103,7 @@ class_exists(CommandExecuted::class) 'exception.message' => $e->getMessage(), 'exception.code' => (string) $e->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span?->setData(['exception.stack_trace' => (string) $e]); } diff --git a/src/sentry/src/Tracing/Aspect/RpcAspect.php b/src/sentry/src/Tracing/Aspect/RpcAspect.php index c1d1e4fbb..70932f4c6 100644 --- a/src/sentry/src/Tracing/Aspect/RpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/RpcAspect.php @@ -52,7 +52,7 @@ public function __construct( public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcher->isTracingSpanEnable('rpc')) { + if (! $this->switcher->isTracingSpanEnabled('rpc')) { return $proceedingJoinPoint->process(); } @@ -129,7 +129,7 @@ private function handleSend(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); - if ($this->switcher->isTracingExtraTagEnable('rpc.result')) { + if ($this->switcher->isTracingExtraTagEnabled('rpc.result')) { $span?->setData(['rpc.result' => $result]); } @@ -142,7 +142,7 @@ private function handleSend(ProceedingJoinPoint $proceedingJoinPoint) 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span?->setData(['exception.stack_trace' => (string) $exception]); } diff --git a/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php b/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php index 96fcbaafa..ac367227e 100644 --- a/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php +++ b/src/sentry/src/Tracing/Aspect/TraceAnnotationAspect.php @@ -67,7 +67,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) try { $result = $proceedingJoinPoint->process(); - if ($this->switcher->isTracingExtraTagEnable('annotation.result')) { + if ($this->switcher->isTracingExtraTagEnabled('annotation.result')) { $span?->setData(['annotation.result' => $result]); } @@ -80,7 +80,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span?->setData(['exception.stack_trace' => (string) $exception]); } throw $exception; diff --git a/src/sentry/src/Tracing/Listener/EventHandleListener.php b/src/sentry/src/Tracing/Listener/EventHandleListener.php index 8464a8282..d17541275 100644 --- a/src/sentry/src/Tracing/Listener/EventHandleListener.php +++ b/src/sentry/src/Tracing/Listener/EventHandleListener.php @@ -163,7 +163,7 @@ public function process(object $event): void private function handleDbQueryExecuted(DbEvent\QueryExecuted $event): void { - if (! $this->switcher->isTracingSpanEnable('sql_queries')) { + if (! $this->switcher->isTracingSpanEnabled('sql_queries')) { return; } if (! SentrySdk::getCurrentHub()->getSpan()) { @@ -193,7 +193,7 @@ private function handleDbQueryExecuted(DbEvent\QueryExecuted $event): void 'db.pool.using' => $pool->getCurrentConnections(), ]; - if ($this->switcher->isTracingExtraTagEnable('db.sql.bindings', true)) { + if ($this->switcher->isTracingExtraTagEnabled('db.sql.bindings', true)) { $data['db.sql.bindings'] = $event->bindings; foreach ($event->bindings as $key => $value) { $data['db.parameter.' . $key] = $value; @@ -213,7 +213,7 @@ private function handleDbQueryExecuted(DbEvent\QueryExecuted $event): void private function handleDbTransactionBeginning(DbEvent\TransactionBeginning $event): void { - if (! $this->switcher->isTracingSpanEnable('sql_transactions')) { + if (! $this->switcher->isTracingSpanEnabled('sql_transactions')) { return; } if (! SentrySdk::getCurrentHub()->getSpan()) { @@ -234,7 +234,7 @@ private function handleDbTransactionBeginning(DbEvent\TransactionBeginning $even private function handleDbTransactionCommitted(DbEvent\TransactionCommitted $event): void { - if (! $this->switcher->isTracingSpanEnable('sql_transactions')) { + if (! $this->switcher->isTracingSpanEnabled('sql_transactions')) { return; } if (! $span = SentrySdk::getCurrentHub()->getSpan()) { @@ -248,7 +248,7 @@ private function handleDbTransactionCommitted(DbEvent\TransactionCommitted $even private function handleDbTransactionRolledBack(DbEvent\TransactionRolledBack $event): void { - if (! $this->switcher->isTracingSpanEnable('sql_transactions')) { + if (! $this->switcher->isTracingSpanEnabled('sql_transactions')) { return; } if (! $span = SentrySdk::getCurrentHub()->getSpan()) { @@ -262,7 +262,7 @@ private function handleDbTransactionRolledBack(DbEvent\TransactionRolledBack $ev private function handleRequestReceived(HttpEvent\RequestReceived|RpcEvent\RequestReceived $event): void { - if (! $this->switcher->isTracingEnable('request')) { + if (! $this->switcher->isTracingEnabled('request')) { return; } @@ -270,7 +270,7 @@ private function handleRequestReceived(HttpEvent\RequestReceived|RpcEvent\Reques /** @var Dispatched $dispatched */ $dispatched = $request->getAttribute(Dispatched::class); - if (! $dispatched->isFound() && ! $this->switcher->isTracingEnable('missing_routes')) { + if (! $dispatched->isFound() && ! $this->switcher->isTracingEnabled('missing_routes')) { return; } @@ -364,7 +364,7 @@ private function handleRequestHandled(HttpEvent\RequestHandled|RpcEvent\RequestH 'exception.code' => (string) $exception->getCode(), 'exception.message' => $exception->getMessage(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $transaction->setData([ 'exception.stack_trace' => (string) $exception, ]); @@ -375,7 +375,7 @@ private function handleRequestHandled(HttpEvent\RequestHandled|RpcEvent\RequestH private function handleCommandStarting(CommandEvent\BeforeHandle $event): void { if ( - ! $this->switcher->isTracingEnable('command') + ! $this->switcher->isTracingEnabled('command') || Str::is($this->ignoreCommands, $event->getCommand()->getName()) ) { return; @@ -418,7 +418,7 @@ private function handleCommandFinished(CommandEvent\AfterExecute $event): void 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $transaction->setData([ 'exception.stack_trace' => (string) $exception, ]); @@ -436,7 +436,7 @@ private function handleCommandFinished(CommandEvent\AfterExecute $event): void private function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event): void { - if (! $this->switcher->isTracingSpanEnable('redis')) { + if (! $this->switcher->isTracingSpanEnabled('redis')) { return; } @@ -469,7 +469,7 @@ private function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event): 'duration' => $event->time * 1000, ]); - if ($this->switcher->isTracingExtraTagEnable('redis.result')) { + if ($this->switcher->isTracingExtraTagEnabled('redis.result')) { $span->setData(['db.redis.result' => $event->result]); } @@ -481,7 +481,7 @@ private function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event): 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $span->setData(['exception.stack_trace' => (string) $exception]); } } @@ -491,7 +491,7 @@ private function handleRedisCommandExecuted(RedisEvent\CommandExecuted $event): private function handleCrontabTaskStarting(CrontabEvent\BeforeExecute $event): void { - if (! $this->switcher->isTracingEnable('crontab')) { + if (! $this->switcher->isTracingEnabled('crontab')) { return; } @@ -530,7 +530,7 @@ private function handleCrontabTaskFinished(CrontabEvent\FailToExecute|CrontabEve 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $transaction->setData(['exception.stack_trace' => (string) $exception]); } } @@ -542,7 +542,7 @@ private function handleCrontabTaskFinished(CrontabEvent\FailToExecute|CrontabEve private function handleAmqpMessageProcessing(AmqpEvent\BeforeConsume $event): void { - if (! $this->switcher->isTracingEnable('amqp')) { + if (! $this->switcher->isTracingEnabled('amqp')) { return; } @@ -608,7 +608,7 @@ private function handleAmqpMessageProcessed(AmqpEvent\AfterConsume|AmqpEvent\Fai 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $transaction->setData(['exception.stack_trace' => (string) $exception]); } } @@ -620,7 +620,7 @@ private function handleAmqpMessageProcessed(AmqpEvent\AfterConsume|AmqpEvent\Fai private function handleKafkaMessageProcessing(KafkaEvent\BeforeConsume $event): void { - if (! $this->switcher->isTracingEnable('kafka')) { + if (! $this->switcher->isTracingEnabled('kafka')) { return; } @@ -680,7 +680,7 @@ private function handleKafkaMessageProcessed(KafkaEvent\AfterConsume|KafkaEvent\ 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $transaction->setData(['exception.stack_trace' => (string) $exception]); } } @@ -692,7 +692,7 @@ private function handleKafkaMessageProcessed(KafkaEvent\AfterConsume|KafkaEvent\ private function handleAsyncQueueJobProcessing(AsyncQueueEvent\BeforeHandle $event): void { - if (! $this->switcher->isTracingEnable('async_queue')) { + if (! $this->switcher->isTracingEnabled('async_queue')) { return; } @@ -739,7 +739,7 @@ private function handleAsyncQueueJobProcessed(AsyncQueueEvent\AfterHandle|AsyncQ 'exception.message' => $exception->getMessage(), 'exception.code' => (string) $exception->getCode(), ]); - if ($this->switcher->isTracingExtraTagEnable('exception.stack_trace')) { + if ($this->switcher->isTracingExtraTagEnabled('exception.stack_trace')) { $transaction->setData(['exception.stack_trace' => (string) $exception]); } } From 69ab12981582eb4f4792f7ace57b642ee1d74e13 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 21 Sep 2025 00:41:05 +0800 Subject: [PATCH 18/18] fix(switcher): deprecate isExceptionIgnored method in favor of isExceptionIgnored with documentation reference --- src/sentry/src/Switcher.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sentry/src/Switcher.php b/src/sentry/src/Switcher.php index 4f5523d02..445299d81 100644 --- a/src/sentry/src/Switcher.php +++ b/src/sentry/src/Switcher.php @@ -95,6 +95,10 @@ public function isTracingExtraTagEnable(string $key, bool $default = false): boo return $this->isTracingExtraTagEnabled($key, $default); } + /** + * @deprecated since v3.1, use isExceptionIgnored instead, will be removed in v3.2 + * @see https://docs.sentry.io/platforms/php/configuration/options/#ignore_exceptions + */ public function isExceptionIgnored(string|Throwable $exception): bool { $ignoreExceptions = (array) $this->config->get('sentry.ignore_exceptions', []);