Skip to content

Commit bdfdc76

Browse files
lazergadinauerclaude
authored
feat(apollo): Add a flag to stop attaching tracing headers in SentryApolloInterceptor (#6414)
* feat(apollo): Add a flag to stop attaching tracing headers in SentryApolloInterceptor * changelog * fix(apollo): Preserve interceptor constructor compatibility Keep the existing Kotlin default-argument constructor ABI while exposing the tracing-header opt-out through a secondary constructor. This prevents runtime linkage failures for precompiled consumers upgrading the SDK. Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Alexander Dinauer <adinauer@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 21a827f commit bdfdc76

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Added `Environment` to the `Scope`, allowing you to overwrite the `Environment` set on the options ([#5772](https://github.com/getsentry/sentry-java/pull/5772))
88
- The scope `Environment` is also applied to the trace envelope header (DSC), logs, metrics and profiles, so all telemetry of a trace reports the same environment
9+
- Add an `attachTracingHeaders` flag to `SentryApolloInterceptor` to stop attaching `sentry-trace` and `baggage` headers to Apollo v2 requests ([#6414](https://github.com/getsentry/sentry-java/pull/6414))
10+
- Apollo v2 has no request URL in the interceptor, so these headers are attached without checking `tracePropagationTargets`. Pass `attachTracingHeaders = false` to opt out.
911

1012
### Improvements
1113

‎sentry-apollo/api/sentry-apollo.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public final class io/sentry/apollo/SentryApolloInterceptor : com/apollographql/
88
public fun <init> (Lio/sentry/IScopes;)V
99
public fun <init> (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V
1010
public synthetic fun <init> (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
11+
public fun <init> (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;Z)V
12+
public synthetic fun <init> (Lio/sentry/IScopes;Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
1113
public fun <init> (Lio/sentry/apollo/SentryApolloInterceptor$BeforeSpanCallback;)V
1214
public fun dispose ()V
1315
public fun interceptAsync (Lcom/apollographql/apollo/interceptor/ApolloInterceptor$InterceptorRequest;Lcom/apollographql/apollo/interceptor/ApolloInterceptorChain;Ljava/util/concurrent/Executor;Lcom/apollographql/apollo/interceptor/ApolloInterceptor$CallBack;)V

‎sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,18 @@ class SentryApolloInterceptor(
4444
}
4545
}
4646

47+
private var attachTracingHeaders: Boolean = true
48+
4749
constructor(scopes: IScopes) : this(scopes, null)
4850

51+
constructor(
52+
scopes: IScopes = ScopesAdapter.getInstance(),
53+
beforeSpan: BeforeSpanCallback? = null,
54+
attachTracingHeaders: Boolean,
55+
) : this(scopes, beforeSpan) {
56+
this.attachTracingHeaders = attachTracingHeaders
57+
}
58+
4959
constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan)
5060

5161
init {
@@ -134,7 +144,7 @@ class SentryApolloInterceptor(
134144
private fun addTracingHeaders(request: InterceptorRequest, span: ISpan?): RequestHeaders {
135145
val requestHeaderBuilder = request.requestHeaders.toBuilder()
136146

137-
if (scopes.options.isTraceSampling && !isIgnored()) {
147+
if (attachTracingHeaders && scopes.options.isTraceSampling && !isIgnored()) {
138148
// we have no access to URI, no way to verify tracing origins
139149
TracingUtils.trace(
140150
scopes,

‎sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class SentryApolloInterceptorTest {
8888
}
8989
}""",
9090
socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN,
91+
attachTracingHeaders: Boolean = true,
9192
beforeSpan: SentryApolloInterceptor.BeforeSpanCallback? = null,
9293
): ApolloClient {
9394
server.enqueue(
@@ -97,8 +98,8 @@ class SentryApolloInterceptorTest {
9798
.setResponseCode(httpStatusCode)
9899
)
99100

100-
if (beforeSpan != null) {
101-
interceptor = SentryApolloInterceptor(scopes, beforeSpan)
101+
if (beforeSpan != null || !attachTracingHeaders) {
102+
interceptor = SentryApolloInterceptor(scopes, beforeSpan, attachTracingHeaders)
102103
}
103104
return ApolloClient.builder()
104105
.serverUrl(server.url("/"))
@@ -206,6 +207,16 @@ class SentryApolloInterceptorTest {
206207
assertNull(recorderRequest.headers[BaggageHeader.BAGGAGE_HEADER])
207208
}
208209

210+
@Test
211+
fun `does not add sentry-trace header when attaching tracing headers is disabled`() {
212+
executeQuery(fixture.getSut(attachTracingHeaders = false))
213+
214+
val recorderRequest =
215+
fixture.server.takeRequest(mockServerRequestTimeoutMillis, TimeUnit.MILLISECONDS)!!
216+
assertNull(recorderRequest.headers[SentryTraceHeader.SENTRY_TRACE_HEADER])
217+
assertNull(recorderRequest.headers[BaggageHeader.BAGGAGE_HEADER])
218+
}
219+
209220
@Test
210221
fun `when there is an active span, adds sentry trace headers to the request`() {
211222
executeQuery()

0 commit comments

Comments
 (0)