From 93cb4afde913440c004642134881df5e9f79783e Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 24 Jan 2024 15:45:55 +0800 Subject: [PATCH] Add support for W3C traceparent header (#519) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php | 1 + src/sentry/src/Tracing/Aspect/RpcAspect.php | 7 +++---- src/sentry/src/Tracing/SpanStarter.php | 8 ++++++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php index 81acfb67c..3af0b9380 100644 --- a/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php +++ b/src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php @@ -97,6 +97,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) $options['headers'] = array_replace($options['headers'] ?? [], [ 'sentry-trace' => $parent->toTraceparent(), 'baggage' => $parent->toBaggage(), + 'traceparent' => $parent->toW3CTraceparent(), ]); $proceedingJoinPoint->arguments['keys']['options']['headers'] = $options['headers']; diff --git a/src/sentry/src/Tracing/Aspect/RpcAspect.php b/src/sentry/src/Tracing/Aspect/RpcAspect.php index 11d1026b8..6b5406721 100644 --- a/src/sentry/src/Tracing/Aspect/RpcAspect.php +++ b/src/sentry/src/Tracing/Aspect/RpcAspect.php @@ -79,12 +79,11 @@ private function handleGenerateRpcPath(ProceedingJoinPoint $proceedingJoinPoint) Context::set(static::DATA, $data); if ($this->container->has(Rpc\Context::class)) { - $sentryTrace = $span->toTraceparent(); - $baggage = $span->toBaggage(); $rpcContext = $this->container->get(Rpc\Context::class); $rpcContext->set(Constants::RPC_CARRIER, [ - 'sentry-trace' => $sentryTrace, - 'baggage' => $baggage, + 'sentry-trace' => $span->toTraceparent(), + 'baggage' => $span->toBaggage(), + 'traceparent' => $span->toW3CTraceparent(), ]); } diff --git a/src/sentry/src/Tracing/SpanStarter.php b/src/sentry/src/Tracing/SpanStarter.php index b0c1c8583..c4859bdfb 100644 --- a/src/sentry/src/Tracing/SpanStarter.php +++ b/src/sentry/src/Tracing/SpanStarter.php @@ -50,7 +50,11 @@ protected function startSpan(?string $op = null, ?string $description = null, bo protected function startRequestTransaction(ServerRequestInterface $request, ...$options): Transaction { // Get sentry-trace and baggage - $sentryTrace = $request->getHeaderLine('sentry-trace'); + $sentryTrace = match (true) { + $request->hasHeader('sentry-trace') => $request->getHeaderLine('sentry-trace'), + $request->hasHeader('traceparent') => $request->getHeaderLine('traceparent'), + default => '', + }; $baggage = $request->getHeaderLine('baggage'); $container = $this->container ?? ApplicationContext::getContainer(); @@ -58,7 +62,7 @@ protected function startRequestTransaction(ServerRequestInterface $request, ...$ $rpcContext = $container->get(RpcContext::class); $carrier = $rpcContext->get(Constants::RPC_CARRIER); if (! empty($carrier['sentry-trace']) && ! empty($carrier['baggage'])) { - $sentryTrace = $carrier['sentry-trace']; + $sentryTrace = $carrier['sentry-trace'] ?? $carrier['traceparent']; $baggage = $carrier['baggage']; } }