Skip to content

Commit f459280

Browse files
committed
Apply logic to other getString* methods and get method
1 parent 0ea5afb commit f459280

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,31 @@ public String getString(String key, String defaultValue, String... aliases) {
9797
* an empty or blank string.
9898
*/
9999
public String getStringNotEmpty(String key, String defaultValue, String... aliases) {
100+
String foundValue = null;
100101
for (ConfigProvider.Source source : sources) {
101102
String value = source.get(key, aliases);
103+
// TODO: Is a source still configured "set" if it's empty, though?
102104
if (value != null && !value.trim().isEmpty()) {
103105
if (collectConfig) {
104106
ConfigCollector.get().put(key, value, source.origin());
105107
}
106-
return value;
108+
if (foundValue == null) {
109+
foundValue = value;
110+
}
107111
}
108112
}
109113
if (collectConfig) {
110114
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
111115
}
112-
return defaultValue;
116+
return foundValue != null ? foundValue : defaultValue;
113117
}
114118

115119
public String getStringExcludingSource(
116120
String key,
117121
String defaultValue,
118122
Class<? extends ConfigProvider.Source> excludedSource,
119123
String... aliases) {
124+
String foundValue = null;
120125
for (ConfigProvider.Source source : sources) {
121126
if (excludedSource.isAssignableFrom(source.getClass())) {
122127
continue;
@@ -127,13 +132,15 @@ public String getStringExcludingSource(
127132
if (collectConfig) {
128133
ConfigCollector.get().put(key, value, source.origin());
129134
}
130-
return value;
135+
if (foundValue == null) {
136+
foundValue = value;
137+
}
131138
}
132139
}
133140
if (collectConfig) {
134141
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
135142
}
136-
return defaultValue;
143+
return foundValue != null ? foundValue : defaultValue;
137144
}
138145

139146
public boolean isSet(String key) {
@@ -194,6 +201,7 @@ public double getDouble(String key, double defaultValue) {
194201
}
195202

196203
private <T> T get(String key, T defaultValue, Class<T> type, String... aliases) {
204+
T foundValue = null;
197205
for (ConfigProvider.Source source : sources) {
198206
try {
199207
String sourceValue = source.get(key, aliases);
@@ -202,7 +210,9 @@ private <T> T get(String key, T defaultValue, Class<T> type, String... aliases)
202210
if (collectConfig) {
203211
ConfigCollector.get().put(key, sourceValue, source.origin());
204212
}
205-
return value;
213+
if (foundValue == null) {
214+
foundValue = value;
215+
}
206216
}
207217
} catch (NumberFormatException ex) {
208218
// continue
@@ -211,7 +221,7 @@ private <T> T get(String key, T defaultValue, Class<T> type, String... aliases)
211221
if (collectConfig) {
212222
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
213223
}
214-
return defaultValue;
224+
return foundValue != null ? foundValue : defaultValue;
215225
}
216226

217227
public List<String> getList(String key) {

0 commit comments

Comments
 (0)