Skip to content

Commit 0f37c1d

Browse files
authored
Optimize when maxBreadcrumb is 0 (#4504)
* breadcumbs queue is now a simple DisabledQueue when maxBreadcrumb is 0 * if breadcumbs is a DisabledQueue, no more processing is done (beforeBreadcrumb or scopeObservers)
1 parent 7f91916 commit 0f37c1d

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- Optimize scope when maxBreadcrumb is 0 ([#4504](https://github.com/getsentry/sentry-java/pull/4504))
78
- Fix javadoc on TransportResult ([#4528](https://github.com/getsentry/sentry-java/pull/4528))
89

910
### Internal

sentry/src/main/java/io/sentry/Scope.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ public Queue<Breadcrumb> getBreadcrumbs() {
466466
*/
467467
@Override
468468
public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) {
469-
if (breadcrumb == null) {
469+
if (breadcrumb == null || breadcrumbs instanceof DisabledQueue) {
470470
return;
471471
}
472472
if (hint == null) {
@@ -862,7 +862,7 @@ public void clearAttachments() {
862862
static @NotNull Queue<Breadcrumb> createBreadcrumbsList(final int maxBreadcrumb) {
863863
return maxBreadcrumb > 0
864864
? SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb))
865-
: SynchronizedQueue.synchronizedQueue(new DisabledQueue<>());
865+
: new DisabledQueue<>();
866866
}
867867

868868
/**

sentry/src/test/java/io/sentry/ScopeTest.kt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import kotlin.test.assertNotSame
1414
import kotlin.test.assertNull
1515
import kotlin.test.assertTrue
1616
import org.junit.Assert.assertArrayEquals
17+
import org.mockito.kotlin.any
1718
import org.mockito.kotlin.argThat
1819
import org.mockito.kotlin.check
1920
import org.mockito.kotlin.eq
2021
import org.mockito.kotlin.mock
2122
import org.mockito.kotlin.never
2223
import org.mockito.kotlin.times
2324
import org.mockito.kotlin.verify
25+
import org.mockito.kotlin.verifyNoInteractions
2426
import org.mockito.kotlin.whenever
2527

2628
class ScopeTest {
@@ -331,6 +333,69 @@ class ScopeTest {
331333
assertEquals(1, scope.breadcrumbs.count())
332334
}
333335

336+
@Test
337+
fun `when adding breadcrumb and maxBreadcrumb is 0, beforeBreadcrumb is not executed`() {
338+
var called = false
339+
val options =
340+
SentryOptions().apply {
341+
maxBreadcrumbs = 0
342+
beforeBreadcrumb =
343+
SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ ->
344+
called = true
345+
breadcrumb
346+
}
347+
}
348+
349+
val scope = Scope(options)
350+
scope.addBreadcrumb(Breadcrumb())
351+
assertEquals(0, scope.breadcrumbs.count())
352+
assertFalse(called)
353+
}
354+
355+
@Test
356+
fun `when adding breadcrumb and maxBreadcrumb is not 0, beforeBreadcrumb is executed`() {
357+
var called = false
358+
val options =
359+
SentryOptions().apply {
360+
beforeBreadcrumb =
361+
SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ ->
362+
called = true
363+
breadcrumb
364+
}
365+
}
366+
367+
val scope = Scope(options)
368+
scope.addBreadcrumb(Breadcrumb())
369+
assertEquals(1, scope.breadcrumbs.count())
370+
assertTrue(called)
371+
}
372+
373+
@Test
374+
fun `when adding breadcrumb and maxBreadcrumb is 0, scopesObservers are not called`() {
375+
val observer = mock<IScopeObserver>()
376+
val options =
377+
SentryOptions().apply {
378+
maxBreadcrumbs = 0
379+
addScopeObserver(observer)
380+
}
381+
382+
val scope = Scope(options)
383+
scope.addBreadcrumb(Breadcrumb())
384+
assertEquals(0, scope.breadcrumbs.count())
385+
verifyNoInteractions(observer)
386+
}
387+
388+
@Test
389+
fun `when adding breadcrumb and maxBreadcrumb is not 0, scopesObservers are called`() {
390+
val observer = mock<IScopeObserver>()
391+
val options = SentryOptions().apply { addScopeObserver(observer) }
392+
393+
val scope = Scope(options)
394+
scope.addBreadcrumb(Breadcrumb())
395+
assertEquals(1, scope.breadcrumbs.count())
396+
verify(observer).addBreadcrumb(any())
397+
}
398+
334399
@Test
335400
fun `when adding eventProcessor, eventProcessor should be in the list`() {
336401
val processor = CustomEventProcessor()

0 commit comments

Comments
 (0)