Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import datadog.environment.EnvironmentVariables;
import datadog.trace.api.telemetry.ConfigInversionMetricCollectorProvider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,7 +18,7 @@ private ConfigHelper() {}

/** Config Inversion strictness policy for enforcement of undocumented environment variables */
public enum StrictnessPolicy {
STRICT,
STRICT_TEST,
WARNING,
TEST;

Expand Down Expand Up @@ -49,6 +52,9 @@ public String toString() {
// Default to production source
private SupportedConfigurationSource configSource = new SupportedConfigurationSource();

// Collects unsupported config keys encountered in STRICT_TEST mode
private final Set<String> unsupportedConfigs = ConcurrentHashMap.newKeySet();

public static ConfigHelper get() {
return INSTANCE;
}
Expand All @@ -75,9 +81,18 @@ void resetCache() {
void resetToDefaults() {
configSource = new SupportedConfigurationSource();
this.configInversionStrict = StrictnessPolicy.WARNING;
unsupportedConfigs.clear();
resetCache();
}

/** Returns and clears the set of unsupported config keys encountered in STRICT_TEST mode. */
public List<String> drainUnsupportedConfigs() {
List<String> result = new ArrayList<>(unsupportedConfigs);
unsupportedConfigs.clear();
Collections.sort(result);
return result;
}

public static Map<String, String> env() {
return get().getEnvironmentVariables();
}
Expand Down Expand Up @@ -142,8 +157,13 @@ public String getEnvironmentVariable(String name) {
ConfigInversionMetricCollectorProvider.get().setUndocumentedEnvVarMetric(name);
}

if (configInversionStrict == StrictnessPolicy.STRICT) {
return null; // If strict mode is enabled, return null for unsupported configs
if (configInversionStrict == StrictnessPolicy.STRICT_TEST) {
unsupportedConfigs.add(name);
throw new IllegalArgumentException(
"Unsupported configuration: "
+ name
+ " is not in GeneratedSupportedConfigurations. "
+ "Add it to metadata/supported-configurations.json or remove the usage.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void setUp() {
testSupported, testAliases, testAliasMapping, new HashMap<>());
ConfigHelper.get().setConfigurationSource(testSource);
strictness = ConfigHelper.get().configInversionStrictFlag();
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT);
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT_TEST);
}

@AfterAll
Expand Down Expand Up @@ -177,7 +177,7 @@ void testUnsupportedEnvWarningNotInTestMode() {
assertEquals("banana", ConfigHelper.env("DD_FAKE_VAR"));

// Cleanup
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT);
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT_TEST);
}

@Test
Expand Down
Loading