Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer send event / transaction to Sentry if beforeSend / beforeSendTransaction throws #2591

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixes

- Deprecate reportFullDisplayed in favor of reportFullyDisplayed ([#2585](https://github.com/getsentry/sentry-java/pull/2585))
- No longer send event / transaction to Sentry if `beforeSend` / `beforeSendTransaction` throws ([#2591](https://github.com/getsentry/sentry-java/pull/2591))

## 6.15.0

Expand Down
22 changes: 4 additions & 18 deletions sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,15 +705,8 @@ private void sortBreadcrumbsByDate(
"The BeforeSend callback threw an exception. It will be added as breadcrumb and continue.",
e);

final Breadcrumb breadcrumb = new Breadcrumb();
breadcrumb.setMessage("BeforeSend callback failed.");
breadcrumb.setCategory("SentryClient");
breadcrumb.setLevel(SentryLevel.ERROR);
if (e.getMessage() != null) {
breadcrumb.setData("sentry:message", e.getMessage());
}

event.addBreadcrumb(breadcrumb);
// drop event in case of an error in beforeSend due to PII concerns
event = null;
}
}
return event;
Expand All @@ -734,15 +727,8 @@ private void sortBreadcrumbsByDate(
"The BeforeSendTransaction callback threw an exception. It will be added as breadcrumb and continue.",
e);

final Breadcrumb breadcrumb = new Breadcrumb();
breadcrumb.setMessage("BeforeSendTransaction callback failed.");
breadcrumb.setCategory("SentryClient");
breadcrumb.setLevel(SentryLevel.ERROR);
if (e.getMessage() != null) {
breadcrumb.setData("sentry:message", e.getMessage());
}

transaction.addBreadcrumb(breadcrumb);
// drop transaction in case of an error in beforeSend due to PII concerns
transaction = null;
}
}
return transaction;
Expand Down
32 changes: 16 additions & 16 deletions sentry/src/test/java/io/sentry/SentryClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,21 @@ class SentryClientTest {
}

@Test
fun `when beforeSend throws an exception, breadcrumb is added and event is sent`() {
fun `when beforeSend throws an exception, event is dropped`() {
val exception = Exception("test")

exception.stackTrace.toString()
fixture.sentryOptions.setBeforeSend { _, _ -> throw exception }
val sut = fixture.getSut()
val actual = SentryEvent()
sut.captureEvent(actual)
val id = sut.captureEvent(actual)

assertNotNull(actual.breadcrumbs) {
assertEquals("test", it.first().data["sentry:message"])
assertEquals("SentryClient", it.first().category)
assertEquals(SentryLevel.ERROR, it.first().level)
assertEquals("BeforeSend callback failed.", it.first().message)
}
assertEquals(SentryId.EMPTY_ID, id)

assertClientReport(
fixture.sentryOptions.clientReportRecorder,
listOf(DiscardedEvent(DiscardReason.BEFORE_SEND.reason, DataCategory.Error.category, 1))
)
}

@Test
Expand Down Expand Up @@ -808,21 +808,21 @@ class SentryClientTest {
}

@Test
fun `when beforeSendTransaction throws an exception, breadcrumb is added and event is sent`() {
fun `when beforeSendTransaction throws an exception, transaction is dropped`() {
val exception = Exception("test")

exception.stackTrace.toString()
fixture.sentryOptions.setBeforeSendTransaction { _, _ -> throw exception }

val transaction = SentryTransaction(fixture.sentryTracer)
fixture.getSut().captureTransaction(transaction, fixture.sentryTracer.traceContext())
val id = fixture.getSut().captureTransaction(transaction, fixture.sentryTracer.traceContext())

assertNotNull(transaction.breadcrumbs) {
assertEquals("test", it.first().data["sentry:message"])
assertEquals("SentryClient", it.first().category)
assertEquals(SentryLevel.ERROR, it.first().level)
assertEquals("BeforeSendTransaction callback failed.", it.first().message)
}
assertEquals(SentryId.EMPTY_ID, id)

assertClientReport(
fixture.sentryOptions.clientReportRecorder,
listOf(DiscardedEvent(DiscardReason.BEFORE_SEND.reason, DataCategory.Transaction.category, 1))
)
}

@Test
Expand Down