Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions sentry/src/main/java/io/sentry/IScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/NoOpScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 17 additions & 3 deletions sentry/src/main/java/io/sentry/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,20 +42,23 @@ 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(
final @NotNull IScope scope,
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();
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions sentry/src/main/java/io/sentry/ScopesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integration>()
Expand Down
Loading