Skip to content

Commit d689526

Browse files
adinauerclaude
andcommitted
fix(core): [Callback Errors 2] Drop failed processor data
Stop processing and drop telemetry when an event processor throws. Record callback_error outcomes for every supported category instead of sending potentially partially processed data. Refs #6081 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 28af8f2 commit d689526

3 files changed

Lines changed: 231 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124

125125
- Fix SDK callback error handling ([#6140](https://github.com/getsentry/sentry-java/pull/6140))
126126
- Add `DiscardReason.CALLBACK_ERROR` and use it for telemetry dropped when a `beforeSend*` callback throws. `OnDiscardCallback` can now receive this value.
127+
- Drop telemetry and record `callback_error` when an event processor throws instead of continuing with a potentially partially processed item.
127128
- Disable URL caching when reading `META-INF/MANIFEST.MF` files during version detection so that the SDK no longer keeps jar file handles open for the life of the process ([#6124](https://github.com/getsentry/sentry-java/pull/6124)
128129
- Keep the `EventListener` wrapped by `SentryOkHttpEventListener` per `Call` ([#6003](https://github.com/getsentry/sentry-java/pull/6003))
129130

‎sentry/src/main/java/io/sentry/SentryClient.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ private SentryEvent processEvent(
506506
e,
507507
"An exception occurred while processing event by processor: %s",
508508
processor.getClass().getName());
509+
options
510+
.getClientReportRecorder()
511+
.recordLostEvent(DiscardReason.CALLBACK_ERROR, DataCategory.Error);
512+
return null;
509513
}
510514

511515
if (event == null) {
@@ -557,6 +561,8 @@ private SentryLogEvent processLogEvent(
557561
e,
558562
"An exception occurred while processing log event by processor: %s",
559563
processor.getClass().getName());
564+
recordLostLogEvent(DiscardReason.CALLBACK_ERROR, eventBeforeProcessor);
565+
return null;
560566
}
561567

562568
if (event == null) {
@@ -590,6 +596,8 @@ private SentryMetricsEvent processMetricsEvent(
590596
e,
591597
"An exception occurred while processing metrics event by processor: %s",
592598
processor.getClass().getName());
599+
recordLostMetricsEvent(DiscardReason.CALLBACK_ERROR, eventBeforeProcessor);
600+
return null;
593601
}
594602

595603
if (event == null) {
@@ -622,6 +630,14 @@ private SentryMetricsEvent processMetricsEvent(
622630
e,
623631
"An exception occurred while processing transaction by processor: %s",
624632
processor.getClass().getName());
633+
options
634+
.getClientReportRecorder()
635+
.recordLostEvent(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction);
636+
options
637+
.getClientReportRecorder()
638+
.recordLostEvent(
639+
DiscardReason.CALLBACK_ERROR, DataCategory.Span, spanCountBeforeProcessor + 1);
640+
return null;
625641
}
626642
final int spanCountAfterProcessor = transaction == null ? 0 : transaction.getSpans().size();
627643

@@ -675,6 +691,10 @@ private SentryReplayEvent processReplayEvent(
675691
e,
676692
"An exception occurred while processing replay event by processor: %s",
677693
processor.getClass().getName());
694+
options
695+
.getClientReportRecorder()
696+
.recordLostEvent(DiscardReason.CALLBACK_ERROR, DataCategory.Replay);
697+
return null;
678698
}
679699

680700
if (replayEvent == null) {
@@ -709,6 +729,10 @@ private SentryEvent processFeedbackEvent(
709729
e,
710730
"An exception occurred while processing feedback event by processor: %s",
711731
processor.getClass().getName());
732+
options
733+
.getClientReportRecorder()
734+
.recordLostEvent(DiscardReason.CALLBACK_ERROR, DataCategory.Feedback);
735+
return null;
712736
}
713737

714738
if (feedbackEvent == null) {

‎sentry/src/test/java/io/sentry/SentryClientTest.kt‎

Lines changed: 206 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry
22

3+
import com.google.common.truth.Truth.assertThat
34
import io.sentry.Scope.IWithPropagationContext
45
import io.sentry.SentryLevel.WARNING
56
import io.sentry.Session.State.Crashed
@@ -474,6 +475,48 @@ class SentryClientTest {
474475
)
475476
}
476477

478+
@Test
479+
fun `throwing log processor drops log and stops callbacks`() {
480+
val scope = createScope()
481+
val logEvent = SentryLogEvent(SentryId(), SentryNanotimeDate(), "message", SentryLogLevel.WARN)
482+
val logEventNumberOfBytes =
483+
JsonSerializationUtils.byteSizeOf(
484+
fixture.sentryOptions.serializer,
485+
fixture.sentryOptions.logger,
486+
logEvent,
487+
)
488+
val throwingProcessor = mock<EventProcessor>()
489+
val nextProcessor = mock<EventProcessor>()
490+
val beforeSend = mock<SentryOptions.Logs.BeforeSendLogCallback>()
491+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
492+
whenever(throwingProcessor.process(any<SentryLogEvent>()))
493+
.thenThrow(IllegalStateException("test"))
494+
scope.addEventProcessor(throwingProcessor)
495+
scope.addEventProcessor(nextProcessor)
496+
fixture.sentryOptions.logs.beforeSend = beforeSend
497+
fixture.sentryOptions.onDiscard = onDiscard
498+
499+
fixture.getSut().captureLog(logEvent, scope)
500+
501+
verify(nextProcessor, never()).process(any<SentryLogEvent>())
502+
verify(beforeSend, never()).execute(any())
503+
verify(fixture.loggerBatchProcessor, never()).add(any())
504+
assertClientReport(
505+
fixture.sentryOptions.clientReportRecorder,
506+
listOf(
507+
DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.LogItem.category, 1),
508+
DiscardedEvent(
509+
DiscardReason.CALLBACK_ERROR.reason,
510+
DataCategory.LogByte.category,
511+
logEventNumberOfBytes,
512+
),
513+
),
514+
)
515+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.LogItem, 1)
516+
verify(onDiscard)
517+
.execute(DiscardReason.CALLBACK_ERROR, DataCategory.LogByte, logEventNumberOfBytes)
518+
}
519+
477520
@Test
478521
fun `when beforeSendLog is returns new instance, new instance is sent`() {
479522
val scope = createScope()
@@ -595,6 +638,56 @@ class SentryClientTest {
595638
)
596639
}
597640

641+
@Test
642+
fun `throwing metric processor drops metric and stops callbacks`() {
643+
val scope = createScope()
644+
val metricsEvent = SentryMetricsEvent(SentryId(), SentryNanotimeDate(), "name", "gauge", 123.0)
645+
val metricsEventNumberOfBytes =
646+
JsonSerializationUtils.byteSizeOf(
647+
fixture.sentryOptions.serializer,
648+
fixture.sentryOptions.logger,
649+
metricsEvent,
650+
)
651+
val throwingProcessor = mock<EventProcessor>()
652+
val nextProcessor = mock<EventProcessor>()
653+
val beforeSend = mock<SentryOptions.Metrics.BeforeSendMetricCallback>()
654+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
655+
whenever(throwingProcessor.process(any<SentryMetricsEvent>(), anyOrNull()))
656+
.thenThrow(IllegalStateException("test"))
657+
scope.addEventProcessor(throwingProcessor)
658+
scope.addEventProcessor(nextProcessor)
659+
fixture.sentryOptions.metrics.beforeSend = beforeSend
660+
fixture.sentryOptions.onDiscard = onDiscard
661+
662+
fixture.getSut().captureMetric(metricsEvent, scope, null)
663+
664+
verify(nextProcessor, never()).process(any<SentryMetricsEvent>(), anyOrNull())
665+
verify(beforeSend, never()).execute(any(), anyOrNull())
666+
verify(fixture.metricsBatchProcessor, never()).add(any())
667+
assertClientReport(
668+
fixture.sentryOptions.clientReportRecorder,
669+
listOf(
670+
DiscardedEvent(
671+
DiscardReason.CALLBACK_ERROR.reason,
672+
DataCategory.TraceMetric.category,
673+
1,
674+
),
675+
DiscardedEvent(
676+
DiscardReason.CALLBACK_ERROR.reason,
677+
DataCategory.TraceMetricByte.category,
678+
metricsEventNumberOfBytes,
679+
),
680+
),
681+
)
682+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.TraceMetric, 1)
683+
verify(onDiscard)
684+
.execute(
685+
DiscardReason.CALLBACK_ERROR,
686+
DataCategory.TraceMetricByte,
687+
metricsEventNumberOfBytes,
688+
)
689+
}
690+
598691
@Test
599692
fun `when beforeSendMetric is returns new instance, new instance is sent`() {
600693
val scope = createScope()
@@ -1242,6 +1335,42 @@ class SentryClientTest {
12421335
)
12431336
}
12441337

1338+
@Test
1339+
fun `throwing transaction processor drops transaction and stops callbacks`() {
1340+
val throwingProcessor = mock<EventProcessor>()
1341+
val nextProcessor = mock<EventProcessor>()
1342+
val beforeSend = mock<SentryOptions.BeforeSendTransactionCallback>()
1343+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
1344+
whenever(throwingProcessor.process(any<SentryTransaction>(), anyOrNull()))
1345+
.thenThrow(IllegalStateException("test"))
1346+
fixture.sentryOptions.addEventProcessor(throwingProcessor)
1347+
fixture.sentryOptions.addEventProcessor(nextProcessor)
1348+
fixture.sentryOptions.beforeSendTransaction = beforeSend
1349+
fixture.sentryOptions.onDiscard = onDiscard
1350+
1351+
val id =
1352+
fixture
1353+
.getSut()
1354+
.captureTransaction(
1355+
SentryTransaction(fixture.sentryTracer),
1356+
fixture.sentryTracer.traceContext(),
1357+
)
1358+
1359+
assertThat(id).isEqualTo(SentryId.EMPTY_ID)
1360+
verify(nextProcessor, never()).process(any<SentryTransaction>(), anyOrNull())
1361+
verify(beforeSend, never()).execute(any(), anyOrNull())
1362+
verify(fixture.transport, never()).send(any(), anyOrNull())
1363+
assertClientReport(
1364+
fixture.sentryOptions.clientReportRecorder,
1365+
listOf(
1366+
DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Transaction.category, 1),
1367+
DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Span.category, 2),
1368+
),
1369+
)
1370+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1)
1371+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 2)
1372+
}
1373+
12451374
@Test
12461375
fun `transaction dropped by ignoredTransactions is recorded`() {
12471376
fixture.sentryOptions.setIgnoredTransactions(listOf("a-transaction"))
@@ -1927,10 +2056,29 @@ class SentryClientTest {
19272056
}
19282057

19292058
@Test
1930-
fun `exception thrown by an event processor is handled gracefully`() {
1931-
fixture.sentryOptions.addEventProcessor(eventProcessorThrows())
1932-
val sut = fixture.getSut()
1933-
sut.captureEvent(SentryEvent())
2059+
fun `exception thrown by an event processor drops event and stops callbacks`() {
2060+
val throwingProcessor = mock<EventProcessor>()
2061+
val nextProcessor = mock<EventProcessor>()
2062+
val beforeSend = mock<SentryOptions.BeforeSendCallback>()
2063+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
2064+
whenever(throwingProcessor.process(any<SentryEvent>(), anyOrNull()))
2065+
.thenThrow(IllegalStateException("test"))
2066+
fixture.sentryOptions.addEventProcessor(throwingProcessor)
2067+
fixture.sentryOptions.addEventProcessor(nextProcessor)
2068+
fixture.sentryOptions.beforeSend = beforeSend
2069+
fixture.sentryOptions.onDiscard = onDiscard
2070+
2071+
val id = fixture.getSut().captureEvent(SentryEvent())
2072+
2073+
assertThat(id).isEqualTo(SentryId.EMPTY_ID)
2074+
verify(nextProcessor, never()).process(any<SentryEvent>(), anyOrNull())
2075+
verify(beforeSend, never()).execute(any(), anyOrNull())
2076+
verify(fixture.transport, never()).send(any(), anyOrNull())
2077+
assertClientReport(
2078+
fixture.sentryOptions.clientReportRecorder,
2079+
listOf(DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Error.category, 1)),
2080+
)
2081+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Error, 1)
19342082
}
19352083

