Skip to content

Commit d0d0d0a

Browse files
committed
fix(core): Exclude reusable errors from deduplication
Allow exception class names and patterns to bypass throwable-based event deduplication. Exclude Netty ReadTimeoutException by default so its static singleton does not suppress independent timeout occurrences for the SDK lifetime. Fixes #6365
1 parent 7803c61 commit d0d0d0a

5 files changed

Lines changed: 153 additions & 7 deletions

File tree

‎sentry/api/sentry.api‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,7 @@ public class io/sentry/SentryOptions : io/sentry/transport/RateLimiterConfig {
34803480
public fun addEventProcessor (Lio/sentry/EventProcessor;)V
34813481
public fun addIgnoredCheckIn (Ljava/lang/String;)V
34823482
public fun addIgnoredError (Ljava/lang/String;)V
3483+
public fun addIgnoredErrorForDeduplication (Ljava/lang/String;)V
34833484
public fun addIgnoredExceptionForType (Ljava/lang/Class;)V
34843485
public fun addIgnoredSpanOrigin (Ljava/lang/String;)V
34853486
public fun addIgnoredTransaction (Ljava/lang/String;)V
@@ -3537,6 +3538,7 @@ public class io/sentry/SentryOptions : io/sentry/transport/RateLimiterConfig {
35373538
public fun getIdleTimeout ()Ljava/lang/Long;
35383539
public fun getIgnoredCheckIns ()Ljava/util/List;
35393540
public fun getIgnoredErrors ()Ljava/util/List;
3541+
public fun getIgnoredErrorsForDeduplication ()Ljava/util/List;
35403542
public fun getIgnoredExceptionsForType ()Ljava/util/Set;
35413543
public fun getIgnoredSpanOrigins ()Ljava/util/List;
35423544
public fun getIgnoredTransactions ()Ljava/util/List;
@@ -3708,6 +3710,7 @@ public class io/sentry/SentryOptions : io/sentry/transport/RateLimiterConfig {
37083710
public fun setIdleTimeout (Ljava/lang/Long;)V
37093711
public fun setIgnoredCheckIns (Ljava/util/List;)V
37103712
public fun setIgnoredErrors (Ljava/util/List;)V
3713+
public fun setIgnoredErrorsForDeduplication (Ljava/util/List;)V
37113714
public fun setIgnoredSpanOrigins (Ljava/util/List;)V
37123715
public fun setIgnoredTransactions (Ljava/util/List;)V
37133716
public fun setInitPriority (Lio/sentry/InitPriority;)V

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ public DuplicateEventDetectionEventProcessor(final @NotNull SentryOptions option
2626
if (options.isEnableDeduplication()) {
2727
final Throwable throwable = event.getThrowable();
2828
if (throwable != null) {
29-
if (capturedObjects.containsKey(throwable)
30-
|| containsAnyKey(capturedObjects, allCauses(throwable))) {
29+
final boolean ignoredForDeduplication = isIgnoredForDeduplication(throwable);
30+
if ((!ignoredForDeduplication && capturedObjects.containsKey(throwable))
31+
|| containsAnyKey(allCauses(throwable))) {
3132
options
3233
.getLogger()
3334
.log(
3435
SentryLevel.DEBUG,
3536
"Duplicate Exception detected. Event %s will be discarded.",
3637
event.getEventId());
3738
return null;
38-
} else {
39+
} else if (!ignoredForDeduplication) {
3940
capturedObjects.put(throwable, null);
4041
}
4142
}
@@ -45,10 +46,25 @@ public DuplicateEventDetectionEventProcessor(final @NotNull SentryOptions option
4546
return event;
4647
}
4748

48-
private static <T> boolean containsAnyKey(
49-
final @NotNull Map<T, Object> map, final @NotNull List<T> list) {
50-
for (T entry : list) {
51-
if (map.containsKey(entry)) {
49+
private boolean containsAnyKey(final @NotNull List<Throwable> throwables) {
50+
for (final Throwable throwable : throwables) {
51+
if (!isIgnoredForDeduplication(throwable) && capturedObjects.containsKey(throwable)) {
52+
return true;
53+
}
54+
}
55+
return false;
56+
}
57+
58+
private boolean isIgnoredForDeduplication(final @NotNull Throwable throwable) {
59+
final @Nullable List<FilterString> ignoredErrors = options.getIgnoredErrorsForDeduplication();
60+
if (ignoredErrors == null || ignoredErrors.isEmpty()) {
61+
return false;
62+
}
63+
64+
final String exceptionClassName = throwable.getClass().getName();
65+
for (final FilterString ignoredError : ignoredErrors) {
66+
if (ignoredError.getFilterString().equals(exceptionClassName)
67+
|| ignoredError.matches(exceptionClassName)) {
5268
return true;
5369
}
5470
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public class SentryOptions implements RateLimiterConfig {
7878
*/
7979
private static final String DEFAULT_ENVIRONMENT = "production";
8080

81+
private static final String DEFAULT_IGNORED_ERROR_FOR_DEDUPLICATION =
82+
"io.netty.handler.timeout.ReadTimeoutException";
83+
8184
/**
8285
* Are callbacks that run for every event. They can either return a new event which in most cases
8386
* means just adding data OR return null in case the event will be dropped and not sent.
@@ -387,6 +390,11 @@ public class SentryOptions implements RateLimiterConfig {
387390
*/
388391
private boolean enableDeduplication = true;
389392

393+
/** Exception types that are excluded from event deduplication. */
394+
private @Nullable List<FilterString> ignoredErrorsForDeduplication =
395+
new ArrayList<>(
396+
Collections.singletonList(new FilterString(DEFAULT_IGNORED_ERROR_FOR_DEDUPLICATION)));
397+
390398
/**
391399
* Enables event size limiting with {@link EventSizeLimitingEventProcessor}. When enabled, events
392400
* exceeding 1MB will have breadcrumbs and stack frames reduced to stay under the limit.
@@ -1947,6 +1955,49 @@ public void setEnableDeduplication(final boolean enableDeduplication) {
19471955
this.enableDeduplication = enableDeduplication;
19481956
}
19491957

1958+
/**
1959+
* Returns the list of exception class names excluded from event deduplication.
1960+
*
1961+
* @return the list of exception class names excluded from event deduplication
1962+
*/
1963+
public @Nullable List<FilterString> getIgnoredErrorsForDeduplication() {
1964+
return ignoredErrorsForDeduplication;
1965+
}
1966+
1967+
/**
1968+
* Sets the list of exception class names excluded from event deduplication. Entries may be exact
1969+
* class names or regular expressions.
1970+
*
1971+
* @param ignoredErrorsForDeduplication exception class names or regular expressions
1972+
*/
1973+
public void setIgnoredErrorsForDeduplication(
1974+
final @Nullable List<String> ignoredErrorsForDeduplication) {
1975+
if (ignoredErrorsForDeduplication == null) {
1976+
this.ignoredErrorsForDeduplication = null;
1977+
} else {
1978+
final @NotNull List<FilterString> patterns = new ArrayList<>();
1979+
for (final String pattern : ignoredErrorsForDeduplication) {
1980+
if (pattern != null && !pattern.isEmpty()) {
1981+
patterns.add(new FilterString(pattern));
1982+
}
1983+
}
1984+
this.ignoredErrorsForDeduplication = patterns;
1985+
}
1986+
}
1987+
1988+
/**
1989+
* Adds an exception class name or regular expression to the list excluded from event
1990+
* deduplication.
1991+
*
1992+
* @param pattern the exception class name or regular expression
1993+
*/
1994+
public void addIgnoredErrorForDeduplication(final @NotNull String pattern) {
1995+
if (ignoredErrorsForDeduplication == null) {
1996+
ignoredErrorsForDeduplication = new ArrayList<>();
1997+
}
1998+
ignoredErrorsForDeduplication.add(new FilterString(pattern));
1999+
}
2000+
19502001
/**
19512002
* Returns if event size limiting is enabled.
19522003
*

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry
22

3+
import com.google.common.truth.Truth.assertThat
34
import io.sentry.exception.ExceptionMechanismException
45
import io.sentry.protocol.Mechanism
56
import java.lang.RuntimeException
@@ -8,6 +9,8 @@ import kotlin.test.assertNotNull
89
import kotlin.test.assertNull
910

1011
class DuplicateEventDetectionEventProcessorTest {
12+
private class IgnoredForDeduplicationException(cause: Throwable? = null) : RuntimeException(cause)
13+
1114
private class CircularCauseThrowable : RuntimeException() {
1215
private var nextCause: Throwable? = null
1316
private var causeReads = 0
@@ -57,6 +60,58 @@ class DuplicateEventDetectionEventProcessorTest {
5760
assertNull(result)
5861
}
5962

63+
@Test
64+
fun `does not add ignored exception to captured exceptions`() {
65+
val options =
66+
SentryOptions().apply {
67+
setIgnoredErrorsForDeduplication(listOf(IgnoredForDeduplicationException::class.java.name))
68+
}
69+
val processor = DuplicateEventDetectionEventProcessor(options)
70+
val event = SentryEvent(IgnoredForDeduplicationException())
71+
72+
assertThat(processor.process(event, Hint())).isNotNull()
73+
options.setIgnoredErrorsForDeduplication(emptyList())
74+
assertThat(processor.process(event, Hint())).isNotNull()
75+
assertThat(processor.process(event, Hint())).isNull()
76+
}
77+
78+
@Test
79+
fun `does not check ignored exception against captured exceptions`() {
80+
val options = SentryOptions().apply { setIgnoredErrorsForDeduplication(emptyList()) }
81+
val processor = DuplicateEventDetectionEventProcessor(options)
82+
val event = SentryEvent(IgnoredForDeduplicationException())
83+
assertThat(processor.process(event, Hint())).isNotNull()
84+
85+
options.addIgnoredErrorForDeduplication(IgnoredForDeduplicationException::class.java.name)
86+
87+
assertThat(processor.process(event, Hint())).isNotNull()
88+
}
89+
90+
@Test
91+
fun `does not check ignored cause against captured exceptions`() {
92+
val options = SentryOptions().apply { setIgnoredErrorsForDeduplication(emptyList()) }
93+
val processor = DuplicateEventDetectionEventProcessor(options)
94+
val cause = IgnoredForDeduplicationException()
95+
assertThat(processor.process(SentryEvent(cause), Hint())).isNotNull()
96+
97+
options.addIgnoredErrorForDeduplication(IgnoredForDeduplicationException::class.java.name)
98+
99+
assertThat(processor.process(SentryEvent(RuntimeException(cause)), Hint())).isNotNull()
100+
}
101+
102+
@Test
103+
fun `ignored exception patterns are matched against the fully qualified class name`() {
104+
val options =
105+
SentryOptions().apply {
106+
setIgnoredErrorsForDeduplication(listOf(".*IgnoredForDeduplicationException"))
107+
}
108+
val processor = DuplicateEventDetectionEventProcessor(options)
109+
val event = SentryEvent(IgnoredForDeduplicationException())
110+
111+
assertThat(processor.process(event, Hint())).isNotNull()
112+
assertThat(processor.process(event, Hint())).isNotNull()
113+
}
114+
60115
@Test
61116
fun `drops event with mechanism exception having an exception that has already been processed`() {
62117
val processor = fixture.getSut()

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,27 @@ class SentryOptionsTest {
10491049
assertEquals(SentryOptions().initPriority, InitPriority.MEDIUM)
10501050
}
10511051

1052+
@Test
1053+
fun `ignored errors for deduplication contains Netty read timeout by default`() {
1054+
assertThat(SentryOptions().ignoredErrorsForDeduplication)
1055+
.containsExactly(FilterString("io.netty.handler.timeout.ReadTimeoutException"))
1056+
}
1057+
1058+
@Test
1059+
fun `ignored errors for deduplication can be replaced and extended`() {
1060+
val options = SentryOptions()
1061+
1062+
options.setIgnoredErrorsForDeduplication(listOf("example.FirstException", ""))
1063+
options.addIgnoredErrorForDeduplication("example.SecondException")
1064+
1065+
assertThat(options.ignoredErrorsForDeduplication)
1066+
.containsExactly(
1067+
FilterString("example.FirstException"),
1068+
FilterString("example.SecondException"),
1069+
)
1070+
.inOrder()
1071+
}
1072+
10521073
@Test
10531074
fun `merging options when ignoredErrors is not set preserves the previous value`() {
10541075
val externalOptions = ExternalOptions()

0 commit comments

Comments
 (0)