Skip to content

Commit

Permalink
Support DD_TRACE_<INTEGRATION>_ENABLED (#7718)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoffl01 authored Oct 17, 2024
1 parent 84d0670 commit 666f2af
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,35 @@ public boolean isIntegrationsEnabled() {
return integrationsEnabled;
}

/**
* isIntegrationEnabled determines whether an integration under the specified name(s) is enabled
* according to the following list of configurations, from highest to lowest precedence:
* trace.name.enabled, trace.integration.name.enabled, integration.name.enabled. If none of these
* configurations is set, the defaultEnabled value is used. All system properties take precedence
* over all env vars.
*
* @param integrationNames the name(s) that represent(s) the integration
* @param defaultEnabled true if enabled by default, else false
* @return boolean on whether the integration is enabled
*/
public boolean isIntegrationEnabled(
final Iterable<String> integrationNames, final boolean defaultEnabled) {
return configProvider.isEnabled(integrationNames, "integration.", ".enabled", defaultEnabled);
// If default is enabled, we want to disable individually.
// If default is disabled, we want to enable individually.
boolean anyEnabled = defaultEnabled;
for (final String name : integrationNames) {
final String primaryKey = "trace." + name + ".enabled";
final String[] aliases = {
"trace.integration." + name + ".enabled", "integration." + name + ".enabled"
}; // listed in order of precedence
final boolean configEnabled = configProvider.getBoolean(primaryKey, defaultEnabled, aliases);
if (defaultEnabled) {
anyEnabled &= configEnabled;
} else {
anyEnabled |= configEnabled;
}
}
return anyEnabled;
}

public boolean isIntegrationShortcutMatchingEnabled(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class InstrumenterConfigTest extends DDSpecification {
setup:
environmentVariables.set("DD_INTEGRATION_ORDER_ENABLED", "false")
environmentVariables.set("DD_INTEGRATION_TEST_ENV_ENABLED", "true")
environmentVariables.set("DD_TRACE_NEW_ENV_ENABLED", "false")
environmentVariables.set("DD_INTEGRATION_DISABLED_ENV_ENABLED", "false")

System.setProperty("dd.integration.order.enabled", "true")
Expand All @@ -16,6 +17,7 @@ class InstrumenterConfigTest extends DDSpecification {

environmentVariables.set("DD_INTEGRATION_ORDER_MATCHING_SHORTCUT_ENABLED", "false")
environmentVariables.set("DD_INTEGRATION_TEST_ENV_MATCHING_SHORTCUT_ENABLED", "true")
environmentVariables.set("DD_INTEGRATION_NEW_ENV_MATCHING_SHORTCUT_ENABLED", "false")
environmentVariables.set("DD_INTEGRATION_DISABLED_ENV_MATCHING_SHORTCUT_ENABLED", "false")

System.setProperty("dd.integration.order.matching.shortcut.enabled", "true")
Expand Down Expand Up @@ -44,11 +46,72 @@ class InstrumenterConfigTest extends DDSpecification {
["disabled-env", "test-env"] | false | true
["test-prop", "disabled-prop"] | true | false
["disabled-env", "test-env"] | true | false
["new-env"] | true | false
// spotless:on

integrationNames = new TreeSet<>(names)
}

def setEnv(String key, String value) {
environmentVariables.set(key, value)
}

def setSysProp(String key, String value) {
System.setProperty(key, value)
}

def randomIntegrationEnabled() {
return InstrumenterConfig.get().isIntegrationEnabled(["random"], true)
}

def "verify integration enabled hierarchy"() {
when:
// the below should have no effect
setEnv("DD_RANDOM_ENABLED", "false")
setSysProp("dd.random.enabled", "false")

then:
randomIntegrationEnabled() == true

when:
setEnv("DD_INTEGRATION_RANDOM_ENABLED", "false")

then:
randomIntegrationEnabled() == false

when:
setEnv("DD_TRACE_INTEGRATION_RANDOM_ENABLED", "true")

then:
randomIntegrationEnabled() == true

when:
setEnv("DD_TRACE_RANDOM_ENABLED", "false")

then:
randomIntegrationEnabled() == false

// assert all system properties take precedence over all env vars
when:
setSysProp("dd.integration.random.enabled", "true")

then:
randomIntegrationEnabled() == true

when:
setSysProp("dd.trace.integration.random.enabled", "false")

then:
randomIntegrationEnabled() == false

when:
setSysProp("dd.trace.random.enabled", "true")

then:
randomIntegrationEnabled() == true

}

def "valid resolver presets"() {
setup:
injectSysConfig("resolver.cache.config", preset)
Expand Down

0 comments on commit 666f2af

Please sign in to comment.