Skip to content

Commit

Permalink
Use configureScope instead of withScope for InternalSentrySdk (#2863)
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi committed Jul 25, 2023
1 parent 2718fc8 commit f60cce2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public final class InternalSentrySdk {
@Nullable
public static Scope getCurrentScope() {
final @NotNull AtomicReference<Scope> scopeRef = new AtomicReference<>();
//noinspection Convert2MethodRef
HubAdapter.getInstance().withScope(scope -> scopeRef.set(scope));
HubAdapter.getInstance()
.configureScope(
scope -> {
scopeRef.set(new Scope(scope));
});
return scopeRef.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ class InternalSentrySdkTest {
assertNotNull(scope)
}

@Test
fun `current scope returns a copy of the scope`() {
Sentry.setCurrentHub(
Hub(
SentryOptions().apply {
dsn = "https://key@uri/1234567"
}
)
)
Sentry.addBreadcrumb("test")

// when the clone is modified
val clonedScope = InternalSentrySdk.getCurrentScope()!!
clonedScope.clearBreadcrumbs()

// then modifications should not be reflected
Sentry.configureScope { scope ->
assertEquals(1, scope.breadcrumbs.size)
}
}

@Test
fun `serializeScope correctly creates top level map`() {
val options = SentryAndroidOptions()
Expand Down
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ public final class io/sentry/SamplingContext {
}

public final class io/sentry/Scope {
public fun <init> (Lio/sentry/Scope;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun addAttachment (Lio/sentry/Attachment;)V
public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V
Expand Down
6 changes: 5 additions & 1 deletion sentry/src/main/java/io/sentry/IHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) {
void popScope();

/**
* Runs the callback with a new scope which gets dropped at the end
* Runs the callback with a new scope which gets dropped at the end. If you're using the Sentry
* SDK in globalHubMode (defaults to true on Android) {@link
* Sentry#init(Sentry.OptionsConfiguration, boolean)} calling withScope is discouraged, as scope
* changes may be dropped when executed in parallel. Use {@link
* IHub#configureScope(ScopeCallback)} instead.
*
* @param callback the callback
*/
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/main/java/io/sentry/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public Scope(final @NotNull SentryOptions options) {
this.propagationContext = new PropagationContext();
}

Scope(final @NotNull Scope scope) {
@ApiStatus.Internal
public Scope(final @NotNull Scope scope) {
this.transaction = scope.transaction;
this.transactionName = scope.transactionName;
this.session = scope.session;
Expand Down

0 comments on commit f60cce2

Please sign in to comment.