Skip to content

Commit d054576

Browse files
authored
Add agent dsl to disable instrumentations (#1714)
* add dsl that allows suppressing some automatically loaded instrumentations by name. * detekt * update api * add kdoc
1 parent a26eda2 commit d054576

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

android-agent/api/android-agent.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public final class io/opentelemetry/android/agent/dsl/instrumentation/Instrument
106106
public final fun networkMonitoring (Lkotlin/jvm/functions/Function1;)V
107107
public final fun screenOrientation (Lkotlin/jvm/functions/Function1;)V
108108
public final fun slowRenderingReporter (Lkotlin/jvm/functions/Function1;)V
109+
public final fun suppressing ([Ljava/lang/String;)V
109110
}
110111

111112
public final class io/opentelemetry/android/agent/dsl/instrumentation/NetworkMonitoringConfiguration : io/opentelemetry/android/agent/dsl/instrumentation/CanBeEnabledAndDisabled {

android-agent/src/main/kotlin/io/opentelemetry/android/agent/dsl/instrumentation/InstrumentationConfiguration.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import io.opentelemetry.android.instrumentation.AndroidInstrumentationLoader
1414
*/
1515
@OpenTelemetryDslMarker
1616
class InstrumentationConfiguration internal constructor(
17-
config: OtelRumConfig,
17+
private val config: OtelRumConfig,
1818
private val instrumentationLoader: AndroidInstrumentationLoader,
1919
) {
2020
private val activity: ActivityLifecycleConfiguration by lazy {
@@ -84,4 +84,16 @@ class InstrumentationConfiguration internal constructor(
8484
fun screenOrientation(configure: ScreenOrientationConfiguration.() -> Unit) {
8585
screenOrientation.configure()
8686
}
87+
88+
/**
89+
* Suppresses the named instrumentations so they will not be installed at startup.
90+
*
91+
* The values passed here must match the instrumentation names as exposed by
92+
* AndroidInstrumentation.
93+
*/
94+
fun suppressing(vararg instrumentationsToExclude: String) {
95+
instrumentationsToExclude.forEach {
96+
config.suppressInstrumentation(it)
97+
}
98+
}
8799
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.opentelemetry.android.agent.dsl
2+
3+
import io.mockk.every
4+
import io.mockk.mockk
5+
import io.mockk.verify
6+
import io.opentelemetry.android.agent.FakeClock
7+
import io.opentelemetry.android.agent.FakeInstrumentationLoader
8+
import io.opentelemetry.android.config.OtelRumConfig
9+
import io.opentelemetry.android.features.diskbuffering.DiskBufferingConfig
10+
import org.junit.Before
11+
import org.junit.Test
12+
13+
class InstrumentationConfigurationTest {
14+
private lateinit var otelConfig: OpenTelemetryConfiguration
15+
private lateinit var rumConfig: OtelRumConfig
16+
17+
@Before
18+
fun setUp() {
19+
rumConfig = mockk()
20+
every { rumConfig.setDiskBufferingConfig(any<DiskBufferingConfig>()) } returns rumConfig
21+
every { rumConfig.suppressInstrumentation(any<String>()) } returns rumConfig
22+
otelConfig = OpenTelemetryConfiguration(
23+
rumConfig = rumConfig,
24+
instrumentationLoader = FakeInstrumentationLoader(),
25+
clock = FakeClock()
26+
)
27+
}
28+
29+
@Test
30+
fun canSuppressInstrumentation() {
31+
otelConfig.instrumentations {
32+
suppressing("one", "two", "three")
33+
}
34+
verify {
35+
rumConfig.suppressInstrumentation("one")
36+
rumConfig.suppressInstrumentation("two")
37+
rumConfig.suppressInstrumentation("three")
38+
}
39+
}
40+
}

demo-app/src/main/java/io/opentelemetry/android/demo/OtelDemoApplication.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ class OtelDemoApplication : Application() {
4141
globalAttributes {
4242
Attributes.of(stringKey("toolkit"), "jetpack compose")
4343
}
44-
}
44+
instrumentations {
45+
suppressing (
46+
"unwanted.instrumentation.name",
47+
"something.unwanted"
48+
)
49+
screenOrientation {
50+
enabled(false)
51+
}
52+
}
53+
},
4554
)
4655
ViewClickInstrumentation().install(this, rum!!)
4756
Log.d(TAG, "RUM session started: " + rum?.sessionProvider?.getSessionId())

0 commit comments

Comments
 (0)