From bf18c0d5b0dfc8c1df5a1a6cda33baf1102fbaaf Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 16 Aug 2022 09:09:05 +0200 Subject: [PATCH] Add baggage header to RestTemplate (#2206) --- CHANGELOG.md | 1 + .../spring/boot/SentrySpanRestTemplateCustomizerTest.kt | 4 ++++ .../tracing/SentrySpanClientHttpRequestInterceptor.java | 5 +++++ 3 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e03654aac..64a4533916 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Send source for transactions ([#2180](https://github.com/getsentry/sentry-java/pull/2180)) - Add profilesSampleRate and profileSampler options for Android sdk ([#2184](https://github.com/getsentry/sentry-java/pull/2184)) +- Add baggage header to RestTemplate ([#2206](https://github.com/getsentry/sentry-java/pull/2206)) ## 6.3.1 diff --git a/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt b/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt index db727aa1ee..8ea7381385 100644 --- a/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt +++ b/sentry-spring-boot-starter/src/test/kotlin/io/sentry/spring/boot/SentrySpanRestTemplateCustomizerTest.kt @@ -46,6 +46,9 @@ class SentrySpanRestTemplateCustomizerTest { sentryOptions.tracingOrigins.add("other-api") } + sentryOptions.dsn = "https://key@sentry.io/proj" + sentryOptions.isTraceSampling = true + mockServer.enqueue( MockResponse() .setBody("OK") @@ -78,6 +81,7 @@ class SentrySpanRestTemplateCustomizerTest { assertThat(recordedRequest.headers["sentry-trace"]!!).startsWith(fixture.transaction.spanContext.traceId.toString()) .endsWith("-1") .doesNotContain(fixture.transaction.spanContext.spanId.toString()) + assertThat(recordedRequest.headers["baggage"]!!).contains(fixture.transaction.spanContext.traceId.toString()) } @Test diff --git a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java index 1ab1c9f20f..830218c853 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java @@ -5,6 +5,7 @@ import static io.sentry.TypeCheckHint.SPRING_REQUEST_INTERCEPTOR_RESPONSE; import com.jakewharton.nopen.annotation.Open; +import io.sentry.BaggageHeader; import io.sentry.Breadcrumb; import io.sentry.Hint; import io.sentry.IHub; @@ -50,6 +51,10 @@ public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) { if (TracingOrigins.contain(hub.getOptions().getTracingOrigins(), request.getURI())) { request.getHeaders().add(sentryTraceHeader.getName(), sentryTraceHeader.getValue()); + @Nullable BaggageHeader baggage = span.toBaggageHeader(); + if (baggage != null) { + request.getHeaders().add(baggage.getName(), baggage.getValue()); + } } try {