Skip to content

Commit 55837d2

Browse files
committed
feat(tracing): Add adapter class for Android binder instrumentation
1 parent b936425 commit 55837d2

10 files changed

Lines changed: 386 additions & 0 deletions

File tree

‎sentry-android-core/api/sentry-android-core.api‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
386386
public fun isEnableAppLifecycleBreadcrumbs ()Z
387387
public fun isEnableAutoActivityLifecycleTracing ()Z
388388
public fun isEnableAutoTraceIdGeneration ()Z
389+
public fun isEnableBinderLogging ()Z
390+
public fun isEnableBinderTracing ()Z
389391
public fun isEnableFramesTracking ()Z
390392
public fun isEnableNdk ()Z
391393
public fun isEnableNetworkEventBreadcrumbs ()Z
@@ -417,6 +419,8 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
417419
public fun setEnableAppLifecycleBreadcrumbs (Z)V
418420
public fun setEnableAutoActivityLifecycleTracing (Z)V
419421
public fun setEnableAutoTraceIdGeneration (Z)V
422+
public fun setEnableBinderLogging (Z)V
423+
public fun setEnableBinderTracing (Z)V
420424
public fun setEnableFramesTracking (Z)V
421425
public fun setEnableNdk (Z)V
422426
public fun setEnableNetworkEventBreadcrumbs (Z)V

‎sentry-android-core/proguard-rules.pro‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@
9292
-dontwarn io.sentry.spotlight.SpotlightIntegration
9393
-keepnames class io.sentry.spotlight.SpotlightIntegration
9494
##---------------End: proguard configuration for sentry-spotlight ----------
95+
96+
-keepnames class io.sentry.android.core.internal.binder.SentryBinderAdapter { *; }

‎sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ final class ManifestMetadataReader {
188188

189189
static final String ENABLE_ANR_FINGERPRINTING = "io.sentry.anr.enable-fingerprinting";
190190

191+
static final String ENABLE_BINDER_TRACING = "io.sentry.traces.binder.enable";
192+
193+
static final String ENABLE_BINDER_LOGGING = "io.sentry.logs.binder.enable";
194+
191195
/** ManifestMetadataReader ctor */
192196
private ManifestMetadataReader() {}
193197

@@ -725,6 +729,12 @@ static void applyMetadata(
725729
options.setEnableAnrFingerprinting(
726730
readBool(
727731
metadata, logger, ENABLE_ANR_FINGERPRINTING, options.isEnableAnrFingerprinting()));
732+
733+
options.setEnableBinderTracing(
734+
readBool(metadata, logger, ENABLE_BINDER_TRACING, options.isEnableBinderTracing()));
735+
736+
options.setEnableBinderLogging(
737+
readBool(metadata, logger, ENABLE_BINDER_LOGGING, options.isEnableBinderLogging()));
728738
}
729739
options
730740
.getLogger()

‎sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.sentry.SentryLevel;
1515
import io.sentry.SentryOptions;
1616
import io.sentry.Session;
17+
import io.sentry.android.core.internal.binder.SentryBinderAdapter;
1718
import io.sentry.android.core.performance.AppStartMetrics;
1819
import io.sentry.android.core.performance.TimeSpan;
1920
import io.sentry.android.fragment.FragmentLifecycleIntegration;
@@ -151,6 +152,9 @@ public static void init(
151152
t);
152153
}
153154

155+
SentryBinderAdapter.setEnabled(
156+
options.isEnableBinderTracing(), options.isEnableBinderLogging());
157+
154158
// if SentryPerformanceProvider was disabled or removed,
155159
// we set the app start / sdk init time here instead
156160
final @NotNull AppStartMetrics appStartMetrics = AppStartMetrics.getInstance();

