Skip to content

Commit a941eb8

Browse files
authored
Hubs/Scopes Merge 21 - Allow controlling which scope configureScope 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
1 parent 385666d commit a941eb8

File tree

14 files changed

+82
-24
lines changed

14 files changed

+82
-24
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.sentry.ILogger;
1111
import io.sentry.ITransactionProfiler;
1212
import io.sentry.NoOpConnectionStatusProvider;
13+
import io.sentry.ScopeType;
1314
import io.sentry.SendFireAndForgetEnvelopeSender;
1415
import io.sentry.SendFireAndForgetOutboxSender;
1516
import io.sentry.SentryLevel;
@@ -98,6 +99,8 @@ static void loadDefaultAndMetadataOptions(
9899
// Firstly set the logger, if `debug=true` configured, logging can start asap.
99100
options.setLogger(logger);
100101

102+
options.setDefaultScopeType(ScopeType.CURRENT);
103+
101104
options.setDateProvider(new SentryAndroidDateProvider());
102105

103106
// set a lower flush timeout on Android to avoid ANRs

sentry/src/main/java/io/sentry/Hub.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,8 @@ public void withScope(final @NotNull ScopeCallback callback) {
594594
}
595595

596596
@Override
597-
public void configureScope(final @NotNull ScopeCallback callback) {
597+
public void configureScope(
598+
final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) {
598599
if (!isEnabled()) {
599600
options
600601
.getLogger()

sentry/src/main/java/io/sentry/HubAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ public void withScope(@NotNull ScopeCallback callback) {
174174
}
175175

176176
@Override
177-
public void configureScope(@NotNull ScopeCallback callback) {
178-
Sentry.configureScope(callback);
177+
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {
178+
Sentry.configureScope(scopeType, callback);
179179
}
180180

181181
@Override

sentry/src/main/java/io/sentry/HubScopesWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public void withScope(@NotNull ScopeCallback callback) {
169169
}
170170

171171
@Override
172-
public void configureScope(@NotNull ScopeCallback callback) {
173-
scopes.configureScope(callback);
172+
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {
173+
scopes.configureScope(scopeType, callback);
174174
}
175175

176176
@Override

sentry/src/main/java/io/sentry/IScopes.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,16 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) {
331331
*
332332
* @param callback The configure scope callback.
333333
*/
334-
void configureScope(@NotNull ScopeCallback callback);
334+
default void configureScope(@NotNull ScopeCallback callback) {
335+
configureScope(null, callback);
336+
}
337+
338+
/**
339+
* Configures the scope through the callback.
340+
*
341+
* @param callback The configure scope callback.
342+
*/
343+
void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback);
335344

336345
/**
337346
* Binds a different client to the hub

sentry/src/main/java/io/sentry/NoOpHub.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void withScope(@NotNull ScopeCallback callback) {
142142
}
143143

144144
@Override
145-
public void configureScope(@NotNull ScopeCallback callback) {}
145+
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {}
146146

147147
@Override
148148
public void bindClient(@NotNull ISentryClient client) {}

sentry/src/main/java/io/sentry/NoOpScopes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void withScope(@NotNull ScopeCallback callback) {
140140
}
141141

142142
@Override
143-
public void configureScope(@NotNull ScopeCallback callback) {}
143+
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {}
144144

145145
@Override
146146
public void bindClient(@NotNull ISentryClient client) {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.sentry;
2+
3+
public enum ScopeType {
4+
CURRENT,
5+
ISOLATION,
6+
GLOBAL;
7+
}

sentry/src/main/java/io/sentry/Scopes.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,16 +428,31 @@ public void addBreadcrumb(final @NotNull Breadcrumb breadcrumb, final @Nullable
428428
}
429429
}
430430

431-
private IScope getDefaultConfigureScope() {
432-
// TODO configurable default scope via SentryOptions, Android = global or isolation, backend =
433-
// isolation
434-
return scope;
435-
}
431+
private IScope getSpecificScope(final @Nullable ScopeType scopeType) {
432+
if (scopeType != null) {
433+
switch (scopeType) {
434+
case CURRENT:
435+
return scope;
436+
case ISOLATION:
437+
return isolationScope;
438+
case GLOBAL:
439+
return getGlobalScope();
440+
default:
441+
break;
442+
}
443+
}
436444

437-
private IScope getDefaultWriteScope() {
438-
// TODO configurable default scope via SentryOptions, Android = global or isolation, backend =
439-
// isolation
440-
return getIsolationScope();
445+
switch (getOptions().getDefaultScopeType()) {
446+
case CURRENT:
447+
return scope;
448+
case ISOLATION:
449+
return isolationScope;
450+
case GLOBAL:
451+
return getGlobalScope();
452+
default:
453+
// calm the compiler
454+
return scope;
455+
}
441456
}
442457

443458
@Override
@@ -657,7 +672,8 @@ public void withScope(final @NotNull ScopeCallback callback) {
657672
}
658673

659674
@Override
660-
public void configureScope(final @NotNull ScopeCallback callback) {
675+
public void configureScope(
676+
final @Nullable ScopeType scopeType, final @NotNull ScopeCallback callback) {
661677
if (!isEnabled()) {
662678
options
663679
.getLogger()
@@ -666,7 +682,7 @@ public void configureScope(final @NotNull ScopeCallback callback) {
666682
"Instance is disabled and this 'configureScope' call is a no-op.");
667683
} else {
668684
try {
669-
callback.run(getDefaultConfigureScope());
685+
callback.run(getSpecificScope(scopeType));
670686
} catch (Throwable e) {
671687
options.getLogger().log(SentryLevel.ERROR, "Error in the 'configureScope' callback.", e);
672688
}

sentry/src/main/java/io/sentry/ScopesAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ public void withScope(@NotNull ScopeCallback callback) {
170170
}
171171

172172
@Override
173-
public void configureScope(@NotNull ScopeCallback callback) {
174-
Sentry.configureScope(callback);
173+
public void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback) {
174+
Sentry.configureScope(scopeType, callback);
175175
}
176176

177177
@Override

0 commit comments

Comments
 (0)