22
33import datadog .environment .EnvironmentVariables ;
44import datadog .trace .api .telemetry .ConfigInversionMetricCollectorProvider ;
5- import java .util .HashMap ;
5+ import java .util .Collections ;
66import java .util .List ;
77import java .util .Locale ;
88import java .util .Map ;
@@ -38,8 +38,11 @@ public String toString() {
3838
3939 private StrictnessPolicy configInversionStrict = StrictnessPolicy .WARNING ;
4040
41+ private static final String DD_PREFIX = "DD_" ;
42+ private static final String OTEL_PREFIX = "OTEL_" ;
43+
4144 // Cache for configs, init value is null
42- private Map <String , String > configs ;
45+ private Map <String , String > configs = Collections . emptyMap () ;
4346
4447 // Default to production source
4548 private SupportedConfigurationSource configSource = new SupportedConfigurationSource ();
@@ -70,25 +73,25 @@ void resetCache() {
7073 void resetToDefaults () {
7174 configSource = new SupportedConfigurationSource ();
7275 this .configInversionStrict = StrictnessPolicy .WARNING ;
76+ configs = Collections .emptyMap ();
7377 }
7478
7579 public Map <String , String > getEnvironmentVariables () {
76- if (configs != null ) {
80+ if (! configs . isEmpty () ) {
7781 return configs ;
7882 }
7983
80- configs = new HashMap <>();
8184 Map <String , String > env = EnvironmentVariables .getAll ();
8285 for (Map .Entry <String , String > entry : env .entrySet ()) {
8386 String key = entry .getKey ();
8487 String value = entry .getValue ();
8588 String primaryEnv = configSource .primaryEnvFromAlias (key );
86- if (key .startsWith ("DD_" ) || key .startsWith ("OTEL_" ) || null != primaryEnv ) {
89+ if (key .startsWith (DD_PREFIX ) || key .startsWith (OTEL_PREFIX ) || primaryEnv != null ) {
8790 if (configSource .supported (key )) {
8891 configs .put (key , value );
8992 // If this environment variable is the alias of another, and we haven't processed the
9093 // original environment variable yet, handle it here.
91- } else if (null != primaryEnv && !configs .containsKey (primaryEnv )) {
94+ } else if (primaryEnv != null && !configs .containsKey (primaryEnv )) {
9295 List <String > aliases = configSource .getAliases (primaryEnv );
9396 for (String alias : aliases ) {
9497 if (env .containsKey (alias )) {
@@ -97,8 +100,8 @@ public Map<String, String> getEnvironmentVariables() {
97100 }
98101 }
99102 }
100- String envFromDeprecated ;
101- if (( envFromDeprecated = configSource . primaryEnvFromDeprecated ( key )) != null ) {
103+ String envFromDeprecated = configSource . primaryEnvFromDeprecated ( key ) ;
104+ if (envFromDeprecated != null ) {
102105 String warning =
103106 "Environment variable "
104107 + key
@@ -115,12 +118,12 @@ public Map<String, String> getEnvironmentVariables() {
115118 }
116119
117120 public String getEnvironmentVariable (String name ) {
118- if (configs != null && configs .containsKey (name )) {
121+ if (configs .containsKey (name )) {
119122 return configs .get (name );
120123 }
121124
122- if ((name .startsWith ("DD_" ) || name .startsWith ("OTEL_" ))
123- && null == configSource .primaryEnvFromAlias (name )
125+ if ((name .startsWith (DD_PREFIX ) || name .startsWith (OTEL_PREFIX ))
126+ && configSource .primaryEnvFromAlias (name ) == null
124127 && !configSource .supported (name )) {
125128 if (configInversionStrict != StrictnessPolicy .TEST ) {
126129 ConfigInversionMetricCollectorProvider .get ().setUndocumentedEnvVarMetric (name );
0 commit comments