‎sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ public interface BeforeCaptureCallback {
263263

264264
private boolean enableAnrFingerprinting = true;
265265

266+
/** Enable or disable creating spans for binder (IPC) calls. Default is disabled. */
267+
private boolean enableBinderTracing = false;
268+
269+
/** Enable or disable logging of binder (IPC) calls. Default is disabled. */
270+
private boolean enableBinderLogging = false;
271+
266272
public SentryAndroidOptions() {
267273
setSentryClientName(BuildConfig.SENTRY_ANDROID_SDK_NAME + "/" + BuildConfig.VERSION_NAME);
268274
setSdkVersion(createSdkVersion());
@@ -755,6 +761,42 @@ public void setEnableAnrFingerprinting(final boolean enableAnrFingerprinting) {
755761
this.enableAnrFingerprinting = enableAnrFingerprinting;
756762
}
757763

764+
/**
765+
* Returns whether creating spans for binder (IPC) calls is enabled. Default is disabled.
766+
*
767+
* @return true if binder spans are enabled
768+
*/
769+
public boolean isEnableBinderTracing() {
770+
return enableBinderTracing;
771+
}
772+
773+
/**
774+
* Enables or disables creating spans for binder (IPC) calls.
775+
*
776+
* @param enableBinderTracing true to enable binder spans
777+
*/
778+
public void setEnableBinderTracing(final boolean enableBinderTracing) {
779+
this.enableBinderTracing = enableBinderTracing;
780+
}
781+
782+
/**
783+
* Returns whether logging of binder (IPC) calls is enabled. Default is disabled.
784+
*
785+
* @return true if binder logging is enabled
786+
*/
787+
public boolean isEnableBinderLogging() {
788+
return enableBinderLogging;
789+
}
790+
791+
/**
792+
* Enables or disables logging of binder (IPC) calls.
793+
*
794+
* @param enableBinderLogging true to enable binder logging
795+
*/
796+
public void setEnableBinderLogging(final boolean enableBinderLogging) {
797+
this.enableBinderLogging = enableBinderLogging;
798+
}
799+
758800
static class AndroidUserFeedbackFormHandler implements SentryFeedbackOptions.IFormHandler {
759801
@Override
760802
public void showForm(
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package io.sentry.android.core.internal.binder;
2+
3+
import android.os.Build;
4+
import io.sentry.ISpan;
5+
import io.sentry.Sentry;
6+
import io.sentry.SentryAttributes;
7+
import io.sentry.SentryLogLevel;
8+
import io.sentry.SpanDataConvention;
9+
import io.sentry.logger.SentryLogParameters;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import java.util.concurrent.atomic.AtomicInteger;
13+
import org.jetbrains.annotations.ApiStatus;
14+
import org.jetbrains.annotations.NotNull;
15+
import org.jetbrains.annotations.Nullable;
16+
17+
@SuppressWarnings({"unused", "deprecation"})
18+
@ApiStatus.Internal
19+
public final class SentryBinderAdapter {
20+
21+
private static final AtomicInteger cookieCounter = new AtomicInteger();
22+
private static final int NO_COOKIE = -1;
23+
24+
private static volatile boolean tracingEnabled = false;
25+
private static volatile boolean loggingEnabled = false;
26+
27+
/** Configures which binder features are active. Expected to be called once during SDK init. */
28+
public static void setEnabled(final boolean tracingEnabled, final boolean loggingEnabled) {
29+
SentryBinderAdapter.tracingEnabled = tracingEnabled;
30+
SentryBinderAdapter.loggingEnabled = loggingEnabled;
31+
}
32+
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) {
42+
if (!tracingEnabled && !loggingEnabled) {
43+
return NO_COOKIE;
44+
}
45+
46+
try {
47+
final @NotNull Thread currentThread = Thread.currentThread();
48+
final long threadId;
49+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
50+
threadId = currentThread.threadId();
51+
} else {
52+
threadId = currentThread.getId();
53+
}
54+
final @Nullable String threadName = currentThread.getName();
55+
56+
if (loggingEnabled) {
57+
recordLog(component, name, threadId, threadName);
58+
}
59+
if (tracingEnabled) {
60+
final int cookie = cookieCounter.incrementAndGet();
61+
recordSpan(component, name, threadId, threadName, cookie);
62+
return cookie;
63+
}
64+
} catch (Throwable t) {
65+
// ignored, as instrumentation should never crash
66+
}
67+
return NO_COOKIE;
68+
}
69+
70+
public static void onCallEnd(final int cookie) {
71+
if (cookie == NO_COOKIE) {
72+
return;
73+
}
74+
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();
82+
}
83+
} catch (Throwable t) {
84+
// ignored
85+
}
86+
}
87+
88+
private static void recordSpan(
89+
final @NotNull String component,
90+
final @NotNull String name,
91+
final long threadId,
92+
final @Nullable String threadName,
93+
final int cookie) {
94+
95+
final @Nullable ISpan parent = Sentry.getCurrentScopes().getTransaction();
96+
if (parent == null) {
97+
return;
98+
}
99+
final @Nullable Map<Integer, ISpan> map = spanMap.get();
100+
if (map == null) {
101+
return;
102+
}
103+
final @NotNull ISpan span = parent.startChild("binder", component + "." + name);
104+
span.setData(SpanDataConvention.THREAD_ID, String.valueOf(threadId));
105+
span.setData(SpanDataConvention.THREAD_NAME, threadName);
106+
map.put(cookie, span);
107+
}
108+
109+
private static void recordLog(
110+
final @NotNull String component,
111+
final @NotNull String name,
112+
final long threadId,
113+
final @Nullable String threadName) {
114+
final @NotNull Map<String, Object> logAttributes = new HashMap<>();
115+
logAttributes.put(SpanDataConvention.THREAD_ID, threadId);
116+
logAttributes.put(SpanDataConvention.THREAD_NAME, threadName);
117+
118+
Sentry.logger()
119+
.log(
120+
SentryLogLevel.INFO,
121+
SentryLogParameters.create(SentryAttributes.fromMap(logAttributes)),
122+
"binder call: %s.%s",
123+
component,
124+
name);
125+
}
126+
}

