Skip to content

Commit

Permalink
Hubs/Scopes Merge 21 - Allow controlling which scope configureScope
Browse files Browse the repository at this point in the history
… uses (#3345)

* replace hub with scopes

* Add Scopes

* Introduce `IScopes` interface.

* Replace `IHub` with `IScopes` in core

* Replace `IHub` with `IScopes` in android core

* Replace `IHub` with `IScopes` in android integrations

* Replace `IHub` with `IScopes` in apollo integrations

* Replace `IHub` with `IScopes` in okhttp integration

* Replace `IHub` with `IScopes` in graphql integration

* Replace `IHub` with `IScopes` in logging integrations

* Replace `IHub` with `IScopes` in more integrations

* Replace `IHub` with `IScopes` in OTel integration

* Replace `IHub` with `IScopes` in Spring 5 / Spring Boot 2 integrations

* Replace `IHub` with `IScopes` in Spring 6 / Spring Boot 3 integrations

* Replace `IHub` with `IScopes` in samples

* gitscopes -> github

* Replace ThreadLocal with ScopesStorage

* Move client and throwable to span map to scope

* Add global scope

* use global scope in Scopes

* Implement pushScope popScope and withScope for Scopes

* Add pushIsolationScope; add fork methods to ISCope

* Use separate scopes for current, isolation and global scope; rename mainScopes to rootScopes

* Allow controlling which scope configureScope uses
  • Loading branch information
adinauer committed Apr 22, 2024
1 parent 385666d commit a941eb8
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.sentry.ILogger;
import io.sentry.ITransactionProfiler;
import io.sentry.NoOpConnectionStatusProvider;
import io.sentry.ScopeType;
import io.sentry.SendFireAndForgetEnvelopeSender;
import io.sentry.SendFireAndForgetOutboxSender;
import io.sentry.SentryLevel;
Expand Down Expand Up @@ -98,6 +99,8 @@ static void loadDefaultAndMetadataOptions(
// Firstly set the logger, if `debug=true` configured, logging can start asap.
options.setLogger(logger);

options.setDefaultScopeType(ScopeType.CURRENT);

options.setDateProvider(new SentryAndroidDateProvider());

// set a lower flush timeout on Android to avoid ANRs
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/main/java/io/sentry/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ public void withScope(final @NotNull ScopeCallback callback) {
}

@Override
public void configureScope(final @NotNull ScopeCallback callback) {
public void configureScope(
final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) {
if (!isEnabled()) {
options
.getLogger()
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/HubAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public void withScope(@NotNull ScopeCallback callback) {
}

@Override
public void configureScope(@NotNull ScopeCallback callback) {
Sentry.configureScope(callback);
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {
Sentry.configureScope(scopeType, callback);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/HubScopesWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public void withScope(@NotNull ScopeCallback callback) {
}

@Override
public void configureScope(@NotNull ScopeCallback callback) {
scopes.configureScope(callback);
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {
scopes.configureScope(scopeType, callback);
}

@Override
Expand Down
11 changes: 10 additions & 1 deletion sentry/src/main/java/io/sentry/IScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,16 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) {
*
* @param callback The configure scope callback.
*/
void configureScope(@NotNull ScopeCallback callback);
default void configureScope(@NotNull ScopeCallback callback) {
configureScope(null, callback);
}

/**
* Configures the scope through the callback.
*
* @param callback The configure scope callback.
*/
void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback);

/**
* Binds a different client to the hub
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/NoOpHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void withScope(@NotNull ScopeCallback callback) {
}

@Override
public void configureScope(@NotNull ScopeCallback callback) {}
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {}

@Override
public void bindClient(@NotNull ISentryClient client) {}
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/NoOpScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void withScope(@NotNull ScopeCallback callback) {
}

@Override
public void configureScope(@NotNull ScopeCallback callback) {}
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {}

@Override
public void bindClient(@NotNull ISentryClient client) {}
Expand Down
7 changes: 7 additions & 0 deletions sentry/src/main/java/io/sentry/ScopeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.sentry;

public enum ScopeType {
CURRENT,
ISOLATION,
GLOBAL;
}
38 changes: 27 additions & 11 deletions sentry/src/main/java/io/sentry/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,31 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable
}
}

private IScope getDefaultConfigureScope() {
// TODO configurable default scope via SentryOptions, Android = global or isolation, backend =
// isolation
return scope;
}
private IScope getSpecificScope(final @Nullable ScopeType scopeType) {
if (scopeType != null) {
switch (scopeType) {
case CURRENT:
return scope;
case ISOLATION:
return isolationScope;
case GLOBAL:
return getGlobalScope();
default:
break;
}
}

private IScope getDefaultWriteScope() {
// TODO configurable default scope via SentryOptions, Android = global or isolation, backend =
// isolation
return getIsolationScope();
switch (getOptions().getDefaultScopeType()) {
case CURRENT:
return scope;
case ISOLATION:
return isolationScope;
case GLOBAL:
return getGlobalScope();
default:
// calm the compiler
return scope;
}
}

@Override
Expand Down Expand Up @@ -657,7 +672,8 @@ public void withScope(final @NotNull ScopeCallback callback) {
}

@Override
public void configureScope(final @NotNull ScopeCallback callback) {
public void configureScope(
final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) {
if (!isEnabled()) {
options
.getLogger()
Expand All @@ -666,7 +682,7 @@ public void configureScope(final @NotNull ScopeCallback callback) {
"Instance is disabled and this 'configureScope' call is a no-op.");
} else {
try {
callback.run(getDefaultConfigureScope());
callback.run(getSpecificScope(scopeType));
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e);
}
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/ScopesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ public void withScope(@NotNull ScopeCallback callback) {
}

@Override
public void configureScope(@NotNull ScopeCallback callback) {
Sentry.configureScope(callback);
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {
Sentry.configureScope(scopeType, callback);
}

@Override
Expand Down
12 changes: 11 additions & 1 deletion sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,17 @@ public static void withScope(final @NotNull ScopeCallback callback) {
* @param callback The configure scope callback.
*/
public static void configureScope(final @NotNull ScopeCallback callback) {
getCurrentScopes().configureScope(callback);
configureScope(null, callback);
}

/**
* Configures the scope through the callback.
*
* @param callback The configure scope callback.
*/
public static void configureScope(
final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) {
getCurrentScopes().configureScope(scopeType, callback);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ public class SentryOptions {

@ApiStatus.Experimental private @Nullable Cron cron = null;

private @NotNull ScopeType defaultScopeType = ScopeType.ISOLATION;

/**
* Adds an event processor
*
Expand Down Expand Up @@ -2385,6 +2387,14 @@ public void setCron(@Nullable Cron cron) {
this.cron = cron;
}

public void setDefaultScopeType(final @NotNull ScopeType scopeType) {
this.defaultScopeType = scopeType;
}

public @NotNull ScopeType getDefaultScopeType() {
return defaultScopeType;
}

/** The BeforeSend callback */
public interface BeforeSendCallback {

Expand Down
3 changes: 2 additions & 1 deletion sentry/src/test/java/io/sentry/HubAdapterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.sentry
import io.sentry.protocol.SentryTransaction
import io.sentry.protocol.User
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.reset
Expand Down Expand Up @@ -185,7 +186,7 @@ class HubAdapterTest {
@Test fun `configureScope calls Hub`() {
val scopeCallback = mock<ScopeCallback>()
HubAdapter.getInstance().configureScope(scopeCallback)
verify(scopes).configureScope(eq(scopeCallback))
verify(scopes).configureScope(anyOrNull(), eq(scopeCallback))
}

@Test fun `bindClient calls Hub`() {
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/test/java/io/sentry/ScopesAdapterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.sentry
import io.sentry.protocol.SentryTransaction
import io.sentry.protocol.User
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.reset
Expand Down Expand Up @@ -185,7 +186,7 @@ class ScopesAdapterTest {
@Test fun `configureScope calls Hub`() {
val scopeCallback = mock<ScopeCallback>()
ScopesAdapter.getInstance().configureScope(scopeCallback)
verify(scopes).configureScope(eq(scopeCallback))
verify(scopes).configureScope(anyOrNull(), eq(scopeCallback))
}

@Test fun `bindClient calls Hub`() {
Expand Down

0 comments on commit a941eb8

Please sign in to comment.