Skip to content

Commit 3ac0405

Browse files
committed
Report ConfigSettings with seq_id in ConfigProvider and ConfigCollector
1 parent 48e3af7 commit 3ac0405

File tree

3 files changed

+72
-38
lines changed

3 files changed

+72
-38
lines changed

internal-api/src/main/java/datadog/trace/api/ConfigCollector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public void put(String key, Object value, ConfigOrigin origin) {
2727
collected.put(key, setting);
2828
}
2929

30+
public void put(String key, Object value, ConfigOrigin origin, int seqId) {
31+
ConfigSetting setting = ConfigSetting.of(key, value, origin, seqId);
32+
collected.put(key, setting);
33+
}
34+
3035
public void putAll(Map<String, Object> keysAndValues, ConfigOrigin origin) {
3136
// attempt merge+replace to avoid collector seeing partial update
3237
Map<String, ConfigSetting> merged =

internal-api/src/main/java/datadog/trace/api/ConfigSetting.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public final class ConfigSetting {
1313
public final ConfigOrigin origin;
1414
public final int seqId;
1515

16+
public static final int DEFAULT_SEQ_ID = 1;
17+
1618
private static final Set<String> CONFIG_FILTER_LIST =
1719
new HashSet<>(
1820
Arrays.asList("DD_API_KEY", "dd.api-key", "dd.profiling.api-key", "dd.profiling.apikey"));

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

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import datadog.environment.SystemProperties;
66
import datadog.trace.api.ConfigCollector;
77
import datadog.trace.api.ConfigOrigin;
8+
import datadog.trace.api.ConfigSetting;
89
import de.thetaphi.forbiddenapis.SuppressForbidden;
910
import java.io.File;
1011
import java.io.FileNotFoundException;
@@ -85,6 +86,7 @@ public String getString(String key) {
8586
return getString(key, null);
8687
}
8788

89+
// TODO: Change this logic too
8890
public <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T defaultValue) {
8991
String value = getString(key);
9092
if (null != value) {
@@ -94,24 +96,22 @@ public <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T defaultVal
9496
log.debug("failed to parse {} for {}, defaulting to {}", value, key, defaultValue);
9597
}
9698
}
97-
if (collectConfig) {
98-
String valueStr = defaultValue == null ? null : defaultValue.name();
99-
ConfigCollector.get().put(key, valueStr, ConfigOrigin.DEFAULT);
100-
}
10199
return defaultValue;
102100
}
103101

104102
public String getString(String key, String defaultValue, String... aliases) {
103+
int seqId = ConfigSetting.DEFAULT_SEQ_ID;
105104
if (collectConfig) {
106-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
105+
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId);
107106
}
108107
String value = null;
109108
for (ConfigProvider.Source source : sources) {
109+
seqId++;
110110
String tmp = source.get(key, aliases);
111111
if (tmp != null) {
112112
value = tmp;
113113
if (collectConfig) {
114-
ConfigCollector.get().put(key, value, source.origin());
114+
ConfigCollector.get().put(key, value, source.origin(), seqId);
115115
}
116116
}
117117
}
@@ -123,22 +123,21 @@ public String getString(String key, String defaultValue, String... aliases) {
123123
* an empty or blank string.
124124
*/
125125
public String getStringNotEmpty(String key, String defaultValue, String... aliases) {
126+
int seqId = ConfigSetting.DEFAULT_SEQ_ID;
126127
if (collectConfig) {
127-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
128+
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId);
128129
}
129130
String value = null;
130131
for (ConfigProvider.Source source : sources) {
132+
seqId++;
131133
String tmp = source.get(key, aliases);
132134
if (tmp != null && !tmp.trim().isEmpty()) {
133135
value = tmp;
134136
if (collectConfig) {
135-
ConfigCollector.get().put(key, value, source.origin());
137+
ConfigCollector.get().put(key, value, source.origin(), seqId);
136138
}
137139
}
138140
}
139-
if (collectConfig) {
140-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
141-
}
142141
return value != null ? value : defaultValue;
143142
}
144143

