From 6f72b4d98a8d56d9af73f40c3ede9bf54c87793f Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Fri, 19 Jan 2024 11:58:11 +0100 Subject: [PATCH] Add support for W3C traceparent header --- composer.json | 2 +- .../Laravel/Features/HttpClientIntegration.php | 4 +++- src/Sentry/Laravel/Integration.php | 13 ++++++++++++- src/Sentry/Laravel/Tracing/Middleware.php | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index d02e445a..501b8cc8 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "require": { "php": "^7.2 | ^8.0", "illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0", - "sentry/sentry": "^4.3", + "sentry/sentry": "^4.4", "symfony/psr-http-message-bridge": "^1.0 | ^2.0 | ^6.0 | ^7.0", "nyholm/psr7": "^1.0" }, diff --git a/src/Sentry/Laravel/Features/HttpClientIntegration.php b/src/Sentry/Laravel/Features/HttpClientIntegration.php index 56e81249..76a9711f 100644 --- a/src/Sentry/Laravel/Features/HttpClientIntegration.php +++ b/src/Sentry/Laravel/Features/HttpClientIntegration.php @@ -18,6 +18,7 @@ use Sentry\Tracing\SpanStatus; use function Sentry\getBaggage; use function Sentry\getTraceparent; +use function Sentry\getW3CTraceparent; class HttpClientIntegration extends Feature { @@ -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; diff --git a/src/Sentry/Laravel/Integration.php b/src/Sentry/Laravel/Integration.php index df319a9f..5ee57f63 100644 --- a/src/Sentry/Laravel/Integration.php +++ b/src/Sentry/Laravel/Integration.php @@ -22,6 +22,7 @@ use function Sentry\configureScope; use function Sentry\getBaggage; use function Sentry\getTraceparent; +use function Sentry\getW3CTraceparent; class Integration implements IntegrationInterface { @@ -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(); } /** @@ -181,6 +182,16 @@ public static function sentryTracingMeta(): string return sprintf('', 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('', getW3CTraceparent()); + } + /** * Retrieve the `baggage` meta tag with information to link this request to front-end requests. * This propagates the Dynamic Sampling Context. diff --git a/src/Sentry/Laravel/Tracing/Middleware.php b/src/Sentry/Laravel/Tracing/Middleware.php index 89eb0a6c..5cbe73aa 100644 --- a/src/Sentry/Laravel/Tracing/Middleware.php +++ b/src/Sentry/Laravel/Tracing/Middleware.php @@ -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', '') );