Alter instrumentation suppression behavior (#11640)

This commit is contained in:
Lauri Tulmin 2024-07-10 06:28:51 +03:00 committed by GitHub
parent 7016470b2e
commit ec91735598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 20 deletions

View File

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

View File

@ -11,20 +11,14 @@ public final class AgentConfig {
public static boolean isInstrumentationEnabled(
ConfigProperties config, Iterable<String> instrumentationNames, boolean defaultEnabled) {
// If default is enabled, we want to enable individually,
// if default is disabled, we want to disable individually.
boolean anyEnabled = defaultEnabled;
for (String name : instrumentationNames) {
String propertyName = "otel.instrumentation." + name + ".enabled";
boolean enabled = config.getBoolean(propertyName, defaultEnabled);
if (defaultEnabled) {
anyEnabled &= enabled;
} else {
anyEnabled |= enabled;
Boolean enabled = config.getBoolean(propertyName);
if (enabled != null) {
return enabled;
}
}
return anyEnabled;
return defaultEnabled;
}
public static boolean isDebugModeEnabled(ConfigProperties config) {

View File

@ -25,16 +25,14 @@ class AgentConfigTest {
@ArgumentsSource(InstrumentationEnabledParams.class)
void testIsInstrumentationEnabled(
@SuppressWarnings("unused") String description,
boolean firstEnabled,
boolean secondEnabled,
Boolean firstEnabled,
Boolean secondEnabled,
boolean defaultEnabled,
boolean expected) {
ConfigProperties config = mock(ConfigProperties.class);
when(config.getBoolean("otel.instrumentation.first.enabled", defaultEnabled))
.thenReturn(firstEnabled);
when(config.getBoolean("otel.instrumentation.second.enabled", defaultEnabled))
.thenReturn(secondEnabled);
when(config.getBoolean("otel.instrumentation.first.enabled")).thenReturn(firstEnabled);
when(config.getBoolean("otel.instrumentation.second.enabled")).thenReturn(secondEnabled);
assertEquals(
expected,
@ -49,13 +47,41 @@ class AgentConfigTest {
return Stream.of(
Arguments.of(
"enabled by default, both instrumentations are off", false, false, true, false),
Arguments.of("enabled by default, one instrumentation is on", true, false, true, false),
Arguments.of("enabled by default, first instrumentation is on", true, null, true, true),
Arguments.of("enabled by default, second instrumentation is on", null, true, true, true),
Arguments.of("enabled by default, both instrumentations are on", true, true, true, true),
Arguments.of(
"disabled by default, both instrumentations are off", false, false, false, false),
Arguments.of("disabled by default, one instrumentation is on", true, false, false, true),
"enabled by default, first instrumentation is off, second is on",
false,
true,
true,
false),
Arguments.of(
"disabled by default, both instrumentation are on", true, true, false, true));
"enabled by default, first instrumentation is on, second is off",
true,
false,
true,
true),
Arguments.of("enabled by default", null, null, true, true),
Arguments.of(
"disabled by default, both instrumentations are off", false, false, false, false),
Arguments.of("disabled by default, first instrumentation is on", true, null, false, true),
Arguments.of(
"disabled by default, second instrumentation is on", null, true, false, true),
Arguments.of("disabled by default, both instrumentation are on", true, true, false, true),
Arguments.of(
"disabled by default, first instrumentation is off, second is on",
false,
true,
false,
false),
Arguments.of(
"disabled by default, first instrumentation is on, second is off",
true,
false,
false,
true),
Arguments.of("disabled by default", null, null, false, false));
}
}
}