Skip to content

Commit f60cce2

Browse files
authored
Use configureScope instead of withScope for InternalSentrySdk (#2863)
1 parent 2718fc8 commit f60cce2

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ public final class InternalSentrySdk {
4141
@Nullable
4242
public static Scope getCurrentScope() {
4343
final @NotNull AtomicReference<Scope> scopeRef = new AtomicReference<>();
44-
//noinspection Convert2MethodRef
45-
HubAdapter.getInstance().withScope(scope -> scopeRef.set(scope));
44+
HubAdapter.getInstance()
45+
.configureScope(
46+
scope -> {
47+
scopeRef.set(new Scope(scope));
48+
});
4649
return scopeRef.get();
4750
}
4851

sentry-android-core/src/test/java/io/sentry/android/core/InternalSentrySdkTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ class InternalSentrySdkTest {
119119
assertNotNull(scope)
120120
}
121121

122+
@Test
123+
fun `current scope returns a copy of the scope`() {
124+
Sentry.setCurrentHub(
125+
Hub(
126+
SentryOptions().apply {
127+
dsn = "https://key@uri/1234567"
128+
}
129+
)
130+
)
131+
Sentry.addBreadcrumb("test")
132+
133+
// when the clone is modified
134+
val clonedScope = InternalSentrySdk.getCurrentScope()!!
135+
clonedScope.clearBreadcrumbs()
136+
137+
// then modifications should not be reflected
138+
Sentry.configureScope { scope ->
139+
assertEquals(1, scope.breadcrumbs.size)
140+
}
141+
}
142+
122143
@Test
123144
fun `serializeScope correctly creates top level map`() {
124145
val options = SentryAndroidOptions()

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ public final class io/sentry/SamplingContext {
12051205
}
12061206

12071207
public final class io/sentry/Scope {
1208+
public fun <init> (Lio/sentry/Scope;)V
12081209
public fun <init> (Lio/sentry/SentryOptions;)V
12091210
public fun addAttachment (Lio/sentry/Attachment;)V
12101211
public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V

sentry/src/main/java/io/sentry/IHub.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,11 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) {
306306
void popScope();
307307

308308
/**
309-
* Runs the callback with a new scope which gets dropped at the end
309+
* Runs the callback with a new scope which gets dropped at the end. If you're using the Sentry
310+
* SDK in globalHubMode (defaults to true on Android) {@link
311+
* Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope
312+
* changes may be dropped when executed in parallel. Use {@link
313+
* IHub#configureScope(ScopeCallback)} instead.
310314
*
311315
* @param callback the callback
312316
*/

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public Scope(final @NotNull SentryOptions options) {
8787
this.propagationContext = new PropagationContext();
8888
}
8989

90-
Scope(final @NotNull Scope scope) {
90+
@ApiStatus.Internal
91+
public Scope(final @NotNull Scope scope) {
9192
this.transaction = scope.transaction;
9293
this.transactionName = scope.transactionName;
9394
this.session = scope.session;

0 commit comments

Comments
 (0)