‎sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,4 +2586,54 @@ class ManifestMetadataReaderTest {
25862586
// Assert
25872587
assertEquals("12345", fixture.options.orgId)
25882588
}
2589+
2590+
@Test
2591+
fun `applyMetadata reads enableBinderTracing to options`() {
2592+
// Arrange
2593+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_BINDER_TRACING to true)
2594+
val context = fixture.getContext(metaData = bundle)
2595+
2596+
// Act
2597+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2598+
2599+
// Assert
2600+
assertTrue(fixture.options.isEnableBinderTracing)
2601+
}
2602+
2603+
@Test
2604+
fun `applyMetadata keeps enableBinderTracing default when not set in manifest`() {
2605+
// Arrange
2606+
val context = fixture.getContext()
2607+
2608+
// Act
2609+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2610+
2611+
// Assert
2612+
assertFalse(fixture.options.isEnableBinderTracing)
2613+
}
2614+
2615+
@Test
2616+
fun `applyMetadata reads enableBinderLogging to options`() {
2617+
// Arrange
2618+
val bundle = bundleOf(ManifestMetadataReader.ENABLE_BINDER_LOGGING to true)
2619+
val context = fixture.getContext(metaData = bundle)
2620+
2621+
// Act
2622+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2623+
2624+
// Assert
2625+
assertTrue(fixture.options.isEnableBinderLogging)
2626+
}
2627+
2628+
@Test
2629+
fun `applyMetadata keeps enableBinderLogging default when not set in manifest`() {
2630+
// Arrange
2631+
val context = fixture.getContext()
2632+
2633+
// Act
2634+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
2635+
2636+
// Assert
2637+
assertFalse(fixture.options.isEnableBinderLogging)
2638+
}
25892639
}

‎sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidOptionsTest.kt‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ class SentryAndroidOptionsTest {
114114
assertFalse(sentryOptions.isAttachViewHierarchy)
115115
}
116116

117+
@Test
118+
fun `binder tracing is disabled by default for Android`() {
119+
val sentryOptions = SentryAndroidOptions()
120+
121+
assertFalse(sentryOptions.isEnableBinderTracing)
122+
}
123+
124+
@Test
125+
fun `binder logging is disabled by default for Android`() {
126+
val sentryOptions = SentryAndroidOptions()
127+
128+
assertFalse(sentryOptions.isEnableBinderLogging)
129+
}
130+
131+
@Test
132+
fun `binder tracing can be enabled`() {
133+
val sentryOptions = SentryAndroidOptions()
134+
sentryOptions.isEnableBinderTracing = true
135+
136+
assertTrue(sentryOptions.isEnableBinderTracing)
137+
}
138+
139+
@Test
140+
fun `binder logging can be enabled`() {
141+
val sentryOptions = SentryAndroidOptions()
142+
sentryOptions.isEnableBinderLogging = true
143+
144+
assertTrue(sentryOptions.isEnableBinderLogging)
145+
}
146+
117147
@Test
118148
fun `native sdk name is null by default`() {
119149
val sentryOptions = SentryAndroidOptions()

0 commit comments

Comments
 (0)