19362084
@Test
@@ -3524,6 +3672,32 @@ class SentryClientTest {
35243672
verify(onDiscardMock, times(1)).execute(DiscardReason.EVENT_PROCESSOR, DataCategory.Replay, 1)
35253673
}
35263674

3675+
@Test
3676+
fun `throwing replay processor drops replay and stops callbacks`() {
3677+
val throwingProcessor = mock<EventProcessor>()
3678+
val nextProcessor = mock<EventProcessor>()
3679+
val beforeSend = mock<SentryOptions.BeforeSendReplayCallback>()
3680+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
3681+
whenever(throwingProcessor.process(any<SentryReplayEvent>(), anyOrNull()))
3682+
.thenThrow(IllegalStateException("test"))
3683+
fixture.sentryOptions.addEventProcessor(throwingProcessor)
3684+
fixture.sentryOptions.addEventProcessor(nextProcessor)
3685+
fixture.sentryOptions.beforeSendReplay = beforeSend
3686+
fixture.sentryOptions.onDiscard = onDiscard
3687+
3688+
val id = fixture.getSut().captureReplayEvent(createReplayEvent(), createScope(), null)
3689+
3690+
assertThat(id).isEqualTo(SentryId.EMPTY_ID)
3691+
verify(nextProcessor, never()).process(any<SentryReplayEvent>(), anyOrNull())
3692+
verify(beforeSend, never()).execute(any(), anyOrNull())
3693+
verify(fixture.transport, never()).send(any(), anyOrNull())
3694+
assertClientReport(
3695+
fixture.sentryOptions.clientReportRecorder,
3696+
listOf(DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Replay.category, 1)),
3697+
)
3698+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Replay, 1)
3699+
}
3700+
35273701
@Test
35283702
fun `calls captureReplay on replay controller for error events`() {
35293703
var called = false
@@ -4086,6 +4260,34 @@ class SentryClientTest {
40864260
verify(onDiscardMock, times(1)).execute(DiscardReason.EVENT_PROCESSOR, DataCategory.Feedback, 1)
40874261
}
40884262

4263+
@Test
4264+
fun `throwing feedback processor drops feedback and stops callbacks`() {
4265+
val throwingProcessor = mock<EventProcessor>()
4266+
val nextProcessor = mock<EventProcessor>()
4267+
val beforeSend = mock<SentryOptions.BeforeSendCallback>()
4268+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
4269+
whenever(throwingProcessor.process(any<SentryEvent>(), anyOrNull()))
4270+
.thenThrow(IllegalStateException("test"))
4271+
fixture.sentryOptions.addEventProcessor(throwingProcessor)
4272+
fixture.sentryOptions.addEventProcessor(nextProcessor)
4273+
fixture.sentryOptions.beforeSendFeedback = beforeSend
4274+
fixture.sentryOptions.onDiscard = onDiscard
4275+
4276+
val id = fixture.getSut().captureFeedback(Feedback("message"), null, createScope())
4277+
4278+
assertThat(id).isEqualTo(SentryId.EMPTY_ID)
4279+
verify(nextProcessor, never()).process(any<SentryEvent>(), anyOrNull())
4280+
verify(beforeSend, never()).execute(any(), anyOrNull())
4281+
verify(fixture.transport, never()).send(any(), anyOrNull())
4282+
assertClientReport(
4283+
fixture.sentryOptions.clientReportRecorder,
4284+
listOf(
4285+
DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Feedback.category, 1)
4286+
),
4287+
)
4288+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Feedback, 1)
4289+
}
4290+
40894291
// endregion
40904292

40914293
private fun givenScopeWithStartedSession(
@@ -4352,14 +4554,6 @@ class SentryClientTest {
43524554
override fun timestamp(): Long? = null
43534555
}
43544556

4355-
private fun eventProcessorThrows(): EventProcessor {
4356-
return object : EventProcessor {
4357-
override fun process(event: SentryEvent, hint: Hint): SentryEvent? {
4358-
throw Throwable()
4359-
}
4360-
}
4361-
}
4362-
43634557
private class BackfillableHint : Backfillable {
43644558
override fun shouldEnrich(): Boolean = false
43654559
}

0 commit comments

Comments
 (0)