Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sentry-core/src/main/java/io/sentry/core/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ public void close() {
public void addBreadcrumb(Breadcrumb breadcrumb) {
StackItem item = stack.peek();
if (item != null) {
item.scope.addBreadcrumb(breadcrumb);
SentryOptions.BeforeBreadcrumbCallback callback = options.getBeforeBreadcrumb();
if (callback != null) {
breadcrumb = callback.execute(breadcrumb);
}
if (breadcrumb != null) {
item.scope.addBreadcrumb(breadcrumb);
}
} else {
log(options.getLogger(), SentryLevel.FATAL, "Stack peek was NULL when addBreadcrumb");
}
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/main/java/io/sentry/core/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SentryId captureEvent(SentryEvent event, @Nullable Scope scope) {
processor.process(event);
}

SentryOptions.BeforeSecondCallback beforeSend = options.getBeforeSend();
SentryOptions.BeforeSendCallback beforeSend = options.getBeforeSend();
if (beforeSend != null) {
event = beforeSend.execute(event);
if (event == null) {
Expand Down
21 changes: 17 additions & 4 deletions sentry-core/src/main/java/io/sentry/core/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class SentryOptions {
private SentryLevel diagnosticLevel = DEFAULT_DIAGNOSTIC_LEVEL;
private ISerializer serializer;
private String sentryClientName;
private BeforeSecondCallback beforeSend;
private BeforeSendCallback beforeSend;
private BeforeBreadcrumbCallback beforeBreadcrumb;
private String cacheDirPath;

public void addEventProcessor(EventProcessor eventProcessor) {
Expand Down Expand Up @@ -104,14 +105,22 @@ public void setSentryClientName(String sentryClientName) {
this.sentryClientName = sentryClientName;
}

public BeforeSecondCallback getBeforeSend() {
public BeforeSendCallback getBeforeSend() {
return beforeSend;
}

public void setBeforeSend(BeforeSecondCallback beforeSend) {
public void setBeforeSend(BeforeSendCallback beforeSend) {
this.beforeSend = beforeSend;
}

public BeforeBreadcrumbCallback getBeforeBreadcrumb() {
return beforeBreadcrumb;
}

public void setBeforeBreadcrumb(BeforeBreadcrumbCallback beforeBreadcrumb) {
this.beforeBreadcrumb = beforeBreadcrumb;
}

public String getCacheDirPath() {
return cacheDirPath;
}
Expand All @@ -120,10 +129,14 @@ public void setCacheDirPath(String cacheDirPath) {
this.cacheDirPath = cacheDirPath;
}

public interface BeforeSecondCallback {
public interface BeforeSendCallback {
SentryEvent execute(SentryEvent event);
}

public interface BeforeBreadcrumbCallback {
Breadcrumb execute(Breadcrumb breadcrumb);
}

public SentryOptions() {
integrations.add(new UncaughtExceptionHandlerIntegration());
}
Expand Down
42 changes: 41 additions & 1 deletion sentry-core/src/test/java/io/sentry/core/HubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,52 @@ class HubTest {
}

@Test
fun `when hub is initialized, integrations are registed`() {
fun `when hub is initialized, integrations are registered`() {
val integrationMock = mock<Integration>()
val options = SentryOptions()
options.dsn = "https://[email protected]/proj"
options.addIntegration(integrationMock)
val expected = Hub(options)
verify(integrationMock).register(expected, options)
}

@Test
fun `when beforeBreadcrumb returns null, crumb is dropped`() {
val options = SentryOptions()
options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { null }
options.dsn = "https://[email protected]/proj"
val sut = Hub(options)
sut.addBreadcrumb(Breadcrumb())
var breadcrumbs: List<Breadcrumb>? = null
sut.configureScope { breadcrumbs = it.breadcrumbs }
assertEquals(0, breadcrumbs!!.size)
}

@Test
fun `when beforeBreadcrumb modifies crumb, crumb is stored modified`() {
val options = SentryOptions()
val expected = "expected"
options.beforeBreadcrumb = SentryOptions.BeforeBreadcrumbCallback { it.message = expected; it }
options.dsn = "https://[email protected]/proj"
val sut = Hub(options)
var crumb = Breadcrumb()
crumb.message = "original"
sut.addBreadcrumb(crumb)
var breadcrumbs: List<Breadcrumb>? = null
sut.configureScope { breadcrumbs = it.breadcrumbs }
assertEquals(expected, breadcrumbs!!.first().message)
}

@Test
fun `when beforeBreadcrumb is null, crumb is stored`() {
val options = SentryOptions()
options.beforeBreadcrumb = null
options.dsn = "https://[email protected]/proj"
val sut = Hub(options)
var expected = Breadcrumb()
sut.addBreadcrumb(expected)
var breadcrumbs: List<Breadcrumb>? = null
sut.configureScope { breadcrumbs = it.breadcrumbs }
assertEquals(expected, breadcrumbs!!.single())
}
}