99import io .sentry .logger .SentryLogParameters ;
1010import java .util .HashMap ;
1111import java .util .Map ;
12- import java .util .concurrent .atomic .AtomicInteger ;
1312import org .jetbrains .annotations .ApiStatus ;
1413import org .jetbrains .annotations .NotNull ;
1514import org .jetbrains .annotations .Nullable ;
1817@ ApiStatus .Internal
1918public final class SentryBinderAdapter {
2019
21- private static final AtomicInteger cookieCounter = new AtomicInteger ();
22- private static final int NO_COOKIE = -1 ;
23-
2420 private static volatile boolean tracingEnabled = false ;
2521 private static volatile boolean loggingEnabled = false ;
2622
@@ -30,17 +26,19 @@ public static void setEnabled(final boolean tracingEnabled, final boolean loggin
3026 SentryBinderAdapter .loggingEnabled = loggingEnabled ;
3127 }
3228
33- private static final ThreadLocal <Map <Integer , ISpan >> spanMap =
34- new ThreadLocal <Map <Integer , ISpan >>() {
35- @ Override
36- protected Map <Integer , ISpan > initialValue () {
37- return new HashMap <>();
38- }
39- };
40-
41- public static int onCallStart (final @ NotNull String component , final @ NotNull String name ) {
29+ /**
30+ * This method is used by the Sentry Android Gradle plugin for binder instrumentation. Called
31+ * right before a binder call starts. Returns an opaque token that must be passed back to {@link
32+ * #onCallEnd(Object)} once the call completes, or {@code null} if nothing was recorded.
33+ *
34+ * @param component the component being called, e.g. "ActivityManager"
35+ * @param name the method being called, e.g. "startActivity"
36+ * @return an opaque token which must be later passed to {@link #onCallEnd(Object)},
37+ */
38+ public static @ Nullable Object onCallStart (
39+ final @ NotNull String component , final @ NotNull String name ) {
4240 if (!tracingEnabled && !loggingEnabled ) {
43- return NO_COOKIE ;
41+ return null ;
4442 }
4543
4644 try {
@@ -57,53 +55,47 @@ public static int onCallStart(final @NotNull String component, final @NotNull St
5755 recordLog (component , name , threadId , threadName );
5856 }
5957 if (tracingEnabled ) {
60- final int cookie = cookieCounter .incrementAndGet ();
61- recordSpan (component , name , threadId , threadName , cookie );
62- return cookie ;
58+ return recordSpan (component , name , threadId , threadName );
6359 }
6460 } catch (Throwable t ) {
6561 // ignored, as instrumentation should never crash
6662 }
67- return NO_COOKIE ;
63+ return null ;
6864 }
6965
70- public static void onCallEnd (final int cookie ) {
71- if (cookie == NO_COOKIE ) {
66+ /**
67+ * This method is used by the Sentry Android Gradle plugin for binder instrumentation. Called
68+ * right after a binder call ends.
69+ *
70+ * @param token the token returned by {@link #onCallStart(String, String)}
71+ */
72+ public static void onCallEnd (final @ Nullable Object token ) {
73+ if (token == null ) {
7274 return ;
7375 }
7476 try {
75- final @ Nullable Map <Integer , ISpan > map = spanMap .get ();
76- if (map == null ) {
77- return ;
78- }
79- final @ Nullable ISpan span = map .remove (cookie );
80- if (span != null ) {
81- span .finish ();
77+ if (token instanceof ISpan ) {
78+ ((ISpan ) token ).finish ();
8279 }
8380 } catch (Throwable t ) {
8481 // ignored
8582 }
8683 }
8784
88- private static void recordSpan (
85+ private static @ Nullable ISpan recordSpan (
8986 final @ NotNull String component ,
9087 final @ NotNull String name ,
9188 final long threadId ,
92- final @ Nullable String threadName ,
93- final int cookie ) {
89+ final @ Nullable String threadName ) {
9490
9591 final @ Nullable ISpan parent = Sentry .getCurrentScopes ().getTransaction ();
9692 if (parent == null ) {
97- return ;
98- }
99- final @ Nullable Map <Integer , ISpan > map = spanMap .get ();
100- if (map == null ) {
101- return ;
93+ return null ;
10294 }
10395 final @ NotNull ISpan span = parent .startChild ("binder" , component + "." + name );
10496 span .setData (SpanDataConvention .THREAD_ID , String .valueOf (threadId ));
10597 span .setData (SpanDataConvention .THREAD_NAME , threadName );
106- map . put ( cookie , span ) ;
98+ return span ;
10799 }
108100
109101 private static void recordLog (
0 commit comments