Skip to content

Commit ec91735

Browse files
authored
Alter instrumentation suppression behavior (#11640)
1 parent 7016470 commit ec91735

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/extension/instrumentation/InstrumentationModule.java

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public abstract class InstrumentationModule implements Ordered {
4949
* InstrumentationModule} must have a default constructor (for SPI), so they have to pass the
5050
* instrumentation names to the super class constructor.
5151
*
52+
* <p>When enabling or disabling the instrumentation module configuration property that
53+
* corresponds to the main instrumentation name is considered first, after that additional
54+
* instrumentation names are considered in the order they are listed here.
55+
*
5256
* <p>The instrumentation names should follow several rules:
5357
*
5458
* <ul>

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/AgentConfig.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,14 @@ public final class AgentConfig {
1111

1212
public static boolean isInstrumentationEnabled(
1313
ConfigProperties config, Iterable<String> instrumentationNames, boolean defaultEnabled) {
14-
// If default is enabled, we want to enable individually,
15-
// if default is disabled, we want to disable individually.
16-
boolean anyEnabled = defaultEnabled;
1714
for (String name : instrumentationNames) {
1815
String propertyName = "otel.instrumentation." + name + ".enabled";
19-
boolean enabled = config.getBoolean(propertyName, defaultEnabled);
20-
21-
if (defaultEnabled) {
22-
anyEnabled &= enabled;
23-
} else {
24-
anyEnabled |= enabled;
16+
Boolean enabled = config.getBoolean(propertyName);
17+
if (enabled != null) {
18+
return enabled;
2519
}
2620
}
27-
return anyEnabled;
21+
return defaultEnabled;
2822
}
2923

3024
public static boolean isDebugModeEnabled(ConfigProperties config) {

javaagent-tooling/src/test/java/io/opentelemetry/javaagent/tooling/config/AgentConfigTest.java

+35-9
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ class AgentConfigTest {
2525
@ArgumentsSource(InstrumentationEnabledParams.class)
2626
void testIsInstrumentationEnabled(
2727
@SuppressWarnings("unused") String description,
28-
boolean firstEnabled,
29-
boolean secondEnabled,
28+
Boolean firstEnabled,
29+
Boolean secondEnabled,
3030
boolean defaultEnabled,
3131
boolean expected) {
3232

3333
ConfigProperties config = mock(ConfigProperties.class);
34-
when(config.getBoolean("otel.instrumentation.first.enabled", defaultEnabled))
35-
.thenReturn(firstEnabled);
36-
when(config.getBoolean("otel.instrumentation.second.enabled", defaultEnabled))
37-
.thenReturn(secondEnabled);
34+
when(config.getBoolean("otel.instrumentation.first.enabled")).thenReturn(firstEnabled);
35+
when(config.getBoolean("otel.instrumentation.second.enabled")).thenReturn(secondEnabled);
3836

3937
assertEquals(
4038
expected,
@@ -49,13 +47,41 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
4947
return Stream.of(
5048
Arguments.of(
5149
"enabled by default, both instrumentations are off", false, false, true, false),
52-
Arguments.of("enabled by default, one instrumentation is on", true, false, true, false),
50+
Arguments.of("enabled by default, first instrumentation is on", true, null, true, true),
51+
Arguments.of("enabled by default, second instrumentation is on", null, true, true, true),
5352
Arguments.of("enabled by default, both instrumentations are on", true, true, true, true),
53+
Arguments.of(
54+
"enabled by default, first instrumentation is off, second is on",
55+
false,
56+
true,
57+
true,
58+
false),
59+
Arguments.of(
60+
"enabled by default, first instrumentation is on, second is off",
61+
true,
62+
false,
63+
true,
64+
true),
65+
Arguments.of("enabled by default", null, null, true, true),
5466
Arguments.of(
5567
"disabled by default, both instrumentations are off", false, false, false, false),
56-
Arguments.of("disabled by default, one instrumentation is on", true, false, false, true),
68+
Arguments.of("disabled by default, first instrumentation is on", true, null, false, true),
69+
Arguments.of(
70+
"disabled by default, second instrumentation is on", null, true, false, true),
71+
Arguments.of("disabled by default, both instrumentation are on", true, true, false, true),
72+
Arguments.of(
73+
"disabled by default, first instrumentation is off, second is on",
74+
false,
75+
true,
76+
false,
77+
false),
5778
Arguments.of(
58-
"disabled by default, both instrumentation are on", true, true, false, true));
79+
"disabled by default, first instrumentation is on, second is off",
80+
true,
81+
false,
82+
false,
83+
true),
84+
Arguments.of("disabled by default", null, null, false, false));
5985
}
6086
}
6187
}

0 commit comments

Comments
 (0)