diff --git a/CHANGELOG.md b/CHANGELOG.md index c2296506c4b..f3835bc43c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Unreleased + +### Improvements + +- Avoid forking `rootScopes` for Reactor if current thread has `NoOpScopes` ([#4793](https://github.com/getsentry/sentry-java/pull/4793)) + - This reduces the SDKs overhead by avoiding unnecessary scope forks + ## 8.27.1 ### Fixes diff --git a/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java b/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java index 7ef4bb9bd1e..d2b841abe70 100644 --- a/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java +++ b/sentry-reactor/src/main/java/io/sentry/reactor/SentryReactorThreadLocalAccessor.java @@ -16,7 +16,7 @@ public Object key() { @Override public IScopes getValue() { - return Sentry.getCurrentScopes(); + return Sentry.getCurrentScopes(false); } @Override diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index a638df4ebef..8f9220eebee 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2631,6 +2631,7 @@ public final class io/sentry/Sentry { public static fun getBaggage ()Lio/sentry/BaggageHeader; public static fun getCurrentHub ()Lio/sentry/IHub; public static fun getCurrentScopes ()Lio/sentry/IScopes; + public static fun getCurrentScopes (Z)Lio/sentry/IScopes; public static fun getGlobalScope ()Lio/sentry/IScope; public static fun getLastEventId ()Lio/sentry/protocol/SentryId; public static fun getSpan ()Lio/sentry/ISpan; diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index bf62fc5e238..f726c1a602c 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -97,16 +97,33 @@ private Sentry() {} return new HubScopesWrapper(getCurrentScopes()); } - @ApiStatus.Internal // exposed for the coroutines integration in SentryContext + @ApiStatus.Internal @SuppressWarnings("deprecation") public static @NotNull IScopes getCurrentScopes() { + return getCurrentScopes(true); + } + + /** + * Returns the current contexts scopes. + * + * @param ensureForked if true, forks root scopes in case there are no scopes for this context if + * false, returns NoOpScopes if there are no scopes for this context + * @return current scopes, a root scopes fork or NoopScopes + */ + @ApiStatus.Internal + @SuppressWarnings("deprecation") + public static @NotNull IScopes getCurrentScopes(final boolean ensureForked) { if (globalHubMode) { return rootScopes; } @Nullable IScopes scopes = getScopesStorage().get(); if (scopes == null || scopes.isNoOp()) { - scopes = rootScopes.forkedScopes("getCurrentScopes"); - getScopesStorage().set(scopes); + if (!ensureForked) { + return NoOpScopes.getInstance(); + } else { + scopes = rootScopes.forkedScopes("getCurrentScopes"); + getScopesStorage().set(scopes); + } } return scopes; }