-
Notifications
You must be signed in to change notification settings - Fork 317
Implement Config Inversion with Default Strictness of Warning
#9539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7a74abc
Refactored EnvironmentVariables to be testable.
AlexeyKuznetsov-DD cda6f30
adding supported-configurations.json file
mhlidd 6ba7dc0
removing extra supported-configurations.json
mhlidd e5333f1
migrating config-utils tests and ConfigInversionMetric telemetry
mhlidd c4b7853
config inversion init
mhlidd ab185c3
migrating config-utils tests
mhlidd 0a529b0
undo move of test files that rely on inject*config
mhlidd 640d674
adding deprecation handling
mhlidd 802812c
updating tests
mhlidd c0c3a47
spotless
mhlidd d5e8b9a
responding to PR comments
mhlidd a4f33a0
updating class coverage exclude
mhlidd 50bf05f
updating ConfigHelper to be a singleton
mhlidd 68fe4e3
refactoring ConfigHelper and ConfigurationSources to simplify code re…
mhlidd 11256b1
responding to PR comments and refactoring ConfigHelperTest to utilize…
mhlidd f823554
updating PR comments
mhlidd ebabc9a
cleanup and code coverage
mhlidd 3023190
bugfix
mhlidd abe8786
responding to PR comments
mhlidd a257e6c
fixing issue with EmptyMap
mhlidd 7a2f0c5
manually handling rebase issues
mhlidd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
utils/config-utils/src/main/java/datadog/trace/config/inversion/ConfigHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package datadog.trace.config.inversion; | ||
|
|
||
| import datadog.environment.EnvironmentVariables; | ||
| import datadog.trace.api.telemetry.ConfigInversionMetricCollectorProvider; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class ConfigHelper { | ||
|
|
||
| /** Config Inversion strictness policy for enforcement of undocumented environment variables */ | ||
| public enum StrictnessPolicy { | ||
| STRICT, | ||
| WARNING, | ||
| TEST; | ||
|
|
||
| private String displayName; | ||
|
|
||
| StrictnessPolicy() { | ||
| this.displayName = name().toLowerCase(Locale.ROOT); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (displayName == null) { | ||
| displayName = name().toLowerCase(Locale.ROOT); | ||
| } | ||
| return displayName; | ||
| } | ||
| } | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(ConfigHelper.class); | ||
|
|
||
| private static final ConfigHelper INSTANCE = new ConfigHelper(); | ||
|
|
||
| private StrictnessPolicy configInversionStrict = StrictnessPolicy.WARNING; | ||
|
|
||
| private static final String DD_PREFIX = "DD_"; | ||
| private static final String OTEL_PREFIX = "OTEL_"; | ||
|
|
||
| // Cache for configs, init value is EmptyMap | ||
| private Map<String, String> configs = Collections.emptyMap(); | ||
|
|
||
| // Default to production source | ||
| private SupportedConfigurationSource configSource = new SupportedConfigurationSource(); | ||
|
|
||
| public static ConfigHelper get() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| public void setConfigInversionStrict(StrictnessPolicy configInversionStrict) { | ||
| this.configInversionStrict = configInversionStrict; | ||
| } | ||
|
|
||
| public StrictnessPolicy configInversionStrictFlag() { | ||
| return configInversionStrict; | ||
| } | ||
|
|
||
| // Used only for testing purposes | ||
| void setConfigurationSource(SupportedConfigurationSource testSource) { | ||
| configSource = testSource; | ||
| } | ||
|
|
||
| /** Resetting config cache. Useful for cleaning up after tests. */ | ||
| void resetCache() { | ||
| configs = Collections.emptyMap(); | ||
| } | ||
|
|
||
| /** Reset all configuration data to the generated defaults. Useful for cleaning up after tests. */ | ||
| void resetToDefaults() { | ||
| configSource = new SupportedConfigurationSource(); | ||
| this.configInversionStrict = StrictnessPolicy.WARNING; | ||
| resetCache(); | ||
| } | ||
|
|
||
| public Map<String, String> getEnvironmentVariables() { | ||
| if (!configs.isEmpty()) { | ||
| return configs; | ||
| } | ||
|
|
||
| // Initial value is EmptyMap | ||
| configs = new HashMap<>(); | ||
|
|
||
| Map<String, String> env = EnvironmentVariables.getAll(); | ||
| for (Map.Entry<String, String> entry : env.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
| String primaryEnv = configSource.primaryEnvFromAlias(key); | ||
| if (key.startsWith(DD_PREFIX) || key.startsWith(OTEL_PREFIX) || primaryEnv != null) { | ||
| if (configSource.supported(key)) { | ||
| configs.put(key, value); | ||
| // If this environment variable is the alias of another, and we haven't processed the | ||
| // original environment variable yet, handle it here. | ||
| } else if (primaryEnv != null && !configs.containsKey(primaryEnv)) { | ||
| List<String> aliases = configSource.getAliases(primaryEnv); | ||
| for (String alias : aliases) { | ||
| if (env.containsKey(alias)) { | ||
| configs.put(primaryEnv, env.get(alias)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| String envFromDeprecated = configSource.primaryEnvFromDeprecated(key); | ||
| if (envFromDeprecated != null) { | ||
| String warning = | ||
| "Environment variable " | ||
| + key | ||
| + " is deprecated. Please use " | ||
| + (primaryEnv != null ? primaryEnv : envFromDeprecated) | ||
| + " instead."; | ||
| log.warn(warning); | ||
| } | ||
| } else { | ||
| configs.put(key, value); | ||
| } | ||
| } | ||
| return configs; | ||
| } | ||
|
|
||
| public String getEnvironmentVariable(String name) { | ||
| if (configs.containsKey(name)) { | ||
| return configs.get(name); | ||
| } | ||
|
|
||
| if ((name.startsWith(DD_PREFIX) || name.startsWith(OTEL_PREFIX)) | ||
| && configSource.primaryEnvFromAlias(name) == null | ||
| && !configSource.supported(name)) { | ||
| if (configInversionStrict != StrictnessPolicy.TEST) { | ||
| ConfigInversionMetricCollectorProvider.get().setUndocumentedEnvVarMetric(name); | ||
| } | ||
|
|
||
| if (configInversionStrict == StrictnessPolicy.STRICT) { | ||
| return null; // If strict mode is enabled, return null for unsupported configs | ||
| } | ||
| } | ||
|
|
||
| String config = EnvironmentVariables.get(name); | ||
| List<String> aliases; | ||
| if (config == null && (aliases = configSource.getAliases(name)) != null) { | ||
| for (String alias : aliases) { | ||
| String aliasValue = EnvironmentVariables.get(alias); | ||
| if (aliasValue != null) { | ||
| return aliasValue; | ||
| } | ||
| } | ||
| } | ||
| return config; | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
...nfig-utils/src/main/java/datadog/trace/config/inversion/SupportedConfigurationSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package datadog.trace.config.inversion; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * This class uses {@link #GeneratedSupportedConfigurations} for handling supported configurations | ||
| * for Config Inversion Can be extended for testing with custom configuration data. | ||
| */ | ||
| class SupportedConfigurationSource { | ||
|
|
||
| /** @return Set of supported environment variable keys */ | ||
| public boolean supported(String env) { | ||
| return GeneratedSupportedConfigurations.SUPPORTED.contains(env); | ||
| } | ||
|
|
||
| /** @return List of aliases for an environment variable */ | ||
| public List<String> getAliases(String env) { | ||
mhlidd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return GeneratedSupportedConfigurations.ALIASES.getOrDefault(env, Collections.emptyList()); | ||
| } | ||
|
|
||
| /** @return Primary environment variable for a queried alias */ | ||
| public String primaryEnvFromAlias(String alias) { | ||
| return GeneratedSupportedConfigurations.ALIAS_MAPPING.getOrDefault(alias, null); | ||
| } | ||
|
|
||
| /** @return Map of deprecated configurations */ | ||
| public String primaryEnvFromDeprecated(String deprecated) { | ||
| return GeneratedSupportedConfigurations.DEPRECATED.getOrDefault(deprecated, null); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.