Skip to content
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

Hubs/Scopes Merge 21 - Allow controlling which scope configureScope uses #3345

Merged
merged 26 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
305baf5
replace hub with scopes
adinauer Mar 27, 2024
95f5e1b
Add Scopes
adinauer Apr 2, 2024
27f2398
Introduce `IScopes` interface.
adinauer Apr 2, 2024
ce3c14f
Replace `IHub` with `IScopes` in core
adinauer Apr 2, 2024
ce615f4
Replace `IHub` with `IScopes` in android core
adinauer Apr 2, 2024
22ddc00
Replace `IHub` with `IScopes` in android integrations
adinauer Apr 2, 2024
305c217
Replace `IHub` with `IScopes` in apollo integrations
adinauer Apr 2, 2024
da927bc
Replace `IHub` with `IScopes` in okhttp integration
adinauer Apr 2, 2024
8279276
Replace `IHub` with `IScopes` in graphql integration
adinauer Apr 2, 2024
9bfc086
Replace `IHub` with `IScopes` in logging integrations
adinauer Apr 2, 2024
b998e50
Replace `IHub` with `IScopes` in more integrations
adinauer Apr 2, 2024
739827a
Replace `IHub` with `IScopes` in OTel integration
adinauer Apr 2, 2024
69f2d63
Replace `IHub` with `IScopes` in Spring 5 / Spring Boot 2 integrations
adinauer Apr 2, 2024
792d482
Replace `IHub` with `IScopes` in Spring 6 / Spring Boot 3 integrations
adinauer Apr 2, 2024
9bcbce6
Replace `IHub` with `IScopes` in samples
adinauer Apr 2, 2024
3f25a4b
Merge branch 'feat/hsm-13-replacements-in-samples' into feat/hubs-sco…
adinauer Apr 2, 2024
d6fb40a
gitscopes -> github
adinauer Apr 2, 2024
7752bcc
Replace ThreadLocal with ScopesStorage
adinauer Apr 4, 2024
1e329c5
Move client and throwable to span map to scope
adinauer Apr 4, 2024
b0d89ae
Add global scope
adinauer Apr 4, 2024
cdd414a
use global scope in Scopes
adinauer Apr 4, 2024
98da9ff
Implement pushScope popScope and withScope for Scopes
adinauer Apr 4, 2024
2d26033
Add pushIsolationScope; add fork methods to ISCope
adinauer Apr 12, 2024
bbb6700
Use separate scopes for current, isolation and global scope; rename m…
adinauer Apr 12, 2024
c714b21
Allow controlling which scope configureScope uses
adinauer Apr 12, 2024
3d6f2fe
Merge branch '8.x.x' into feat/hsm-21-configure-scope-configurable
adinauer Apr 19, 2024
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
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) {
Copy link
Member

Choose a reason for hiding this comment

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

m: I guess we should do the same for withScope

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, will add in a follow up PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, this has the same problem as having an overload for forkedScope where withScope(ISOLATION) ... forks both isolation and current scope which might be confusing.

Maybe having another method called withIsolationScope, as RFC suggests, is better but very similar.

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding withIsolationScope in a follow up PR

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
Loading