diff --git a/CHANGELOG.md b/CHANGELOG.md index 77a48c8140e..81a1624de68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ - Fix profilerId propagation ([#4833](https://github.com/getsentry/sentry-java/pull/4833)) - Fix profiling init for Spring and Spring Boot w Agent auto-init ([#4815](https://github.com/getsentry/sentry-java/pull/4815)) - Copy active span on scope clone ([#4878](https://github.com/getsentry/sentry-java/pull/4878)) +- 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 ### Improvements 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 fb241c9b4ef..855818f186e 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; }