Skip to content

Commit 3bd17d2

Browse files
adinauerrbro112
authored andcommitted
Add ScopesUtil.printScopesChain for debugging (#4443)
1 parent b623b2a commit 3bd17d2

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

sentry/api/sentry.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub {
711711
public fun getParentScopes ()Lio/sentry/IScopes;
712712
public fun getRateLimiter ()Lio/sentry/transport/RateLimiter;
713713
public fun getScope ()Lio/sentry/IScope;
714+
public fun getScopes ()Lio/sentry/IScopes;
714715
public fun getSpan ()Lio/sentry/ISpan;
715716
public fun getTraceparent ()Lio/sentry/SentryTraceHeader;
716717
public fun getTransaction ()Lio/sentry/ITransaction;
@@ -6921,6 +6922,11 @@ public final class io/sentry/util/SampleRateUtils {
69216922
public static fun isValidTracesSampleRate (Ljava/lang/Double;Z)Z
69226923
}
69236924

6925+
public final class io/sentry/util/ScopesUtil {
6926+
public fun <init> ()V
6927+
public static fun printScopesChain (Lio/sentry/IScopes;)V
6928+
}
6929+
69246930
public final class io/sentry/util/SentryRandom {
69256931
public fun <init> ()V
69266932
public static fun current ()Lio/sentry/util/Random;

sentry/src/main/java/io/sentry/HubScopesWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public HubScopesWrapper(final @NotNull IScopes scopes) {
2121
this.scopes = scopes;
2222
}
2323

24+
public @NotNull IScopes getScopes() {
25+
return scopes;
26+
}
27+
2428
@Override
2529
public boolean isEnabled() {
2630
return scopes.isEnabled();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.sentry.util;
2+
3+
import io.sentry.IScopes;
4+
import io.sentry.Scopes;
5+
import io.sentry.ScopesAdapter;
6+
import io.sentry.Sentry;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
/**
10+
* A util for printing the scopes chain from passed in scopes all the way up to its initial
11+
* ancestor.
12+
*
13+
* <p>It walks up the parents of each scopes instance until it reaches a parent of null. The scopes
14+
* without a parent are the ones created in Sentry.init.
15+
*/
16+
public final class ScopesUtil {
17+
18+
public static void printScopesChain(final @Nullable IScopes scopes) {
19+
System.out.println("==========================================");
20+
System.out.println("=============== v Scopes v ===============");
21+
System.out.println("==========================================");
22+
23+
printScopesChainInternal(scopes);
24+
25+
System.out.println("==========================================");
26+
System.out.println("=============== ^ Scopes ^ ===============");
27+
System.out.println("==========================================");
28+
}
29+
30+
@SuppressWarnings({"ObjectToString", "deprecation"})
31+
private static void printScopesChainInternal(final @Nullable IScopes someScopes) {
32+
if (someScopes != null) {
33+
if (someScopes instanceof Scopes) {
34+
Scopes scopes = (Scopes) someScopes;
35+
String info =
36+
String.format(
37+
"%-25s {g=%-25s, i=%-25s, c=%-25s} [%s]",
38+
scopes,
39+
scopes.getGlobalScope(),
40+
scopes.getIsolationScope(),
41+
scopes.getScope(),
42+
scopes.getCreator());
43+
System.out.println(info);
44+
printScopesChainInternal(someScopes.getParentScopes());
45+
} else if (someScopes instanceof ScopesAdapter
46+
|| someScopes instanceof io.sentry.HubAdapter) {
47+
printScopesChainInternal(Sentry.getCurrentScopes());
48+
} else if (someScopes instanceof io.sentry.HubScopesWrapper) {
49+
io.sentry.HubScopesWrapper wrapper = (io.sentry.HubScopesWrapper) someScopes;
50+
printScopesChainInternal(wrapper.getScopes());
51+
} else {
52+
System.out.println("Hit unhandled Scopes class" + someScopes.getClass());
53+
}
54+
} else {
55+
System.out.println("-");
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)