Skip to content

Commit efb5dfb

Browse files
adinauerclaude
andcommitted
fix(core): [Callback Errors 8] Report dropped profiles
Record callback_error/profile when beforeSendTransaction or a customer transaction processor throws and an attached profile is dropped. Notify OnDiscardCallback through the same client-report accounting path. Cover scope and options processors, absent profiles, and SDK processor failures that retain profiles. Leave catch types, intentional drops, and profile-file cleanup unchanged. Refs #6081 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b62c094 commit efb5dfb

4 files changed

Lines changed: 134 additions & 5 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+
- Report attached profiles dropped by transaction callback errors as `callback_error` in client reports and `OnDiscardCallback`.
127128
- 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.
128129
- Drop breadcrumbs when `beforeBreadcrumb` throws instead of storing exception details on the breadcrumb.
129130
- Skip replay capture when `beforeErrorSampling` throws, while still sending the error event ([#6165](https://github.com/getsentry/sentry-java/pull/6165))

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,8 @@ private SentryMetricsEvent processMetricsEvent(
624624
private @Nullable SentryTransaction processTransaction(
625625
@NotNull SentryTransaction transaction,
626626
final @NotNull Hint hint,
627-
final @NotNull List<EventProcessor> eventProcessors) {
627+
final @NotNull List<EventProcessor> eventProcessors,
628+
final boolean hasProfile) {
628629
for (final EventProcessor processor : eventProcessors) {
629630
final int spanCountBeforeProcessor = transaction.getSpans().size();
630631
try {
@@ -645,6 +646,11 @@ private SentryMetricsEvent processMetricsEvent(
645646
.getClientReportRecorder()
646647
.recordLostEvent(
647648
DiscardReason.CALLBACK_ERROR, DataCategory.Span, spanCountBeforeProcessor + 1);
649+
if (hasProfile) {
650+
options
651+
.getClientReportRecorder()
652+
.recordLostEvent(DiscardReason.CALLBACK_ERROR, DataCategory.Profile);
653+
}
648654
return null;
649655
}
650656
}
@@ -1061,7 +1067,9 @@ public void captureSession(final @NotNull Session session, final @Nullable Hint
10611067
transaction = applyScope(transaction, scope, HintUtils.hasType(hint, Cached.class));
10621068

10631069
if (transaction != null && scope != null) {
1064-
transaction = processTransaction(transaction, hint, scope.getEventProcessors());
1070+
transaction =
1071+
processTransaction(
1072+
transaction, hint, scope.getEventProcessors(), profilingTraceData != null);
10651073
}
10661074

10671075
if (transaction == null) {
@@ -1070,15 +1078,17 @@ public void captureSession(final @NotNull Session session, final @Nullable Hint
10701078
}
10711079

10721080
if (transaction != null) {
1073-
transaction = processTransaction(transaction, hint, options.getEventProcessors());
1081+
transaction =
1082+
processTransaction(
1083+
transaction, hint, options.getEventProcessors(), profilingTraceData != null);
10741084
}
10751085

10761086
if (transaction == null) {
10771087
options.getLogger().log(SentryLevel.DEBUG, "Transaction was dropped by Event processors.");
10781088
return SentryId.EMPTY_ID;
10791089
}
10801090

1081-
transaction = executeBeforeSendTransaction(transaction, hint);
1091+
transaction = executeBeforeSendTransaction(transaction, hint, profilingTraceData != null);
10821092

10831093
if (transaction == null) {
10841094
options
@@ -1672,7 +1682,7 @@ private void sortBreadcrumbsByDate(
16721682
}
16731683

16741684
private @Nullable SentryTransaction executeBeforeSendTransaction(
1675-
@NotNull SentryTransaction transaction, final @NotNull Hint hint) {
1685+
@NotNull SentryTransaction transaction, final @NotNull Hint hint, final boolean hasProfile) {
16761686
final SentryOptions.BeforeSendTransactionCallback beforeSendTransaction =
16771687
options.getBeforeSendTransaction();
16781688
if (beforeSendTransaction != null) {
@@ -1693,6 +1703,11 @@ private void sortBreadcrumbsByDate(
16931703
.getClientReportRecorder()
16941704
.recordLostEvent(
16951705
DiscardReason.CALLBACK_ERROR, DataCategory.Span, spanCountBeforeCallback + 1);
1706+
if (hasProfile) {
1707+
options
1708+
.getClientReportRecorder()
1709+
.recordLostEvent(DiscardReason.CALLBACK_ERROR, DataCategory.Profile);
1710+
}
16961711
return null;
16971712
}
16981713

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ class SentryClientInternalEventProcessorTest(private val onScope: Boolean) {
100100
assertFailureLoggedWithoutLoss("transaction")
101101
}
102102

103+
@Test
104+
fun `SDK transaction processor failure keeps attached profile without reporting loss`() {
105+
val transaction = SentryTransaction(fixture.sentryTracer)
106+
whenever(processor.process(any<SentryTransaction>(), any())).thenThrow(failure)
107+
whenever(nextProcessor.process(any<SentryTransaction>(), any())).thenAnswer { it.arguments[0] }
108+
109+
val id =
110+
fixture
111+
.getSut()
112+
.captureTransaction(
113+
transaction,
114+
fixture.sentryTracer.traceContext(),
115+
scope,
116+
null,
117+
fixture.profilingTraceData,
118+
)
119+
120+
assertThat(id).isEqualTo(transaction.eventId)
121+
verify(fixture.transport)
122+
.send(
123+
check {
124+
assertThat(it.items.map { item -> item.header.type })
125+
.containsExactly(SentryItemType.Transaction, SentryItemType.Profile)
126+
},
127+
anyOrNull(),
128+
)
129+
assertFailureLoggedWithoutLoss("transaction")
130+
}
131+
103132
@Test
104133
fun `SDK feedback processor failure keeps feedback and runs remaining callbacks`() {
105134
val feedback = Feedback("message")
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.sentry
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import io.sentry.clientreport.ClientReportTestHelper.Companion.assertClientReport
5+
import io.sentry.clientreport.DiscardReason
6+
import io.sentry.clientreport.DiscardedEvent
7+
import io.sentry.protocol.SentryId
8+
import io.sentry.protocol.SentryTransaction
9+
import kotlin.test.Test
10+
import org.junit.runner.RunWith
11+
import org.junit.runners.Parameterized
12+
import org.mockito.kotlin.any
13+
import org.mockito.kotlin.anyOrNull
14+
import org.mockito.kotlin.mock
15+
import org.mockito.kotlin.never
16+
import org.mockito.kotlin.verify
17+
import org.mockito.kotlin.verifyNoMoreInteractions
18+
import org.mockito.kotlin.whenever
19+
20+
@RunWith(Parameterized::class)
21+
class SentryClientTransactionProfileTest(
22+
private val callback: String,
23+
private val hasProfile: Boolean,
24+
) {
25+
companion object {
26+
@JvmStatic
27+
@Parameterized.Parameters(name = "callback={0}, hasProfile={1}")
28+
fun data(): List<Array<Any>> =
29+
listOf("scope processor", "options processor", "beforeSendTransaction").flatMap { callback ->
30+
listOf(false, true).map { hasProfile -> arrayOf<Any>(callback, hasProfile) }
31+
}
32+
}
33+
34+
@Test
35+
fun `transaction callback failure reports attached profile exactly once`() {
36+
val fixture = SentryClientTest.Fixture()
37+
val options = fixture.sentryOptions
38+
options.eventProcessors.clear()
39+
val scope = Scope(options)
40+
val onDiscard = mock<SentryOptions.OnDiscardCallback>()
41+
options.onDiscard = onDiscard
42+
val failure = IllegalStateException("callback failed")
43+
if (callback == "beforeSendTransaction") {
44+
options.setBeforeSendTransaction { _, _ -> throw failure }
45+
} else {
46+
val processor = mock<EventProcessor>()
47+
whenever(processor.process(any<SentryTransaction>(), any())).thenThrow(failure)
48+
if (callback == "scope processor") {
49+
scope.addEventProcessor(processor)
50+
} else {
51+
options.addEventProcessor(processor)
52+
}
53+
}
54+
55+
val id =
56+
fixture
57+
.getSut()
58+
.captureTransaction(
59+
SentryTransaction(fixture.sentryTracer),
60+
fixture.sentryTracer.traceContext(),
61+
scope,
62+
null,
63+
if (hasProfile) fixture.profilingTraceData else null,
64+
)
65+
66+
assertThat(id).isEqualTo(SentryId.EMPTY_ID)
67+
verify(fixture.transport, never()).send(any(), anyOrNull())
68+
val expected =
69+
mutableListOf(
70+
DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Transaction.category, 1),
71+
DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Span.category, 2),
72+
)
73+
if (hasProfile) {
74+
expected.add(
75+
DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Profile.category, 1)
76+
)
77+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Profile, 1)
78+
}
79+
assertClientReport(options.clientReportRecorder, expected)
80+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1)
81+
verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 2)
82+
verifyNoMoreInteractions(onDiscard)
83+
}
84+
}

0 commit comments

Comments
 (0)