@@ -147,26 +146,24 @@ public String getStringExcludingSource(
147146
String defaultValue,
148147
Class<? extends ConfigProvider.Source> excludedSource,
149148
String... aliases) {
149+
int seqId = ConfigSetting.DEFAULT_SEQ_ID;
150150
if (collectConfig) {
151-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
151+
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId);
152152
}
153153
String value = null;
154154
for (ConfigProvider.Source source : sources) {
155155
if (excludedSource.isAssignableFrom(source.getClass())) {
156156
continue;
157157
}
158-
158+
seqId++;
159159
String tmp = source.get(key, aliases);
160160
if (tmp != null) {
161161
value = tmp;
162162
if (collectConfig) {
163-
ConfigCollector.get().put(key, value, source.origin());
163+
ConfigCollector.get().put(key, value, source.origin(), seqId);
164164
}
165165
}
166166
}
167-
if (collectConfig) {
168-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
169-
}
170167
return value != null ? value : defaultValue;
171168
}
172169

@@ -228,27 +225,26 @@ public double getDouble(String key, double defaultValue) {
228225
}
229226

230227
private <T> T get(String key, T defaultValue, Class<T> type, String... aliases) {
228+
int seqId = ConfigSetting.DEFAULT_SEQ_ID;
231229
if (collectConfig) {
232-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
230+
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId);
233231
}
234232
T value = null;
235233
for (ConfigProvider.Source source : sources) {
236234
try {
235+
seqId++;
237236
String sourceValue = source.get(key, aliases);
238237
T tmp = ConfigConverter.valueOf(sourceValue, type);
239238
if (tmp != null) {
240239
value = tmp;
241240
if (collectConfig) {
242-
ConfigCollector.get().put(key, sourceValue, source.origin());
241+
ConfigCollector.get().put(key, sourceValue, source.origin(), seqId);
243242
}
244243
}
245244
} catch (NumberFormatException ex) {
246245
// continue
247246
}
248247
}
249-
if (collectConfig) {
250-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
251-
}
252248
return value != null ? value : defaultValue;
253249
}
254250

@@ -257,8 +253,10 @@ public List<String> getList(String key) {
257253
}
258254

