Skip to content

Commit

Permalink
Add support for W3C traceparent header (#519)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Jan 24, 2024
1 parent 13330e2 commit 93cb4af
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/sentry/src/Tracing/Aspect/GuzzleHttpClientAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
7 changes: 3 additions & 4 deletions src/sentry/src/Tracing/Aspect/RpcAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}

Expand Down
8 changes: 6 additions & 2 deletions src/sentry/src/Tracing/SpanStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,19 @@ 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();

if ($container->has(RpcContext::class)) {
$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'];
}
}
Expand Down

0 comments on commit 93cb4af

Please sign in to comment.