-
Notifications
You must be signed in to change notification settings - Fork 312
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
Open
mhlidd
wants to merge
13
commits into
mhlidd/migrate_config-utils_tests
Choose a base branch
from
mhlidd/config_inversion_base
base: mhlidd/migrate_config-utils_tests
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+450
−2
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
23a4b9e
migrating config-utils tests and ConfigInversionMetric telemetry
mhlidd eeca333
config inversion init
mhlidd f45bf87
migrating config-utils tests
mhlidd 1bb6fd6
undo move of test files that rely on inject*config
mhlidd f76270a
adding deprecation handling
mhlidd 022f028
updating tests
mhlidd 795cec5
spotless
mhlidd 54ed8b3
excluding json from shadowjar
mhlidd aacdcd6
attempting to fix published_artifacts job
mhlidd eeee8b2
updating gradle files
mhlidd 7897c56
responding to PR comments
mhlidd bb4f47c
updating class coverage exclude
mhlidd 74cc4d1
updating ConfigHelper to be a singleton
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
File renamed without changes.
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
149 changes: 149 additions & 0 deletions
149
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,149 @@ | ||
package datadog.trace.config.inversion; | ||
|
||
import datadog.environment.EnvironmentVariables; | ||
import datadog.trace.api.telemetry.ConfigInversionMetricCollectorProvider; | ||
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; | ||
|
||
// Cache for configs, init value is null | ||
private Map<String, String> configs; | ||
|
||
// 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 = null; | ||
} | ||
|
||
/** Reset all configuration data to the generated defaults. Useful for cleaning up after tests. */ | ||
void resetToDefaults() { | ||
configSource = new SupportedConfigurationSource(); | ||
this.configInversionStrict = StrictnessPolicy.WARNING; | ||
} | ||
|
||
public Map<String, String> getEnvironmentVariables() { | ||
if (configs != null) { | ||
return configs; | ||
} | ||
|
||
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(); | ||
Map<String, String> aliasMapping = configSource.getAliasMapping(); | ||
if (key.startsWith("DD_") || key.startsWith("OTEL_") || aliasMapping.containsKey(key)) { | ||
String baseConfig; | ||
if (configSource.getSupportedConfigurations().contains(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 (aliasMapping.containsKey(key) | ||
&& !configs.containsKey(baseConfig = aliasMapping.get(key))) { | ||
List<String> aliasList = configSource.getAliases().get(baseConfig); | ||
for (String alias : aliasList) { | ||
if (env.containsKey(alias)) { | ||
configs.put(baseConfig, env.get(alias)); | ||
break; | ||
} | ||
} | ||
} | ||
// TODO: Follow-up - Add deprecation handling | ||
if (configSource.getDeprecatedConfigurations().containsKey(key)) { | ||
String warning = | ||
"Environment variable " | ||
+ key | ||
+ " is deprecated. " | ||
+ (configSource.getAliasMapping().containsKey(key) | ||
? "Please use " + configSource.getAliasMapping().get(key) + " instead." | ||
: configSource.getDeprecatedConfigurations().get(key)); | ||
log.warn(warning); | ||
} | ||
} else { | ||
configs.put(key, value); | ||
} | ||
} | ||
return configs; | ||
} | ||
|
||
public String getEnvironmentVariable(String name) { | ||
if (configs != null && configs.containsKey(name)) { | ||
return configs.get(name); | ||
} | ||
|
||
if ((name.startsWith("DD_") || name.startsWith("OTEL_")) | ||
&& !configSource.getAliasMapping().containsKey(name) | ||
&& !configSource.getSupportedConfigurations().contains(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().get(name)) != null) { | ||
for (String alias : aliases) { | ||
String aliasValue = EnvironmentVariables.get(alias); | ||
if (aliasValue != null) { | ||
return aliasValue; | ||
} | ||
} | ||
} | ||
return config; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...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,32 @@ | ||
package datadog.trace.config.inversion; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* 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 configuration keys */ | ||
public Set<String> getSupportedConfigurations() { | ||
return GeneratedSupportedConfigurations.SUPPORTED; | ||
} | ||
|
||
/** @return Map of configuration keys to their aliases */ | ||
public Map<String, List<String>> getAliases() { | ||
return GeneratedSupportedConfigurations.ALIASES; | ||
} | ||
|
||
/** @return Map of alias keys to their primary configuration keys */ | ||
public Map<String, String> getAliasMapping() { | ||
return GeneratedSupportedConfigurations.ALIAS_MAPPING; | ||
} | ||
|
||
/** @return Map of deprecated configurations */ | ||
public Map<String, String> getDeprecatedConfigurations() { | ||
return GeneratedSupportedConfigurations.DEPRECATED; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 suggestion: Helper class usually are
final
and have aprivate
constructor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also wondering if this could be made into a singleton as well... and if so there would be no reason to have static methods under it. Not sure if that is the right or wrong direction to go in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, an helper class does not have a state. So if you have a state but only one instance, that would be a singleton.
Here might be a big ambiguous because most of the state is immutable, right? It's more like a big static data table and calling the helper methods won't mutate it. But as you have the StrictStyle thing that can change the behavior, I would with a singleton.
Keep in mind helper class / methods should be easier to test than singleton that usually are painful to deal with on test scenario.