Skip to content

Commit db0ed76

Browse files
authored
Merge pull request #6238 from getsentry/fix/callback-error-handling-apollo-5
fix(apollo5): Handle beforeSpan callback failures
2 parents 92e9ef5 + 77b9784 commit db0ed76

3 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- Drop Apollo 5 spans when `beforeSpan` throws without disrupting the GraphQL request ([#6238](https://github.com/getsentry/sentry-java/pull/6238))
8+
59
### Features
610

711
- Add support for Android Navigation 3 through the new `sentry-android-navigation3` library ([#6233](https://github.com/getsentry/sentry-java/pull/6233))

‎sentry-apollo-5/src/main/java/io/sentry/apollo5/SentryApollo5HttpInterceptor.kt‎

Lines changed: 10 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
@@ -199,6 +201,7 @@ constructor(
199201
span.setData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY, it)
200202
}
201203
if (beforeSpan != null) {
204+
val wasSampled = span.isSampled == true
202205
try {
203206
val result = beforeSpan.execute(span, request, response)
204207
if (result == null) {
@@ -207,6 +210,13 @@ constructor(
207210
}
208211
} catch (e: Throwable) {
209212
ExceptionUtils.rethrowIfFatal(e)
213+
span.spanContext.sampled = false
214+
if (wasSampled) {
215+
scopes.options.clientReportRecorder.recordLostEvent(
216+
DiscardReason.CALLBACK_ERROR,
217+
DataCategory.Span,
218+
)
219+
}
210220
scopes.options.logger.log(
211221
SentryLevel.ERROR,
212222
"An error occurred while executing beforeSpan in ApolloInterceptor",

‎sentry-apollo-5/src/test/java/io/sentry/apollo5/SentryApollo5HttpInterceptorTest.kt‎

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.apollographql.apollo.network.http.HttpInterceptorChain
1414
import com.google.common.truth.Truth.assertThat
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.ITransaction
@@ -32,6 +33,7 @@ import io.sentry.TransactionContext
3233
import io.sentry.W3CTraceparentHeader
3334
import io.sentry.apollo5.SentryApollo5HttpInterceptor.BeforeSpanCallback
3435
import io.sentry.apollo5.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
@@ -40,6 +42,7 @@ import java.util.concurrent.TimeUnit
4042
import kotlin.reflect.KSuspendFunction1
4143
import kotlin.test.Test
4244
import kotlin.test.assertEquals
45+
import kotlin.test.assertFailsWith
4346
import kotlin.test.assertNotNull
4447
import kotlin.test.assertNull
4548
import kotlin.test.assertTrue
@@ -60,6 +63,7 @@ import org.mockito.kotlin.doAnswer
6063
import org.mockito.kotlin.mock
6164
import org.mockito.kotlin.never
6265
import org.mockito.kotlin.verify
66+
import org.mockito.kotlin.verifyNoMoreInteractions
6367
import org.mockito.kotlin.whenever
6468

6569
class SentryApollo5HttpInterceptorTestWithV5Implementation :
@@ -421,16 +425,83 @@ abstract class SentryApollo5HttpInterceptorTest(
421425
}
422426

423427
@Test
424-
fun `when customizer throws, exception is handled`() {
425-
executeQuery(fixture.getSut(beforeSpan = { _, _, _ -> throw RuntimeException() }))
428+
fun `reports callback errors only for sampled spans`(): Unit = runBlocking {
429+
for (sampled in listOf(true, false, null)) {
430+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
431+
fixture.options.onDiscard = onDiscard
432+
val tx = SentryTracer(TransactionContext("op", "desc"), fixture.scopes)
433+
tx.spanContext.sampled = sampled
434+
whenever(fixture.scopes.span).thenReturn(tx)
435+
val sut =
436+
fixture.getSut(
437+
beforeSpan = { span, _, _ ->
438+
span.spanContext.sampled = false
439+
throw IllegalStateException("callback failed")
440+
}
441+
)
442+
443+
assertThat(executeQueryImplementation(sut.query(LaunchDetailsQuery("83"))).data).isNotNull()
444+
tx.finish()
445+
446+
if (sampled == true) {
447+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1)
448+
}
449+
verifyNoMoreInteractions(onDiscard)
450+
}
451+
}
426452

453+
@Test
454+
fun `when beforeSpan throws, drops span and preserves response`(): Unit = runBlocking {
455+
val failure = IllegalStateException("callback failed")
456+
val tx =
457+
SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes)
458+
whenever(fixture.scopes.span).thenReturn(tx)
459+
val sut =
460+
fixture.getSut(
461+
beforeSpan = { span, _, _ ->
462+
span.description = "partially modified"
463+
throw failure
464+
}
465+
)
466+
467+
val response = executeQueryImplementation(sut.query(LaunchDetailsQuery("83")))
468+
assertThat(response.data).isNotNull()
469+
val span = tx.children.single()
470+
assertThat(span.isSampled).isFalse()
471+
assertThat(span.isFinished).isTrue()
472+
tx.finish()
427473
verify(fixture.scopes)
428474
.captureTransaction(
429-
check { assertEquals(1, it.spans.size) },
475+
check { assertThat(it.spans).isEmpty() },
430476
anyOrNull<TraceContext>(),
431477
anyOrNull(),
432478
anyOrNull(),
433479
)
480+
verify(fixture.scopes).addBreadcrumb(any<Breadcrumb>(), anyOrNull())
481+
}
482+
483+
@Test
484+
fun `fatal error from beforeSpan is not swallowed`(): Unit = runBlocking {
485+
val failure = OutOfMemoryError("callback failed")
486+
val tx =
487+
SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes)
488+
whenever(fixture.scopes.span).thenReturn(tx)
489+
val request = HttpRequest.Builder(HttpMethod.Post, "https://example.com/graphql").build()
490+
val response = HttpResponse.Builder(200).build()
491+
val chain =
492+
object : HttpInterceptorChain {
493+
override suspend fun proceed(request: HttpRequest): HttpResponse = response
494+
}
495+
val interceptor =
496+
SentryApollo5HttpInterceptor(
497+
fixture.scopes,
498+
beforeSpan = { _, _, _ -> throw failure },
499+
captureFailedRequests = false,
500+
)
501+
502+
val thrown = assertFailsWith<OutOfMemoryError> { interceptor.intercept(request, chain) }
503+
504+
assertThat(thrown).isSameInstanceAs(failure)
434505
}
435506

436507
@Test

0 commit comments

Comments
 (0)