Skip to content

Commit

Permalink
Add support for W3C traceparent header (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric authored Jan 23, 2024
1 parent 8919f41 commit 3f878af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Sentry/Laravel/Features/HttpClientIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sentry\Tracing\SpanStatus;
use function Sentry\getBaggage;
use function Sentry\getTraceparent;
use function Sentry\getW3CTraceparent;

class HttpClientIntegration extends Feature
{
Expand Down Expand Up @@ -55,7 +56,8 @@ public function attachTracingHeadersToRequest(RequestInterface $request)
if ($this->shouldAttachTracingHeaders($request)) {
return $request
->withHeader('baggage', getBaggage())
->withHeader('sentry-trace', getTraceparent());
->withHeader('sentry-trace', getTraceparent())
->withHeader('traceparent', getW3CTraceparent());
}

return $request;
Expand Down
13 changes: 12 additions & 1 deletion src/Sentry/Laravel/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use function Sentry\configureScope;
use function Sentry\getBaggage;
use function Sentry\getTraceparent;
use function Sentry\getW3CTraceparent;

class Integration implements IntegrationInterface
{
Expand Down Expand Up @@ -168,7 +169,7 @@ static function ($carry, $key) use ($routeData) {
*/
public static function sentryMeta(): string
{
return self::sentryTracingMeta() . self::sentryBaggageMeta();
return self::sentryTracingMeta() . self::sentryW3CTracingMeta() . self::sentryBaggageMeta();
}

/**
Expand All @@ -181,6 +182,16 @@ public static function sentryTracingMeta(): string
return sprintf('<meta name="sentry-trace" content="%s"/>', getTraceparent());
}

/**
* Retrieve the `traceparent` meta tag with tracing information to link this request to front-end requests.
*
* @return string
*/
public static function sentryW3CTracingMeta(): string
{
return sprintf('<meta name="traceparent" content="%s"/>', getW3CTraceparent());
}

/**
* Retrieve the `baggage` meta tag with information to link this request to front-end requests.
* This propagates the Dynamic Sampling Context.
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Laravel/Tracing/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function startTransaction(Request $request, HubInterface $sentry): void
);

$context = continueTrace(
$request->header('sentry-trace', ''),
$request->header('sentry-trace') ?? $request->header('traceparent', ''),
$request->header('baggage', '')
);

Expand Down

0 comments on commit 3f878af

Please sign in to comment.