Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Reset contexts when a scope is cleared ([#5895](https://github.com/getsentry/sentry-java/pull/5895))

## 8.52.0

### Fixes
Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public final class io/sentry/CheckInStatus : java/lang/Enum {

public final class io/sentry/CombinedContextsView : io/sentry/protocol/Contexts {
public fun <init> (Lio/sentry/protocol/Contexts;Lio/sentry/protocol/Contexts;Lio/sentry/protocol/Contexts;Lio/sentry/ScopeType;)V
public fun clear ()V
public fun containsKey (Ljava/lang/Object;)Z
public fun entrySet ()Ljava/util/Set;
public fun get (Ljava/lang/Object;)Ljava/lang/Object;
Expand Down Expand Up @@ -5795,6 +5796,7 @@ public class io/sentry/protocol/Contexts : io/sentry/JsonSerializable {
protected final field responseLock Lio/sentry/util/AutoClosableReentrantLock;
public fun <init> ()V
public fun <init> (Lio/sentry/protocol/Contexts;)V
public fun clear ()V
public fun containsKey (Ljava/lang/Object;)Z
public fun entrySet ()Ljava/util/Set;
public fun equals (Ljava/lang/Object;)Z
Expand Down
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/CombinedContextsView.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ public boolean containsKey(final @Nullable Object key) {
return getDefaultContexts().remove(key);
}

@Override
public void clear() {
getDefaultContexts().clear();
}

@Override
public @NotNull Enumeration<String> keys() {
return mergeContexts().keys();
Expand Down
1 change: 1 addition & 0 deletions sentry/src/main/java/io/sentry/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ public void clear() {
tags.clear();
attributes.clear();
extra.clear();
contexts.clear();
eventProcessors.clear();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Also worth mentioning another thing from Scopes spec: "keeping event processors". Currently, they are cleared. I'm not sure if it's an intentional deviation.

clearTransaction();
clearAttachments();
Expand Down
4 changes: 4 additions & 0 deletions sentry/src/main/java/io/sentry/protocol/Contexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ public boolean containsKey(final @Nullable Object key) {
return internalStorage.remove(key);
}

public void clear() {
internalStorage.clear();
}

public @NotNull Enumeration<String> keys() {
return internalStorage.keys();
}
Expand Down
31 changes: 31 additions & 0 deletions sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry

import com.google.common.truth.Truth.assertThat
import io.sentry.protocol.Device
import io.sentry.protocol.Request
import io.sentry.protocol.SentryId
Expand Down Expand Up @@ -575,6 +576,36 @@ class CombinedScopeViewTest {
assertNotNull(fixture.globalScope.level)
}

@Test
fun `clear removes contexts from default scope`() {
val combined = fixture.getSut()

fixture.scope.setContexts("scopeContext", "scopeValue")
fixture.isolationScope.setContexts("isolationContext", "isolationValue")
fixture.globalScope.setContexts("globalContext", "globalValue")

combined.clear()

assertThat(fixture.scope.contexts.containsKey("scopeContext")).isTrue()
assertThat(fixture.isolationScope.contexts.isEmpty).isTrue()
assertThat(fixture.globalScope.contexts.containsKey("globalContext")).isTrue()
}

@Test
fun `contexts view clear removes from default scope`() {
val combined = fixture.getSut()

fixture.scope.setContexts("scopeContext", "scopeValue")
fixture.isolationScope.setContexts("isolationContext", "isolationValue")
fixture.globalScope.setContexts("globalContext", "globalValue")

combined.contexts.clear()

assertThat(fixture.scope.contexts.containsKey("scopeContext")).isTrue()
assertThat(fixture.isolationScope.contexts.isEmpty).isTrue()
assertThat(fixture.globalScope.contexts.containsKey("globalContext")).isTrue()
}

@Test
fun `tags are combined from all scopes`() {
val combined = fixture.getSut()
Expand Down
14 changes: 14 additions & 0 deletions sentry/src/test/java/io/sentry/ScopeTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry

import com.google.common.truth.Truth.assertThat
import io.sentry.SentryLevel.WARNING
import io.sentry.protocol.Request
import io.sentry.protocol.SentryId
Expand Down Expand Up @@ -292,6 +293,7 @@ class ScopeTest {
scope.screen = "MainActivity"
scope.setExtra("some", "extra")
scope.setAttribute("some", "attribute")
scope.setContexts("some", "context")
scope.addEventProcessor(eventProcessor())
scope.addAttachment(Attachment("path"))
scope.addFeatureFlag("flag", true)
Expand All @@ -308,6 +310,7 @@ class ScopeTest {
assertEquals(0, scope.tags.size)
assertEquals(0, scope.attributes.size)
assertEquals(0, scope.extras.size)
assertEquals(0, scope.contexts.size)
assertEquals(0, scope.eventProcessors.size)
assertEquals(0, scope.attachments.size)
assertEquals(0, scope.featureFlags!!.values.size)
Expand Down Expand Up @@ -1290,6 +1293,17 @@ class ScopeTest {
assertEquals(0, scope.attributes.size)
}

@Test
fun `clear removes contexts`() {
val scope = Scope(SentryOptions())
scope.setContexts("key1", "value1")
assertThat(scope.contexts.size).isEqualTo(1)

scope.clear()

assertThat(scope.contexts.isEmpty).isTrue()
}

private fun eventProcessor(): EventProcessor =
object : EventProcessor {
override fun process(event: SentryEvent, hint: Hint): SentryEvent? = event
Expand Down
16 changes: 16 additions & 0 deletions sentry/src/test/java/io/sentry/protocol/ContextsTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.protocol

import com.google.common.truth.Truth.assertThat
import io.sentry.ProfileContext
import io.sentry.SpanContext
import kotlin.test.Test
Expand Down Expand Up @@ -137,4 +138,19 @@ class ContextsTest {

assertEquals(listOf("a"), contexts.keys().toList())
}

@Test
fun `clear removes all entries`() {
val contexts = Contexts()
contexts["some-property"] = "some-value"
contexts.setApp(App())
contexts.setTrace(SpanContext("op"))

contexts.clear()

assertThat(contexts.isEmpty).isTrue()
assertThat(contexts["some-property"]).isNull()
assertThat(contexts.app).isNull()
assertThat(contexts.trace).isNull()
}
}
Loading