diff --git a/android-agent/api/android-agent.api b/android-agent/api/android-agent.api index b9ea6249a..33fd48779 100644 --- a/android-agent/api/android-agent.api +++ b/android-agent/api/android-agent.api @@ -105,6 +105,7 @@ public final class io/opentelemetry/android/agent/dsl/instrumentation/Instrument public final fun networkMonitoring (Lkotlin/jvm/functions/Function1;)V public final fun screenOrientation (Lkotlin/jvm/functions/Function1;)V public final fun slowRenderingReporter (Lkotlin/jvm/functions/Function1;)V + public final fun suppressing ([Ljava/lang/String;)V } public final class io/opentelemetry/android/agent/dsl/instrumentation/NetworkMonitoringConfiguration : io/opentelemetry/android/agent/dsl/instrumentation/CanBeEnabledAndDisabled { diff --git a/android-agent/src/main/kotlin/io/opentelemetry/android/agent/dsl/instrumentation/InstrumentationConfiguration.kt b/android-agent/src/main/kotlin/io/opentelemetry/android/agent/dsl/instrumentation/InstrumentationConfiguration.kt index 331adb356..5fd23dea3 100644 --- a/android-agent/src/main/kotlin/io/opentelemetry/android/agent/dsl/instrumentation/InstrumentationConfiguration.kt +++ b/android-agent/src/main/kotlin/io/opentelemetry/android/agent/dsl/instrumentation/InstrumentationConfiguration.kt @@ -14,7 +14,7 @@ import io.opentelemetry.android.instrumentation.AndroidInstrumentationLoader */ @OpenTelemetryDslMarker class InstrumentationConfiguration internal constructor( - config: OtelRumConfig, + private val config: OtelRumConfig, private val instrumentationLoader: AndroidInstrumentationLoader, ) { private val activity: ActivityLifecycleConfiguration by lazy { @@ -84,4 +84,16 @@ class InstrumentationConfiguration internal constructor( fun screenOrientation(configure: ScreenOrientationConfiguration.() -> Unit) { screenOrientation.configure() } + + /** + * Suppresses the named instrumentations so they will not be installed at startup. + * + * The values passed here must match the instrumentation names as exposed by + * AndroidInstrumentation. + */ + fun suppressing(vararg instrumentationsToExclude: String) { + instrumentationsToExclude.forEach { + config.suppressInstrumentation(it) + } + } } diff --git a/android-agent/src/test/kotlin/io/opentelemetry/android/agent/dsl/InstrumentationConfigurationTest.kt b/android-agent/src/test/kotlin/io/opentelemetry/android/agent/dsl/InstrumentationConfigurationTest.kt new file mode 100644 index 000000000..de0e65156 --- /dev/null +++ b/android-agent/src/test/kotlin/io/opentelemetry/android/agent/dsl/InstrumentationConfigurationTest.kt @@ -0,0 +1,40 @@ +package io.opentelemetry.android.agent.dsl + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import io.opentelemetry.android.agent.FakeClock +import io.opentelemetry.android.agent.FakeInstrumentationLoader +import io.opentelemetry.android.config.OtelRumConfig +import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig +import org.junit.Before +import org.junit.Test + +class InstrumentationConfigurationTest { + private lateinit var otelConfig: OpenTelemetryConfiguration + private lateinit var rumConfig: OtelRumConfig + + @Before + fun setUp() { + rumConfig = mockk() + every { rumConfig.setDiskBufferingConfig(any()) } returns rumConfig + every { rumConfig.suppressInstrumentation(any()) } returns rumConfig + otelConfig = OpenTelemetryConfiguration( + rumConfig = rumConfig, + instrumentationLoader = FakeInstrumentationLoader(), + clock = FakeClock() + ) + } + + @Test + fun canSuppressInstrumentation() { + otelConfig.instrumentations { + suppressing("one", "two", "three") + } + verify { + rumConfig.suppressInstrumentation("one") + rumConfig.suppressInstrumentation("two") + rumConfig.suppressInstrumentation("three") + } + } +} diff --git a/demo-app/src/main/java/io/opentelemetry/android/demo/OtelDemoApplication.kt b/demo-app/src/main/java/io/opentelemetry/android/demo/OtelDemoApplication.kt index aeca878c6..7572a47fc 100644 --- a/demo-app/src/main/java/io/opentelemetry/android/demo/OtelDemoApplication.kt +++ b/demo-app/src/main/java/io/opentelemetry/android/demo/OtelDemoApplication.kt @@ -40,7 +40,16 @@ class OtelDemoApplication : Application() { globalAttributes { Attributes.of(stringKey("toolkit"), "jetpack compose") } - } + instrumentations { + suppressing ( + "unwanted.instrumentation.name", + "something.unwanted" + ) + screenOrientation { + enabled(false) + } + } + }, ) Log.d(TAG, "RUM session started: " + rum?.sessionProvider?.getSessionId()) } catch (e: Exception) {