Skip to content

Commit 2a38eeb

Browse files
adinauerclaude
andcommitted
fix(integrations): Report beforeSpan callback losses
Record callback_error/span when a throwing beforeSpan callback drops a sampled span in OkHttp, OpenFeign, GraphQL, Ktor, and Apollo. Use the pre-callback sampling decision so partial callback mutations do not hide losses, and leave unsampled spans and intentional null drops uncounted. Add regression coverage for discard notifications, sampling states, and intentional drops while preserving request and span completion behavior. Refs #6167 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 835c0d2 commit 2a38eeb

16 files changed

Lines changed: 325 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
- Drop telemetry and record `callback_error` when a customer event processor throws instead of continuing with a potentially partially processed item. SDK-owned processor failures are logged and processing continues without a `callback_error` client report.
129129
- Drop breadcrumbs when `beforeBreadcrumb` throws instead of storing exception details on the breadcrumb.
130130
- Skip Android screenshot or view hierarchy capture when its capture callback throws, while retaining the error event ([#6167](https://github.com/getsentry/sentry-java/pull/6167))
131-
- Drop spans when `beforeSpan` throws in OkHttp, OpenFeign, GraphQL, Ktor, or Apollo, without disrupting the request ([#6167](https://github.com/getsentry/sentry-java/pull/6167))
131+
- Drop spans when `beforeSpan` throws in OkHttp, OpenFeign, GraphQL, Ktor, or Apollo, without disrupting the request. Report lost sampled spans as `callback_error` in client reports and `OnDiscardCallback` ([#6167](https://github.com/getsentry/sentry-java/pull/6167))
132132
- Skip replay capture when `beforeErrorSampling` throws, while still sending the error event ([#6165](https://github.com/getsentry/sentry-java/pull/6165))
133133
- When `tracesSampler` throws, drop the transaction and record `callback_error` instead of inheriting the parent sampling decision or falling back to `tracesSampleRate` ([#6163](https://github.com/getsentry/sentry-java/pull/6163))
134134
- When `profilesSampler` throws, disable profiling instead of falling back to `profilesSampleRate` or inheriting the parent's profiling decision. Trace sampling is unchanged ([#6164](https://github.com/getsentry/sentry-java/pull/6164))

‎sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.apollographql.apollo3.network.http.HttpInterceptor
1010
import com.apollographql.apollo3.network.http.HttpInterceptorChain
1111
import io.sentry.BaggageHeader
1212
import io.sentry.Breadcrumb
13+
import io.sentry.DataCategory
1314
import io.sentry.Hint
1415
import io.sentry.IScopes
1516
import io.sentry.ISpan
@@ -23,6 +24,7 @@ import io.sentry.SpanDataConvention.HTTP_METHOD_KEY
2324
import io.sentry.SpanStatus
2425
import io.sentry.TypeCheckHint.APOLLO_REQUEST
2526
import io.sentry.TypeCheckHint.APOLLO_RESPONSE
27+
import io.sentry.clientreport.DiscardReason
2628
import io.sentry.exception.ExceptionMechanismException
2729
import io.sentry.protocol.Mechanism
2830
import io.sentry.protocol.Request
@@ -216,6 +218,7 @@ constructor(
216218
span.setData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY, it)
217219
}
218220
if (beforeSpan != null) {
221+
val wasSampled = span.isSampled == true
219222
try {
220223
val result = beforeSpan.execute(span, request, response)
221224
if (result == null) {
@@ -224,6 +227,12 @@ constructor(
224227
}
225228
} catch (e: Throwable) {
226229
span.spanContext.sampled = false
230+
if (wasSampled) {
231+
scopes.options.clientReportRecorder.recordLostEvent(
232+
DiscardReason.CALLBACK_ERROR,
233+
DataCategory.Span,
234+
)
235+
}
227236
scopes.options.logger.log(
228237
SentryLevel.ERROR,
229238
"An error occurred while executing beforeSpan on ApolloInterceptor",

‎sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.apollographql.apollo3.network.http.HttpInterceptorChain
1010
import com.google.common.truth.Truth.assertThat
1111
import io.sentry.BaggageHeader
1212
import io.sentry.Breadcrumb
13+
import io.sentry.DataCategory
1314
import io.sentry.ILogger
1415
import io.sentry.IScopes
1516
import io.sentry.ITransaction
@@ -28,6 +29,7 @@ import io.sentry.TracesSamplingDecision
2829
import io.sentry.TransactionContext
2930
import io.sentry.W3CTraceparentHeader
3031
import io.sentry.apollo3.SentryApollo3HttpInterceptor.BeforeSpanCallback
32+
import io.sentry.clientreport.DiscardReason
3133
import io.sentry.mockServerRequestTimeoutMillis
3234
import io.sentry.protocol.SdkVersion
3335
import io.sentry.protocol.SentryTransaction
@@ -50,6 +52,7 @@ import org.mockito.kotlin.check
5052
import org.mockito.kotlin.doAnswer
5153
import org.mockito.kotlin.mock
5254
import org.mockito.kotlin.verify
55+
import org.mockito.kotlin.verifyNoMoreInteractions
5356
import org.mockito.kotlin.whenever
5457

5558
class SentryApollo3InterceptorTest {
@@ -294,7 +297,10 @@ class SentryApollo3InterceptorTest {
294297

295298
@Test
296299
fun `returning null in beforeSpan callback drops span`() {
300+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
301+
fixture.options.onDiscard = onDiscard
297302
executeQuery(fixture.getSut(beforeSpan = { _, _, _ -> null }))
303+
verifyNoMoreInteractions(onDiscard)
298304

299305
verify(fixture.scopes)
300306
.captureTransaction(
@@ -305,6 +311,32 @@ class SentryApollo3InterceptorTest {
305311
)
306312
}
307313

314+
@Test
315+
fun `reports callback errors only for sampled spans`(): Unit = runBlocking {
316+
for (sampled in listOf(true, false, null)) {
317+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
318+
fixture.options.onDiscard = onDiscard
319+
val tx = SentryTracer(TransactionContext("op", "desc"), fixture.scopes)
320+
tx.spanContext.sampled = sampled
321+
whenever(fixture.scopes.span).thenReturn(tx)
322+
val sut =
323+
fixture.getSut(
324+
beforeSpan = { span, _, _ ->
325+
span.spanContext.sampled = false
326+
throw IllegalStateException("callback failed")
327+
}
328+
)
329+
330+
assertThat(sut.query(LaunchDetailsQuery("83")).execute().data).isNotNull()
331+
tx.finish()
332+
333+
if (sampled == true) {
334+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1)
335+
}
336+
verifyNoMoreInteractions(onDiscard)
337+
}
338+
}
339+
308340
@Test
309341
fun `when beforeSpan throws, drops span and preserves response`(): Unit = runBlocking {
310342
val failure = IllegalStateException("callback failed")

‎sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApollo4HttpInterceptor.kt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.apollographql.apollo.network.http.HttpInterceptor
88
import com.apollographql.apollo.network.http.HttpInterceptorChain
99
import io.sentry.BaggageHeader
1010
import io.sentry.Breadcrumb
11+
import io.sentry.DataCategory
1112
import io.sentry.Hint
1213
import io.sentry.IScopes
1314
import io.sentry.ISpan
@@ -21,6 +22,7 @@ import io.sentry.SpanDataConvention.HTTP_METHOD_KEY
2122
import io.sentry.SpanStatus
2223
import io.sentry.TypeCheckHint.APOLLO_REQUEST
2324
import io.sentry.TypeCheckHint.APOLLO_RESPONSE
25+
import io.sentry.clientreport.DiscardReason
2426
import io.sentry.exception.ExceptionMechanismException
2527
import io.sentry.protocol.Mechanism
2628
import io.sentry.protocol.Request
@@ -215,6 +217,7 @@ constructor(
215217
span.setData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY, it)
216218
}
217219
if (beforeSpan != null) {
220+
val wasSampled = span.isSampled == true
218221
try {
219222
val result = beforeSpan.execute(span, request, response)
220223
if (result == null) {
@@ -223,6 +226,12 @@ constructor(
223226
}
224227
} catch (e: Throwable) {
225228
span.spanContext.sampled = false
229+
if (wasSampled) {
230+
scopes.options.clientReportRecorder.recordLostEvent(
231+
DiscardReason.CALLBACK_ERROR,
232+
DataCategory.Span,
233+
)
234+
}
226235
scopes.options.logger.log(
227236
SentryLevel.ERROR,
228237
"An error occurred while executing beforeSpan in ApolloInterceptor",

‎sentry-apollo-4/src/test/java/io/sentry/apollo4/SentryApollo4HttpInterceptorTest.kt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.apollographql.apollo.network.http.HttpInterceptorChain
1313
import com.google.common.truth.Truth.assertThat
1414
import io.sentry.BaggageHeader
1515
import io.sentry.Breadcrumb
16+
import io.sentry.DataCategory
1617
import io.sentry.ILogger
1718
import io.sentry.IScopes
1819
import io.sentry.ITransaction
@@ -32,6 +33,7 @@ import io.sentry.TransactionContext
3233
import io.sentry.W3CTraceparentHeader
3334
import io.sentry.apollo4.SentryApollo4HttpInterceptor.BeforeSpanCallback
3435
import io.sentry.apollo4.generated.LaunchDetailsQuery
36+
import io.sentry.clientreport.DiscardReason
3537
import io.sentry.mockServerRequestTimeoutMillis
3638
import io.sentry.protocol.SdkVersion
3739
import io.sentry.protocol.SentryTransaction
@@ -55,6 +57,7 @@ import org.mockito.kotlin.check
5557
import org.mockito.kotlin.doAnswer
5658
import org.mockito.kotlin.mock
5759
import org.mockito.kotlin.verify
60+
import org.mockito.kotlin.verifyNoMoreInteractions
5861
import org.mockito.kotlin.whenever
5962

6063
class SentryApollo4HttpInterceptorTestWithV4Implementation :
@@ -308,7 +311,10 @@ abstract class SentryApollo4HttpInterceptorTest(
308311

309312
@Test
310313
fun `returning null in beforeSpan callback drops span`() {
314+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
315+
fixture.options.onDiscard = onDiscard
311316
executeQuery(fixture.getSut(beforeSpan = { _, _, _ -> null }))
317+
verifyNoMoreInteractions(onDiscard)
312318

313319
verify(fixture.scopes)
314320
.captureTransaction(
@@ -319,6 +325,32 @@ abstract class SentryApollo4HttpInterceptorTest(
319325
)
320326
}
321327

328+
@Test
329+
fun `reports callback errors only for sampled spans`(): Unit = runBlocking {
330+
for (sampled in listOf(true, false, null)) {
331+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
332+
fixture.options.onDiscard = onDiscard
333+
val tx = SentryTracer(TransactionContext("op", "desc"), fixture.scopes)
334+
tx.spanContext.sampled = sampled
335+
whenever(fixture.scopes.span).thenReturn(tx)
336+
val sut =
337+
fixture.getSut(
338+
beforeSpan = { span, _, _ ->
339+
span.spanContext.sampled = false
340+
throw IllegalStateException("callback failed")
341+
}
342+
)
343+
344+
assertThat(executeQueryImplementation(sut.query(LaunchDetailsQuery("83"))).data).isNotNull()
345+
tx.finish()
346+
347+
if (sampled == true) {
348+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1)
349+
}
350+
verifyNoMoreInteractions(onDiscard)
351+
}
352+
}
353+
322354
@Test
323355
fun `when beforeSpan throws, drops span and preserves response`(): Unit = runBlocking {
324356
val failure = IllegalStateException("callback failed")

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.apollographql.apollo.interceptor.ApolloInterceptorChain
1414
import com.apollographql.apollo.request.RequestHeaders
1515
import io.sentry.BaggageHeader
1616
import io.sentry.Breadcrumb
17+
import io.sentry.DataCategory
1718
import io.sentry.Hint
1819
import io.sentry.IScopes
1920
import io.sentry.ISpan
@@ -24,6 +25,7 @@ import io.sentry.SpanDataConvention
2425
import io.sentry.SpanStatus
2526
import io.sentry.TypeCheckHint.APOLLO_REQUEST
2627
import io.sentry.TypeCheckHint.APOLLO_RESPONSE
28+
import io.sentry.clientreport.DiscardReason
2729
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
2830
import io.sentry.util.SpanUtils
2931
import io.sentry.util.TracingUtils
@@ -175,10 +177,17 @@ class SentryApolloInterceptor(
175177
) {
176178
var newSpan: ISpan? = span
177179
if (beforeSpan != null) {
180+
val wasSampled = span.isSampled == true
178181
try {
179182
newSpan = beforeSpan.execute(span, request, response)
180183
} catch (e: Exception) {
181184
span.spanContext.sampled = false
185+
if (wasSampled) {
186+
scopes.options.clientReportRecorder.recordLostEvent(
187+
DiscardReason.CALLBACK_ERROR,
188+
DataCategory.Span,
189+
)
190+
}
182191
scopes.options.logger.log(
183192
SentryLevel.ERROR,
184193
"An error occurred while executing beforeSpan on ApolloInterceptor",

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.apollographql.apollo.exception.ApolloException
66
import com.google.common.truth.Truth.assertThat
77
import io.sentry.BaggageHeader
88
import io.sentry.Breadcrumb
9+
import io.sentry.DataCategory
910
import io.sentry.ILogger
1011
import io.sentry.IScopes
1112
import io.sentry.ITransaction
@@ -20,6 +21,7 @@ import io.sentry.SpanStatus
2021
import io.sentry.TraceContext
2122
import io.sentry.TracesSamplingDecision
2223
import io.sentry.TransactionContext
24+
import io.sentry.clientreport.DiscardReason
2325
import io.sentry.mockServerRequestTimeoutMillis
2426
import io.sentry.protocol.SdkVersion
2527
import io.sentry.protocol.SentryTransaction
@@ -42,6 +44,7 @@ import org.mockito.kotlin.check
4244
import org.mockito.kotlin.doAnswer
4345
import org.mockito.kotlin.mock
4446
import org.mockito.kotlin.verify
47+
import org.mockito.kotlin.verifyNoMoreInteractions
4548
import org.mockito.kotlin.whenever
4649

4750
class SentryApolloInterceptorTest {
@@ -232,7 +235,10 @@ class SentryApolloInterceptorTest {
232235

233236
@Test
234237
fun `when beforeSpan callback returns null, span is dropped`() {
238+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
239+
fixture.options.onDiscard = onDiscard
235240
executeQuery(fixture.getSut { _, _, _ -> null })
241+
verifyNoMoreInteractions(onDiscard)
236242

237243
verify(fixture.scopes)
238244
.captureTransaction(
@@ -243,6 +249,29 @@ class SentryApolloInterceptorTest {
243249
)
244250
}
245251

252+
@Test
253+
fun `reports callback errors only for sampled spans`(): Unit = runBlocking {
254+
for (sampled in listOf(true, false, null)) {
255+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
256+
fixture.options.onDiscard = onDiscard
257+
val tx = SentryTracer(TransactionContext("op", "desc"), fixture.scopes)
258+
tx.spanContext.sampled = sampled
259+
whenever(fixture.scopes.span).thenReturn(tx)
260+
val sut = fixture.getSut { span, _, _ ->
261+
span.spanContext.sampled = false
262+
throw IllegalStateException("callback failed")
263+
}
264+
265+
assertThat(sut.query(LaunchDetailsQuery.builder().id("83").build()).await().data).isNotNull()
266+
tx.finish()
267+
268+
if (sampled == true) {
269+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1)
270+
}
271+
verifyNoMoreInteractions(onDiscard)
272+
}
273+
}
274+
246275
@Test
247276
fun `when beforeSpan throws, drops span and preserves response`(): Unit = runBlocking {
248277
val failure = IllegalStateException("callback failed")

‎sentry-graphql-22/src/test/kotlin/io/sentry/graphql22/SentryInstrumentationTest.kt‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import graphql.schema.GraphQLScalarType
1919
import graphql.schema.idl.RuntimeWiring
2020
import graphql.schema.idl.SchemaGenerator
2121
import graphql.schema.idl.SchemaParser
22+
import io.sentry.DataCategory
2223
import io.sentry.ILogger
2324
import io.sentry.IScopes
2425
import io.sentry.Sentry
@@ -27,6 +28,7 @@ import io.sentry.SentryOptions
2728
import io.sentry.SentryTracer
2829
import io.sentry.SpanStatus
2930
import io.sentry.TransactionContext
31+
import io.sentry.clientreport.DiscardReason
3032
import io.sentry.graphql.ExceptionReporter
3133
import io.sentry.graphql.NoOpSubscriptionHandler
3234
import io.sentry.graphql.SentryGraphqlInstrumentation
@@ -43,6 +45,7 @@ import org.mockito.Mockito
4345
import org.mockito.kotlin.any
4446
import org.mockito.kotlin.mock
4547
import org.mockito.kotlin.verify
48+
import org.mockito.kotlin.verifyNoMoreInteractions
4649
import org.mockito.kotlin.whenever
4750

4851
class SentryInstrumentationTest {
@@ -56,7 +59,8 @@ class SentryInstrumentationTest {
5659
async: Boolean = false,
5760
beforeSpan: SentryGraphqlInstrumentation.BeforeSpanCallback? = null,
5861
): GraphQL {
59-
whenever(scopes.options).thenReturn(SentryOptions())
62+
whenever(scopes.options)
63+
.thenReturn(SentryOptions().apply { dsn = "https://key@sentry.io/proj" })
6064
activeSpan = SentryTracer(TransactionContext("name", "op"), scopes)
6165
val schema =
6266
"""
@@ -162,6 +166,9 @@ class SentryInstrumentationTest {
162166
fixture.getSut(
163167
beforeSpan = SentryGraphqlInstrumentation.BeforeSpanCallback { _, _, _ -> null }
164168
)
169+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
170+
fixture.scopes.options.onDiscard = onDiscard
171+
fixture.activeSpan.spanContext.sampled = true
165172

166173
withMockScopes {
167174
val result = sut.execute("{ shows { id } }")
@@ -172,6 +179,33 @@ class SentryInstrumentationTest {
172179
assertEquals("graphql", span.operation)
173180
assertEquals("Query.shows", span.description)
174181
assertNotNull(span.isSampled) { assertFalse(it) }
182+
verifyNoMoreInteractions(onDiscard)
183+
}
184+
}
185+
186+
@Test
187+
fun `reports callback errors only for sampled spans`() {
188+
for (sampled in listOf(true, false, null)) {
189+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
190+
val sut =
191+
fixture.getSut(
192+
beforeSpan = { span, _, _ ->
193+
span.spanContext.sampled = false
194+
throw IllegalStateException("callback failed")
195+
}
196+
)
197+
fixture.activeSpan.spanContext.sampled = sampled
198+
fixture.scopes.options.onDiscard = onDiscard
199+
200+
withMockScopes {
201+
assertThat(sut.execute("{ shows { id } }").errors).isEmpty()
202+
fixture.activeSpan.finish()
203+
}
204+
205+
if (sampled == true) {
206+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1)
207+
}
208+
verifyNoMoreInteractions(onDiscard)
175209
}
176210
}
177211

0 commit comments

Comments
 (0)