Skip to content

Optimize when maxBreadcrumb is 0 #4504

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

Merged
merged 8 commits into from
Jun 30, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

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

### Internal
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public Queue<Breadcrumb> getBreadcrumbs() {
*/
@Override
public void addBreadcrumb(@NotNull Breadcrumb breadcrumb, @Nullable Hint hint) {
if (breadcrumb == null) {
if (breadcrumb == null || breadcrumbs instanceof DisabledQueue) {
return;
}
if (hint == null) {
Expand Down Expand Up @@ -862,7 +862,7 @@ public void clearAttachments() {
static @NotNull Queue<Breadcrumb> createBreadcrumbsList(final int maxBreadcrumb) {
return maxBreadcrumb > 0
? SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb))
: SynchronizedQueue.synchronizedQueue(new DisabledQueue<>());
: new DisabledQueue<>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried calling queue.add from multiple threads concurrently, and it didn't crash, so i think we it's fine to remove SynchronizedQueue

}

/**
Expand Down
65 changes: 65 additions & 0 deletions sentry/src/test/java/io/sentry/ScopeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import kotlin.test.assertNotSame
import kotlin.test.assertNull
import kotlin.test.assertTrue
import org.junit.Assert.assertArrayEquals
import org.mockito.kotlin.any
import org.mockito.kotlin.argThat
import org.mockito.kotlin.check
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever

class ScopeTest {
Expand Down Expand Up @@ -331,6 +333,69 @@ class ScopeTest {
assertEquals(1, scope.breadcrumbs.count())
}

@Test
fun `when adding breadcrumb and maxBreadcrumb is 0, beforeBreadcrumb is not executed`() {
var called = false
val options =
SentryOptions().apply {
maxBreadcrumbs = 0
beforeBreadcrumb =
SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ ->
called = true
breadcrumb
}
}

val scope = Scope(options)
scope.addBreadcrumb(Breadcrumb())
assertEquals(0, scope.breadcrumbs.count())
assertFalse(called)
}

@Test
fun `when adding breadcrumb and maxBreadcrumb is not 0, beforeBreadcrumb is executed`() {
var called = false
val options =
SentryOptions().apply {
beforeBreadcrumb =
SentryOptions.BeforeBreadcrumbCallback { breadcrumb, _ ->
called = true
breadcrumb
}
}

val scope = Scope(options)
scope.addBreadcrumb(Breadcrumb())
assertEquals(1, scope.breadcrumbs.count())
assertTrue(called)
}

@Test
fun `when adding breadcrumb and maxBreadcrumb is 0, scopesObservers are not called`() {
val observer = mock<IScopeObserver>()
val options =
SentryOptions().apply {
maxBreadcrumbs = 0
addScopeObserver(observer)
}

val scope = Scope(options)
scope.addBreadcrumb(Breadcrumb())
assertEquals(0, scope.breadcrumbs.count())
verifyNoInteractions(observer)
}

@Test
fun `when adding breadcrumb and maxBreadcrumb is not 0, scopesObservers are called`() {
val observer = mock<IScopeObserver>()
val options = SentryOptions().apply { addScopeObserver(observer) }

val scope = Scope(options)
scope.addBreadcrumb(Breadcrumb())
assertEquals(1, scope.breadcrumbs.count())
verify(observer).addBreadcrumb(any())
}

@Test
fun `when adding eventProcessor, eventProcessor should be in the list`() {
val processor = CustomEventProcessor()
Expand Down
Loading