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 1 commit
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
Prev Previous commit
Next Next commit
adding inApp excludes and includes as list
  • Loading branch information
Manoel Aranda Neto committed Oct 30, 2019
commit fbfa205fe5e9b5fb1dd5a6c5460d0f197bb08495
10 changes: 10 additions & 0 deletions sentry-core/src/main/java/io/sentry/core/MainEventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ public class MainEventProcessor implements EventProcessor {
private final SentryThreadFactory sentryThreadFactory;
private final SentryExceptionFactory sentryExceptionFactory;

MainEventProcessor(final SentryOptions options) {
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");

SentryStackTraceFactory sentryStackTraceFactory =
new SentryStackTraceFactory(options.getInAppExcludes(), options.getInAppIncludes());

sentryExceptionFactory = new SentryExceptionFactory(sentryStackTraceFactory);
sentryThreadFactory = new SentryThreadFactory(sentryStackTraceFactory);
}

MainEventProcessor(
final SentryOptions options,
final SentryThreadFactory sentryThreadFactory,
Expand Down
27 changes: 17 additions & 10 deletions sentry-core/src/main/java/io/sentry/core/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class SentryOptions {
private String environment;
private Proxy proxy;
private Double sampling;
private String inAppPrefix = "io.sentry";
private List<String> inAppExcludes;
private List<String> inAppIncludes;

public void addEventProcessor(EventProcessor eventProcessor) {
eventProcessors.add(eventProcessor);
Expand Down Expand Up @@ -188,12 +189,20 @@ public void setSampling(Double sampling) {
this.sampling = sampling;
}

public String getInAppPrefix() {
return inAppPrefix;
public List<String> getInAppExcludes() {
return inAppExcludes;
}

public void setInAppPrefix(String inAppPrefix) {
this.inAppPrefix = inAppPrefix;
public void setInAppExcludes(List<String> inAppExcludes) {
this.inAppExcludes = inAppExcludes;
}

public List<String> getInAppIncludes() {
return inAppIncludes;
}

public void setInAppIncludes(List<String> inAppIncludes) {
this.inAppIncludes = inAppIncludes;
}

public interface BeforeSendCallback {
Expand All @@ -205,12 +214,10 @@ public interface BeforeBreadcrumbCallback {
}

public SentryOptions() {
SentryStackTraceFactory sentryStackTraceFactory = new SentryStackTraceFactory(this.inAppPrefix);
SentryExceptionFactory sentryExceptionFactory =
new SentryExceptionFactory(sentryStackTraceFactory);
SentryThreadFactory sentryThreadFactory = new SentryThreadFactory(sentryStackTraceFactory);
inAppExcludes = new ArrayList<>();
inAppExcludes.add("io.sentry");

eventProcessors.add(new MainEventProcessor(this, sentryThreadFactory, sentryExceptionFactory));
eventProcessors.add(new MainEventProcessor(this));
integrations.add(new UncaughtExceptionHandlerIntegration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import io.sentry.core.protocol.SentryStackFrame;
import io.sentry.core.util.Nullable;
import io.sentry.core.util.VisibleForTesting;
import java.util.ArrayList;
import java.util.List;

/** class responsible for converting Java StackTraceElements to SentryStackFrames */
class SentryStackTraceFactory {

private final String inAppPrefix;
@VisibleForTesting boolean inAppEnabled = false;
private final List<String> inAppExcludes;
private final List<String> inAppIncludes;

public SentryStackTraceFactory(@Nullable final String inAppPrefix) {
this.inAppPrefix = inAppPrefix;

if (this.inAppPrefix != null && !this.inAppPrefix.isEmpty()) {
inAppEnabled = true;
}
public SentryStackTraceFactory(
@Nullable final List<String> inAppExcludes, @Nullable List<String> inAppIncludes) {
this.inAppExcludes = inAppExcludes;
this.inAppIncludes = inAppIncludes;
}

/**
Expand All @@ -34,12 +31,7 @@ List<SentryStackFrame> getStackFrames(@Nullable final StackTraceElement[] elemen
SentryStackFrame sentryStackFrame = new SentryStackFrame();

// https://docs.sentry.io/development/sdk-dev/features/#in-app-frames
if (inAppEnabled && item.getClassName().startsWith(inAppPrefix)) {
sentryStackFrame.setInApp(true);
} else {
sentryStackFrame.setInApp(false);
}

sentryStackFrame.setInApp(isInApp(item.getClassName()));
sentryStackFrame.setModule(item.getClassName());
sentryStackFrame.setFunction(item.getMethodName());
sentryStackFrame.setFilename(item.getFileName());
Expand All @@ -52,4 +44,22 @@ List<SentryStackFrame> getStackFrames(@Nullable final StackTraceElement[] elemen

return sentryStackFrames;
}

private boolean isInApp(String className) {
// if (inAppIncludes != null) {
// for (String include : inAppIncludes) {
// if (className.startsWith(include)) {
// return false;
// }
// }
// }
if (inAppExcludes != null) {
for (String exclude : inAppExcludes) {
if (className.startsWith(exclude)) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertTrue

class SentryExceptionFactoryTest {
private val sut = SentryExceptionFactory(SentryStackTraceFactory("io.sentry"))
private val sut = SentryExceptionFactory(SentryStackTraceFactory(listOf("io.sentry"), listOf()))

@Test
fun `when getSentryExceptions is called passing an Exception, not empty result`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.assertFalse
import kotlin.test.assertTrue

class SentryStackTraceFactoryTest {
private val sut = SentryStackTraceFactory("io.sentry")
private val sut = SentryStackTraceFactory(listOf(), listOf())

@Test
fun `when getStackFrames is called passing a valid Array, not empty result`() {
Expand All @@ -22,8 +22,8 @@ class SentryStackTraceFactoryTest {

@Test
fun `when getStackFrames is called passing a valid array, fields should be set`() {
val element = StackTraceElement("class", "method", "fileName", -2)
val stacktraces = Array(1) { element }
val element = generateStackTrace("class")
val stacktraces = arrayOf(element)
val stackFrames = sut.getStackFrames(stacktraces)
assertEquals("class", stackFrames[0].module)
assertEquals("method", stackFrames[0].function)
Expand All @@ -32,36 +32,70 @@ class SentryStackTraceFactoryTest {
assertEquals(true, stackFrames[0].isNative)
}

//region inAppExcludes
@Test
fun `when getStackFrames is called passing a valid inAppPrefix, inAppEnabled should be true`() {
assertTrue(sut.inAppEnabled)
}

@Test
fun `when getStackFrames is called passing an empty inAppPrefix, inAppEnabled should be false`() {
assertFalse(SentryStackTraceFactory("").inAppEnabled)
}
fun `when getStackFrames is called passing a valid inAppExcludes, inApp should be true if prefix matches it`() {
val element = generateStackTrace("io.sentry.MyActivity")
val elements = arrayOf(element)
val sentryStackTraceFactory = SentryStackTraceFactory(listOf("io.sentry"), null)
val sentryElements = sentryStackTraceFactory.getStackFrames(elements)

@Test
fun `when getStackFrames is called passing a null inAppPrefix, inAppEnabled should be false`() {
assertFalse(SentryStackTraceFactory(null).inAppEnabled)
assertTrue(sentryElements.first().inApp)
}

@Test
fun `when getStackFrames is called passing a valid inAppPrefix, inApp should be true if prefix matches it`() {
val element = StackTraceElement("io.sentry.MyActivity", "method", "fileName", -2)
fun `when getStackFrames is called passing a valid inAppExcludes, inApp should be false if prefix doesnt matches it`() {
val element = generateStackTrace("io.myapp.MyActivity")
val elements = arrayOf(element)
val sentryElements = sut.getStackFrames(elements)
val sentryStackTraceFactory = SentryStackTraceFactory(listOf("io.sentry"), null)
val sentryElements = sentryStackTraceFactory.getStackFrames(elements)

assertTrue(sentryElements.first().inApp)
assertFalse(sentryElements.first().inApp)
}

@Test
fun `when getStackFrames is called passing a valid inAppPrefix, inApp should be false if prefix doesnt matches it`() {
val element = StackTraceElement("io.myapp.MyActivity", "method", "fileName", -2)
fun `when getStackFrames is called passing an invalid inAppExcludes, inApp should be false`() {
val element = generateStackTrace("io.myapp.MyActivity")
val elements = arrayOf(element)
val sentryElements = sut.getStackFrames(elements)
val sentryStackTraceFactory = SentryStackTraceFactory(null, null)
val sentryElements = sentryStackTraceFactory.getStackFrames(elements)

assertFalse(sentryElements.first().inApp)
}
//endregion

// //region inAppIncludes
// @Test
// fun `when getStackFrames is called passing a valid inAppIncludes, inApp should be true if prefix doesnt matches it`() {
// val element = generateStackTrace("io.myotherapp.MyActivity")
// val elements = arrayOf(element)
// val sentryStackTraceFactory = SentryStackTraceFactory(null, listOf("io.myapp"))
// val sentryElements = sentryStackTraceFactory.getStackFrames(elements)
//
// assertFalse(sentryElements.first().inApp)
// }
//
// @Test
// fun `when getStackFrames is called passing a valid inAppIncludes, inApp should be false`() {
// val element = generateStackTrace("io.myapp.MyActivity")
// val elements = arrayOf(element)
// val sentryStackTraceFactory = SentryStackTraceFactory(null, listOf("io.myapp"))
// val sentryElements = sentryStackTraceFactory.getStackFrames(elements)
//
// assertFalse(sentryElements.first().inApp)
// }
//
// @Test
// fun `when getStackFrames is called passing an invalid inAppIncludes, inApp should be false`() {
// val element = generateStackTrace("io.myotherapp.MyActivity")
// val elements = arrayOf(element)
// val sentryStackTraceFactory = SentryStackTraceFactory(null, null)
// val sentryElements = sentryStackTraceFactory.getStackFrames(elements)
//
// assertFalse(sentryElements.first().inApp)
// }
// //endregion

private fun generateStackTrace(className: String) =
StackTraceElement(className, "method", "fileName", -2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.assertTrue

class SentryThreadFactoryTest {

private val sut = SentryThreadFactory(SentryStackTraceFactory("io.sentry"))
private val sut = SentryThreadFactory(SentryStackTraceFactory(listOf("io.sentry"), listOf()))

@Test
fun `when getCurrentThreads is called, not empty result`() {
Expand Down