Skip to content

Commit c64ee45

Browse files
committed
Switch to object token in favor of int cookie
As this removes any need for synchronization within SentryBindingAdapter, saves one cookie->span lookup, and removes the potential need of housekeeping spans
1 parent bdaa739 commit c64ee45

2 files changed

Lines changed: 40 additions & 48 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/internal/binder/SentryBinderAdapter.java‎

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.sentry.logger.SentryLogParameters;
1010
import java.util.HashMap;
1111
import java.util.Map;
12-
import java.util.concurrent.atomic.AtomicInteger;
1312
import org.jetbrains.annotations.ApiStatus;
1413
import org.jetbrains.annotations.NotNull;
1514
import org.jetbrains.annotations.Nullable;
@@ -18,9 +17,6 @@
1817
@ApiStatus.Internal
1918
public 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(

‎sentry-android-core/src/test/java/io/sentry/android/core/internal/binder/SentryBinderAdapterTest.kt‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import io.sentry.logger.SentryLogParameters
1010
import kotlin.test.AfterTest
1111
import kotlin.test.BeforeTest
1212
import kotlin.test.Test
13-
import kotlin.test.assertEquals
14-
import kotlin.test.assertNotEquals
13+
import kotlin.test.assertNotNull
14+
import kotlin.test.assertNull
1515
import org.mockito.Mockito.mockStatic
1616
import org.mockito.kotlin.any
1717
import org.mockito.kotlin.eq
@@ -51,12 +51,12 @@ class SentryBinderAdapterTest {
5151
}
5252

5353
@Test
54-
fun `returns no cookie and does nothing when both features disabled`() {
54+
fun `returns no token and does nothing when both features disabled`() {
5555
SentryBinderAdapter.setEnabled(false, false)
5656

57-
val cookie = SentryBinderAdapter.onCallStart("ActivityManager", "getRunningTasks")
57+
val token = SentryBinderAdapter.onCallStart("ActivityManager", "getRunningTasks")
5858

59-
assertEquals(-1, cookie)
59+
assertNull(token)
6060
verify(fixture.transaction, never()).startChild(any<String>(), any<String>())
6161
verifyNoInteractions(fixture.logger)
6262
}
@@ -65,12 +65,12 @@ class SentryBinderAdapterTest {
6565
fun `starts and finishes a span when tracing is enabled`() {
6666
SentryBinderAdapter.setEnabled(true, false)
6767

68-
val cookie = SentryBinderAdapter.onCallStart("ActivityManager", "getRunningTasks")
68+
val token = SentryBinderAdapter.onCallStart("ActivityManager", "getRunningTasks")
6969

70-
assertNotEquals(-1, cookie)
70+
assertNotNull(token)
7171
verify(fixture.transaction).startChild(eq("binder"), eq("ActivityManager.getRunningTasks"))
7272

73-
SentryBinderAdapter.onCallEnd(cookie)
73+
SentryBinderAdapter.onCallEnd(token)
7474
verify(fixture.span).finish()
7575
}
7676

@@ -88,9 +88,9 @@ class SentryBinderAdapterTest {
8888
fun `records a log when logging is enabled`() {
8989
SentryBinderAdapter.setEnabled(false, true)
9090

91-
val cookie = SentryBinderAdapter.onCallStart("ActivityManager", "getRunningTasks")
91+
val token = SentryBinderAdapter.onCallStart("ActivityManager", "getRunningTasks")
9292

93-
assertEquals(-1, cookie)
93+
assertNull(token)
9494
verify(fixture.logger)
9595
.log(
9696
eq(SentryLogLevel.INFO),
@@ -102,10 +102,10 @@ class SentryBinderAdapterTest {
102102
}
103103

104104
@Test
105-
fun `onCallEnd with no cookie is a no-op`() {
105+
fun `onCallEnd with null token is a no-op`() {
106106
SentryBinderAdapter.setEnabled(true, false)
107107

108-
SentryBinderAdapter.onCallEnd(-1)
108+
SentryBinderAdapter.onCallEnd(null)
109109

110110
verify(fixture.span, never()).finish()
111111
}

0 commit comments

Comments
 (0)