Skip to content

Commit d872fbc

Browse files
adinauergetsentry-botgetsentry-bot
authored
Avoid forking rootScopes for Reactor if current thread has NoOpScopes (#4793)
* Avoid forking rootScopes for Reactor if current thread has NoOpScopes * release: 8.23.1-alpha.1 * fix changelog; replace hasScopes with bool param on getCurrentScopes * move changelog entry * move changelog again --------- Co-authored-by: getsentry-bot <[email protected]> Co-authored-by: getsentry-bot <[email protected]>
1 parent 8285e52 commit d872fbc

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Improvements
6+
7+
- Avoid forking `rootScopes` for Reactor if current thread has `NoOpScopes` ([#4793](https://github.com/getsentry/sentry-java/pull/4793))
8+
- This reduces the SDKs overhead by avoiding unnecessary scope forks
9+
310
## 8.27.1
411

512
### Fixes

sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Object key() {
1616

1717
@Override
1818
public IScopes getValue() {
19-
return Sentry.getCurrentScopes();
19+
return Sentry.getCurrentScopes(false);
2020
}
2121

2222
@Override

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,6 +2631,7 @@ public final class io/sentry/Sentry {
26312631
public static fun getBaggage ()Lio/sentry/BaggageHeader;
26322632
public static fun getCurrentHub ()Lio/sentry/IHub;
26332633
public static fun getCurrentScopes ()Lio/sentry/IScopes;
2634+
public static fun getCurrentScopes (Z)Lio/sentry/IScopes;
26342635
public static fun getGlobalScope ()Lio/sentry/IScope;
26352636
public static fun getLastEventId ()Lio/sentry/protocol/SentryId;
26362637
public static fun getSpan ()Lio/sentry/ISpan;

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,33 @@ private Sentry() {}
9797
return new HubScopesWrapper(getCurrentScopes());
9898
}
9999

100-
@ApiStatus.Internal // exposed for the coroutines integration in SentryContext
100+
@ApiStatus.Internal
101101
@SuppressWarnings("deprecation")
102102
public static @NotNull IScopes getCurrentScopes() {
103+
return getCurrentScopes(true);
104+
}
105+
106+
/**
107+
* Returns the current contexts scopes.
108+
*
109+
* @param ensureForked if true, forks root scopes in case there are no scopes for this context if
110+
* false, returns NoOpScopes if there are no scopes for this context
111+
* @return current scopes, a root scopes fork or NoopScopes
112+
*/
113+
@ApiStatus.Internal
114+
@SuppressWarnings("deprecation")
115+
public static @NotNull IScopes getCurrentScopes(final boolean ensureForked) {
103116
if (globalHubMode) {
104117
return rootScopes;
105118
}
106119
@Nullable IScopes scopes = getScopesStorage().get();
107120
if (scopes == null || scopes.isNoOp()) {
108-
scopes = rootScopes.forkedScopes("getCurrentScopes");
109-
getScopesStorage().set(scopes);
121+
if (!ensureForked) {
122+
return NoOpScopes.getInstance();
123+
} else {
124+
scopes = rootScopes.forkedScopes("getCurrentScopes");
125+
getScopesStorage().set(scopes);
126+
}
110127
}
111128
return scopes;
112129
}

0 commit comments

Comments
 (0)