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

Add shouldSendEvent callback #428

Merged
merged 5 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 22 additions & 3 deletions sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.sentry.event.Event;
import io.sentry.event.EventBuilder;
import io.sentry.event.helper.EventBuilderHelper;
import io.sentry.event.helper.ShouldSendEventCallback;
import io.sentry.event.interfaces.ExceptionInterface;
import io.sentry.util.Util;
import org.slf4j.Logger;
Expand All @@ -28,7 +29,6 @@ public class SentryClient {
// CHECKSTYLE.OFF: ConstantName
private static final Logger lockdownLogger = LoggerFactory.getLogger(SentryClient.class.getName() + ".lockdown");
// CHECKSTYLE.ON: ConstantName

/**
* Identifies the version of the application.
* <p>
Expand Down Expand Up @@ -66,6 +66,10 @@ public class SentryClient {
* the {@link #extraTags} set will be extracted and set as tags on the {@link Event}.
*/
protected Set<String> extraTags = new HashSet<>();
/**
* Set of callbacks that are checked before each {@link Event} is sent to Sentry.
*/
private final Set<ShouldSendEventCallback> shouldSendEventCallbacks = new HashSet<>();
/**
* The underlying {@link Connection} to use for sending events to Sentry.
*/
Expand Down Expand Up @@ -112,6 +116,13 @@ public void runBuilderHelpers(EventBuilder eventBuilder) {
* @param event event to send to Sentry.
*/
public void sendEvent(Event event) {
for (ShouldSendEventCallback shouldSendEventCallback : shouldSendEventCallbacks) {
if (!shouldSendEventCallback.shouldSend(event)) {
logger.trace("Not sending Event because of ShouldSendEventCallback: {}", shouldSendEventCallback);
return;
}
}

try {
connection.send(event);
} catch (LockedDownException e) {
Expand Down Expand Up @@ -318,15 +329,23 @@ public void addExtraTag(String extraName) {
}

/**
* Add a callback that is called when an exception occurs while attempting to
* send events to the Sentry server.
* Add a callback that is called after an {@link Event} is sent to Sentry.
*
* @param eventSendCallback callback instance
*/
void addEventSendCallback(EventSendCallback eventSendCallback) {
connection.addEventSendCallback(eventSendCallback);
}

/**
* Add a callback that is called before an {@link Event} is sent to Sentry.
*
* @param shouldSendEventCallback callback instance
*/
void addShouldSendEventCallback(ShouldSendEventCallback shouldSendEventCallback) {
shouldSendEventCallbacks.add(shouldSendEventCallback);
}

@Override
public String toString() {
return "SentryClient{"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sentry.event.helper;

import io.sentry.event.Event;

/**
* Callback that decides whether or not an {@link Event} should be sent to Sentry.
*/
public interface ShouldSendEventCallback {

/**
* Callback that decides whether or not an {@link Event} should be sent to Sentry.
*
* @param event {@link Event} that may be sent.
* @return true if the {@link Event} should be sent, false otherwise.
*/
boolean shouldSend(Event event);
}
40 changes: 40 additions & 0 deletions sentry/src/test/java/io/sentry/SentryClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.sentry.context.ContextManager;
import io.sentry.context.SingletonContextManager;
import io.sentry.event.helper.ShouldSendEventCallback;
import mockit.Injectable;
import mockit.NonStrictExpectations;
import mockit.Tested;
Expand All @@ -17,6 +18,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -189,4 +191,42 @@ public void testFields() throws Exception {
assertThat(event.getTags(), equalTo(tags));
}};
}

@Test
public void testShouldNotSendEvent() throws Exception {
final AtomicBoolean called = new AtomicBoolean(false);
sentryClient.addShouldSendEventCallback(new ShouldSendEventCallback() {
@Override
public boolean shouldSend(Event event) {
called.set(true);
return false;
}
});

sentryClient.sendEvent(mockEvent);

new Verifications() {{
mockConnection.send(mockEvent); times = 0;
assertThat(called.get(), is(true));
}};
}

@Test
public void testShouldSendEvent() throws Exception {
final AtomicBoolean called = new AtomicBoolean(false);
sentryClient.addShouldSendEventCallback(new ShouldSendEventCallback() {
@Override
public boolean shouldSend(Event event) {
called.set(true);
return true;
}
});

sentryClient.sendEvent(mockEvent);

new Verifications() {{
mockConnection.send(mockEvent);
assertThat(called.get(), is(true));
}};
}
}