Skip to content

Commit 7db5a70

Browse files
committed
ConfigProvider iterates over all sources and reports all non-null values to ConfigCollector
1 parent e8faa94 commit 7db5a70

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigProvider.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.IOException;
1313
import java.util.Arrays;
1414
import java.util.BitSet;
15+
import java.util.Collections;
1516
import java.util.HashMap;
1617
import java.util.HashSet;
1718
import java.util.LinkedHashMap;
@@ -75,47 +76,55 @@ public <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T defaultVal
7576
}
7677

7778
public String getString(String key, String defaultValue, String... aliases) {
79+
String foundValue = null;
7880
for (ConfigProvider.Source source : sources) {
7981
String value = source.get(key, aliases);
8082
if (value != null) {
8183
if (collectConfig) {
8284
ConfigCollector.get().put(key, value, source.origin());
8385
}
84-
return value;
86+
if (foundValue == null) {
87+
foundValue = value;
88+
}
8589
}
8690
}
8791
if (collectConfig) {
8892
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
8993
}
90-
return defaultValue;
94+
return foundValue != null ? foundValue : defaultValue;
9195
}
9296

9397
/**
9498
* Like {@link #getString(String, String, String...)} but falls back to next source if a value is
9599
* an empty or blank string.
96100
*/
97101
public String getStringNotEmpty(String key, String defaultValue, String... aliases) {
102+
String foundValue = null;
98103
for (ConfigProvider.Source source : sources) {
99104
String value = source.get(key, aliases);
100105
if (value != null && !value.trim().isEmpty()) {
101106
if (collectConfig) {
102107
ConfigCollector.get().put(key, value, source.origin());
103108
}
104-
return value;
109+
if (foundValue == null) {
110+
foundValue = value;
111+
}
105112
}
106113
}
107114
if (collectConfig) {
108115
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
109116
}
110-
return defaultValue;
117+
return foundValue != null ? foundValue : defaultValue;
111118
}
112119

113120
public String getStringExcludingSource(
114121
String key,
115122
String defaultValue,
116123
Class<? extends ConfigProvider.Source> excludedSource,
117124
String... aliases) {
125+
String foundValue = null;
118126
for (ConfigProvider.Source source : sources) {
127+
// Do we still want to report telemetry in this case?
119128
if (excludedSource.isAssignableFrom(source.getClass())) {
120129
continue;
121130
}
@@ -125,13 +134,15 @@ public String getStringExcludingSource(
125134
if (collectConfig) {
126135
ConfigCollector.get().put(key, value, source.origin());
127136
}
128-
return value;
137+
if (foundValue == null) {
138+
foundValue = value;
139+
}
129140
}
130141
}
131142
if (collectConfig) {
132143
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
133144
}
134-
return defaultValue;
145+
return foundValue != null ? foundValue : defaultValue;
135146
}
136147

137148
public boolean isSet(String key) {
@@ -192,6 +203,7 @@ public double getDouble(String key, double defaultValue) {
192203
}
193204

194205
private <T> T get(String key, T defaultValue, Class<T> type, String... aliases) {
206+
T foundValue = null;
195207
for (ConfigProvider.Source source : sources) {
196208
try {
197209
String sourceValue = source.get(key, aliases);
@@ -200,7 +212,9 @@ private <T> T get(String key, T defaultValue, Class<T> type, String... aliases)
200212
if (collectConfig) {
201213
ConfigCollector.get().put(key, sourceValue, source.origin());
202214
}
203-
return value;
215+
if (foundValue == null) {
216+
foundValue = value;
217+
}
204218
}
205219
} catch (NumberFormatException ex) {
206220
// continue
@@ -209,7 +223,7 @@ private <T> T get(String key, T defaultValue, Class<T> type, String... aliases)
209223
if (collectConfig) {
210224
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
211225
}
212-
return defaultValue;
226+
return foundValue != null ? foundValue : defaultValue;
213227
}
214228

215229
public List<String> getList(String key) {
@@ -256,11 +270,14 @@ public Map<String, String> getMergedMap(String key, String... aliases) {
256270
Map<String, String> parsedMap = ConfigConverter.parseMap(value, key);
257271
if (!parsedMap.isEmpty()) {
258272
origin = sources[i].origin();
273+
if (collectConfig) {
274+
ConfigCollector.get().put(key, parsedMap, origin);
275+
}
259276
}
260277
merged.putAll(parsedMap);
261278
}
262-
if (collectConfig) {
263-
ConfigCollector.get().put(key, merged, origin);
279+
if (collectConfig && merged.isEmpty()) {
280+
ConfigCollector.get().put(key, merged, ConfigOrigin.DEFAULT);
264281
}
265282
return merged;
266283
}
@@ -278,11 +295,14 @@ public Map<String, String> getMergedTagsMap(String key, String... aliases) {
278295
ConfigConverter.parseTraceTagsMap(value, ':', Arrays.asList(',', ' '));
279296
if (!parsedMap.isEmpty()) {
280297
origin = sources[i].origin();
298+
if (collectConfig) {
299+
ConfigCollector.get().put(key, parsedMap, origin);
300+
}
281301
}
282302
merged.putAll(parsedMap);
283303
}
284-
if (collectConfig) {
285-
ConfigCollector.get().put(key, merged, origin);
304+
if (collectConfig && merged.isEmpty()) {
305+
ConfigCollector.get().put(key, merged, ConfigOrigin.DEFAULT);
286306
}
287307
return merged;
288308
}
@@ -299,11 +319,14 @@ public Map<String, String> getOrderedMap(String key) {
299319
Map<String, String> parsedMap = ConfigConverter.parseOrderedMap(value, key);
300320
if (!parsedMap.isEmpty()) {
301321
origin = sources[i].origin();
322+
if (collectConfig) {
323+
ConfigCollector.get().put(key, parsedMap, origin);
324+
}
302325
}
303326
merged.putAll(parsedMap);
304327
}
305-
if (collectConfig) {
306-
ConfigCollector.get().put(key, merged, origin);
328+
if (collectConfig && merged.isEmpty()) {
329+
ConfigCollector.get().put(key, merged, ConfigOrigin.DEFAULT);
307330
}
308331
return merged;
309332
}
@@ -323,11 +346,14 @@ public Map<String, String> getMergedMapWithOptionalMappings(
323346
ConfigConverter.parseMapWithOptionalMappings(value, key, defaultPrefix, lowercaseKeys);
324347
if (!parsedMap.isEmpty()) {
325348
origin = sources[i].origin();
349+
if (collectConfig) {
350+
ConfigCollector.get().put(key, parsedMap, origin);
351+
}
326352
}
327353
merged.putAll(parsedMap);
328354
}
329-
if (collectConfig) {
330-
ConfigCollector.get().put(key, merged, origin);
355+
if (collectConfig && merged.isEmpty()) {
356+
ConfigCollector.get().put(key, Collections.emptyMap(), ConfigOrigin.DEFAULT);
331357
}
332358
}
333359
return merged;

0 commit comments

Comments
 (0)