diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 9f88d6323f..62ebe808f3 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -984,6 +984,7 @@ public abstract interface class io/sentry/IScopes { public abstract fun isAncestorOf (Lio/sentry/IScopes;)Z public abstract fun isCrashedLastRun ()Ljava/lang/Boolean; public abstract fun isEnabled ()Z + public abstract fun isFromSameSdkInitialization (Lio/sentry/IScopes;)Z public abstract fun isHealthy ()Z public fun isNoOp ()Z public abstract fun logger ()Lio/sentry/logger/ILoggerApi; @@ -1717,6 +1718,7 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes { public fun isAncestorOf (Lio/sentry/IScopes;)Z public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z + public fun isFromSameSdkInitialization (Lio/sentry/IScopes;)Z public fun isHealthy ()Z public fun isNoOp ()Z public fun logger ()Lio/sentry/logger/ILoggerApi; @@ -2468,6 +2470,7 @@ public final class io/sentry/Scopes : io/sentry/IScopes { public fun isAncestorOf (Lio/sentry/IScopes;)Z public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z + public fun isFromSameSdkInitialization (Lio/sentry/IScopes;)Z public fun isHealthy ()Z public fun logger ()Lio/sentry/logger/ILoggerApi; public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; @@ -2542,6 +2545,7 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes { public fun isAncestorOf (Lio/sentry/IScopes;)Z public fun isCrashedLastRun ()Ljava/lang/Boolean; public fun isEnabled ()Z + public fun isFromSameSdkInitialization (Lio/sentry/IScopes;)Z public fun isHealthy ()Z public fun logger ()Lio/sentry/logger/ILoggerApi; public fun makeCurrent ()Lio/sentry/ISentryLifecycleToken; diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index 6b5a2b8a5b..acf5485ec1 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -486,6 +486,15 @@ default void configureScope(@NotNull ScopeCallback callback) { @ApiStatus.Internal boolean isAncestorOf(final @Nullable IScopes otherScopes); + /** + * Checks whether this and the other Scopes belong to the same SDK initialization. + * + * @param otherScopes Scopes instance to compare with + * @return true if both Scopes belong to the same SDK initialization + */ + @ApiStatus.Internal + boolean isFromSameSdkInitialization(final @Nullable IScopes otherScopes); + /** * Captures the transaction and enqueues it for sending to Sentry server. * diff --git a/sentry/src/main/java/io/sentry/NoOpScopes.java b/sentry/src/main/java/io/sentry/NoOpScopes.java index f40ae029e0..070467ada1 100644 --- a/sentry/src/main/java/io/sentry/NoOpScopes.java +++ b/sentry/src/main/java/io/sentry/NoOpScopes.java @@ -210,6 +210,11 @@ public boolean isAncestorOf(@Nullable IScopes otherScopes) { return false; } + @Override + public boolean isFromSameSdkInitialization(@Nullable IScopes otherScopes) { + return false; + } + @Override public @NotNull SentryId captureTransaction( final @NotNull SentryTransaction transaction, diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 4edd9180d2..f65e78465d 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -27,6 +27,7 @@ public final class Scopes implements IScopes { private final @NotNull IScope globalScope; private final @Nullable Scopes parentScopes; + private final @NotNull Object sdkInitializationToken; private final @NotNull String creator; private final @NotNull CompositePerformanceCollector compositePerformanceCollector; @@ -41,7 +42,7 @@ public Scopes( final @NotNull IScope isolationScope, final @NotNull IScope globalScope, final @NotNull String creator) { - this(scope, isolationScope, globalScope, null, creator); + this(scope, isolationScope, globalScope, null, null, creator); } private Scopes( @@ -49,12 +50,15 @@ private Scopes( final @NotNull IScope isolationScope, final @NotNull IScope globalScope, final @Nullable Scopes parentScopes, + final @Nullable Object sdkInitializationToken, final @NotNull String creator) { this.combinedScope = new CombinedScopeView(globalScope, isolationScope, scope); this.scope = scope; this.isolationScope = isolationScope; this.globalScope = globalScope; this.parentScopes = parentScopes; + this.sdkInitializationToken = + sdkInitializationToken == null ? new Object() : sdkInitializationToken; this.creator = creator; final @NotNull SentryOptions options = getOptions(); @@ -111,14 +115,24 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) { return false; } + @Override + @ApiStatus.Internal + @SuppressWarnings("ReferenceEquality") + public boolean isFromSameSdkInitialization(final @Nullable IScopes otherScopes) { + return otherScopes instanceof Scopes + && sdkInitializationToken == ((Scopes) otherScopes).sdkInitializationToken; + } + @Override public @NotNull IScopes forkedScopes(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope.clone(), globalScope, this, creator); + return new Scopes( + scope.clone(), isolationScope.clone(), globalScope, this, sdkInitializationToken, creator); } @Override public @NotNull IScopes forkedCurrentScope(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope, globalScope, this, creator); + return new Scopes( + scope.clone(), isolationScope, globalScope, this, sdkInitializationToken, creator); } @Override diff --git a/sentry/src/main/java/io/sentry/ScopesAdapter.java b/sentry/src/main/java/io/sentry/ScopesAdapter.java index ae38793c6c..3e3f8a1ec9 100644 --- a/sentry/src/main/java/io/sentry/ScopesAdapter.java +++ b/sentry/src/main/java/io/sentry/ScopesAdapter.java @@ -250,6 +250,11 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) { return Sentry.getCurrentScopes().isAncestorOf(otherScopes); } + @Override + public boolean isFromSameSdkInitialization(final @Nullable IScopes otherScopes) { + return Sentry.getCurrentScopes().isFromSameSdkInitialization(otherScopes); + } + @ApiStatus.Internal @Override public @NotNull SentryId captureTransaction( diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 932569177b..213527f1c8 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -106,7 +106,7 @@ private Sentry() {} // in global hub mode we never fork implicitly, but scopes that have explicitly been made // current (e.g. by withScope) must still be honoured. Anything that did not originate from // the present rootScopes is stale (SDK closed or re-initialized) and gets ignored. - if (scopes != null && !scopes.isNoOp() && root.isAncestorOf(scopes)) { + if (scopes != null && !scopes.isNoOp() && root.isFromSameSdkInitialization(scopes)) { return scopes; } return root; diff --git a/sentry/src/test/java/io/sentry/ScopesTest.kt b/sentry/src/test/java/io/sentry/ScopesTest.kt index 05647d544b..c319b25a57 100644 --- a/sentry/src/test/java/io/sentry/ScopesTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesTest.kt @@ -98,6 +98,20 @@ class ScopesTest { ) } + @Test + fun `SDK initialization identity is inherited by forks`() { + val options = SentryOptions() + options.dsn = "https://key@sentry.io/proj" + options.setSerializer(mock()) + val firstRoot = createScopes(options) + val descendant = firstRoot.forkedCurrentScope("parent").forkedCurrentScope("descendant") + val secondRoot = createScopes(options) + + assertTrue(firstRoot.isFromSameSdkInitialization(descendant)) + assertTrue(descendant.isFromSameSdkInitialization(firstRoot)) + assertFalse(firstRoot.isFromSameSdkInitialization(secondRoot)) + } + @Test fun `when isolation scope is forked, integrations are not registered`() { val integrationMock = mock()