259255
public List<String> getList(String key, List<String> defaultValue) {
256+
// Do we need this, if getString will already report the default?
260257
if (collectConfig) {
261-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
258+
ConfigCollector.get()
259+
.put(key, defaultValue, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID);
262260
}
263261
String list = getString(key);
264262
if (null == list) {
@@ -269,8 +267,10 @@ public List<String> getList(String key, List<String> defaultValue) {
269267
}
270268

271269
public Set<String> getSet(String key, Set<String> defaultValue) {
270+
// Do we need this, if getString will already report the default?
272271
if (collectConfig) {
273-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
272+
ConfigCollector.get()
273+
.put(key, defaultValue, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID);
274274
}
275275
String list = getString(key);
276276
if (null == list) {
@@ -286,7 +286,8 @@ public List<String> getSpacedList(String key) {
286286

287287
public Map<String, String> getMergedMap(String key, String... aliases) {
288288
Map<String, String> merged = new HashMap<>();
289-
ConfigOrigin origin = ConfigOrigin.DEFAULT;
289+
ConfigOrigin origin = null;
290+
int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1;
290291
// System properties take precedence over env
291292
// prior art:
292293
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
@@ -297,21 +298,28 @@ public Map<String, String> getMergedMap(String key, String... aliases) {
297298
if (!parsedMap.isEmpty()) {
298299
origin = source.origin();
299300
if (collectConfig) {
300-
ConfigCollector.get().put(key, parsedMap, origin);
301+
ConfigCollector.get().put(key, parsedMap, origin, seqId);
301302
}
302303
}
303304
merged.putAll(parsedMap);
305+
seqId++;
304306
}
305307
if (collectConfig) {
306-
// TO DISCUSS: But if multiple sources have been set, origin isn't exactly accurate here...?
307-
ConfigCollector.get().put(key, merged, origin);
308+
if (origin != null) {
309+
// TO DISCUSS: But if multiple sources have been set, origin isn't exactly accurate here...?
310+
ConfigCollector.get().put(key, merged, origin, seqId);
311+
} else {
312+
ConfigCollector.get()
313+
.put(key, new HashMap<>(), ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID);
314+
}
308315
}
309316
return merged;
310317
}
311318

312319
public Map<String, String> getMergedTagsMap(String key, String... aliases) {
313320
Map<String, String> merged = new HashMap<>();
314-
ConfigOrigin origin = ConfigOrigin.DEFAULT;
321+
ConfigOrigin origin = null;
322+
int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1;
315323
// System properties take precedence over env
316324
// prior art:
317325
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
@@ -323,20 +331,26 @@ public Map<String, String> getMergedTagsMap(String key, String... aliases) {
323331
if (!parsedMap.isEmpty()) {
324332
origin = source.origin();
325333
if (collectConfig) {
326-
ConfigCollector.get().put(key, parsedMap, origin);
334+
ConfigCollector.get().put(key, parsedMap, origin, seqId);
327335
}
328336
}
329337
merged.putAll(parsedMap);
338+
seqId++;
330339
}
331340
if (collectConfig) {
332-
ConfigCollector.get().put(key, merged, origin);
341+
if (origin != null) {
342+
ConfigCollector.get().put(key, merged, origin, seqId);
343+
} else {
344+
ConfigCollector.get().put(key, merged, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID);
345+
}
333346
}
334347
return merged;
335348
}
336349

337350
public Map<String, String> getOrderedMap(String key) {
338351
LinkedHashMap<String, String> merged = new LinkedHashMap<>();
339-
ConfigOrigin origin = ConfigOrigin.DEFAULT;
352+
ConfigOrigin origin = null;
353+
int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1;
340354
// System properties take precedence over env
341355
// prior art:
342356
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
@@ -347,21 +361,27 @@ public Map<String, String> getOrderedMap(String key) {
347361
if (!parsedMap.isEmpty()) {
348362
origin = source.origin();
349363
if (collectConfig) {
350-
ConfigCollector.get().put(key, parsedMap, origin);
364+
ConfigCollector.get().put(key, parsedMap, origin, seqId);
351365
}
352366
}
353367
merged.putAll(parsedMap);
368+
seqId++;
354369
}
355370
if (collectConfig) {
356-
ConfigCollector.get().put(key, merged, origin);
371+
if (origin != null) {
372+
ConfigCollector.get().put(key, merged, origin, seqId);
373+
} else {
374+
ConfigCollector.get().put(key, merged, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID);
375+
}
357376
}
358377
return merged;
359378
}
360379

361380
public Map<String, String> getMergedMapWithOptionalMappings(
362381
String defaultPrefix, boolean lowercaseKeys, String... keys) {
363382
Map<String, String> merged = new HashMap<>();
364-
ConfigOrigin origin = ConfigOrigin.DEFAULT;
383+
ConfigOrigin origin = null;
384+
int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1;
365385
// System properties take precedence over env
366386
// prior art:
367387
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
@@ -374,13 +394,19 @@ public Map<String, String> getMergedMapWithOptionalMappings(
374394
if (!parsedMap.isEmpty()) {
375395
origin = source.origin();
376396
if (collectConfig) {
377-
ConfigCollector.get().put(key, parsedMap, origin);
397+
ConfigCollector.get().put(key, parsedMap, origin, seqId);
378398
}
379399
}
380400
merged.putAll(parsedMap);
401+
seqId++;
381402
}
382403
if (collectConfig) {
383-
ConfigCollector.get().put(key, merged, origin);
404+
if (origin != null) {
405+
ConfigCollector.get().put(key, merged, origin, seqId);
406+
} else {
407+
ConfigCollector.get()
408+
.put(key, merged, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID);
409+
}
384410
}
385411
}
386412
return merged;
@@ -396,7 +422,8 @@ public BitSet getIntegerRange(final String key, final BitSet defaultValue, Strin
396422
log.warn("Invalid configuration for {}", key, e);
397423
}
398424
if (collectConfig) {
399-
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
425+
ConfigCollector.get()
426+
.put(key, defaultValue, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID);
400427
}
401428
return defaultValue;
402429
}

0 commit comments

Comments
 (0)