Skip to content

Commit 31e2345

Browse files
committed
Introduce seqID field to ConfigSettings and have ConfigProvider set this in getString method
1 parent 844db66 commit 31e2345

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public void put(String key, Object value, ConfigOrigin origin) {
3434
originMap.put(origin, setting); // replaces any previous value for this origin
3535
}
3636

37+
// TODO: Replace old `put` with this `put`
38+
public void put(String key, Object value, ConfigOrigin origin, int seqId) {
39+
ConfigSetting setting = ConfigSetting.of(key, value, origin, seqId);
40+
Map<ConfigOrigin, ConfigSetting> originMap =
41+
collected.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
42+
originMap.put(origin, setting); // replaces any previous value for this origin
43+
}
44+
3745
public void putAll(Map<String, Object> keysAndValues, ConfigOrigin origin) {
3846
for (Map.Entry<String, Object> entry : keysAndValues.entrySet()) {
3947
put(entry.getKey(), entry.getValue(), origin);

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@ public final class ConfigSetting {
1111
public final String key;
1212
public final Object value;
1313
public final ConfigOrigin origin;
14+
public final int seqID;
1415

1516
private static final Set<String> CONFIG_FILTER_LIST =
1617
new HashSet<>(
1718
Arrays.asList("DD_API_KEY", "dd.api-key", "dd.profiling.api-key", "dd.profiling.apikey"));
1819

1920
public static ConfigSetting of(String key, Object value, ConfigOrigin origin) {
20-
return new ConfigSetting(key, value, origin);
21+
return new ConfigSetting(key, value, origin, 0);
2122
}
2223

23-
private ConfigSetting(String key, Object value, ConfigOrigin origin) {
24+
public static ConfigSetting of(String key, Object value, ConfigOrigin origin, int seqID) {
25+
return new ConfigSetting(key, value, origin, seqID);
26+
}
27+
28+
private ConfigSetting(String key, Object value, ConfigOrigin origin, int seqID) {
2429
this.key = key;
2530
this.value = CONFIG_FILTER_LIST.contains(key) ? "<hidden>" : value;
2631
this.origin = origin;
32+
this.seqID = seqID;
2733
}
2834

2935
public String normalizedKey() {
@@ -99,7 +105,10 @@ public boolean equals(Object o) {
99105
if (this == o) return true;
100106
if (o == null || getClass() != o.getClass()) return false;
101107
ConfigSetting that = (ConfigSetting) o;
102-
return key.equals(that.key) && Objects.equals(value, that.value) && origin == that.origin;
108+
return key.equals(that.key)
109+
&& Objects.equals(value, that.value)
110+
&& origin == that.origin
111+
&& seqID == that.seqID;
103112
}
104113

105114
@Override
@@ -117,6 +126,8 @@ public String toString() {
117126
+ stringValue()
118127
+ ", origin="
119128
+ origin
129+
+ ", seq_id="
130+
+ seqID
120131
+ '}';
121132
}
122133
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ private static final class Singleton {
3232

3333
private final ConfigProvider.Source[] sources;
3434

35+
private final int numSources;
36+
3537
private ConfigProvider(ConfigProvider.Source... sources) {
3638
this(true, sources);
3739
}
3840

3941
private ConfigProvider(boolean collectConfig, ConfigProvider.Source... sources) {
4042
this.collectConfig = collectConfig;
4143
this.sources = sources;
44+
this.numSources = sources.length;
4245
}
4346

4447
public String getConfigFileStatus() {
@@ -75,11 +78,18 @@ public <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T defaultVal
7578

7679
public String getString(String key, String defaultValue, String... aliases) {
7780
String foundValue = null;
81+
int ctr = numSources + 1;
7882
for (ConfigProvider.Source source : sources) {
7983
String value = source.get(key, aliases);
8084
if (value != null) {
8185
if (collectConfig) {
82-
ConfigCollector.get().put(key, value, source.origin());
86+
if (ctr <= 1) {
87+
// log a developer error? Report some log to telemetry for developer use?
88+
ConfigCollector.get().put(key, value, source.origin()); // report without seq_id
89+
} else {
90+
ctr--;
91+
ConfigCollector.get().put(key, value, source.origin(), ctr);
92+
}
8393
}
8494
if (foundValue == null) {
8595
foundValue = value;

internal-api/src/test/groovy/datadog/trace/api/ConfigCollectorTest.groovy

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import static datadog.trace.api.ConfigDefaults.DEFAULT_IAST_WEAK_HASH_ALGORITHMS
1616
import static datadog.trace.api.ConfigDefaults.DEFAULT_TELEMETRY_HEARTBEAT_INTERVAL
1717

1818
class ConfigCollectorTest extends DDSpecification {
19-
2019
def "non-default config settings get collected"() {
2120
setup:
2221
injectEnvConfig(Strings.toEnvVar(configKey), configValue)
@@ -83,6 +82,7 @@ class ConfigCollectorTest extends DDSpecification {
8382
setting != null
8483
setting.stringValue() == expectedValue
8584
setting.origin == ConfigOrigin.JVM_PROP
85+
// TODO: Add check for env origin as well
8686

8787
where:
8888
configKey | configValue1 | configValue2 | expectedValue
@@ -248,4 +248,25 @@ class ConfigCollectorTest extends DDSpecification {
248248
"logs.injection.enabled" | "false"
249249
"trace.sample.rate" | "0.3"
250250
}
251+
252+
def "seqID increases with precedence"() {
253+
setup:
254+
def configKey = IastConfig.IAST_TELEMETRY_VERBOSITY
255+
def envValue = Verbosity.DEBUG.toString()
256+
def sysValue = Verbosity.MANDATORY.toString()
257+
injectEnvConfig(Strings.toEnvVar(configKey), envValue)
258+
injectSysConfig(configKey, sysValue)
259+
260+
expect:
261+
def configsByOrigin = ConfigCollector.get().collect().get(configKey)
262+
configsByOrigin != null
263+
def sysSetting = configsByOrigin.get(ConfigOrigin.JVM_PROP)
264+
sysSetting != null
265+
def envSetting = configsByOrigin.get(ConfigOrigin.ENV)
266+
envSetting != null
267+
def defaultSetting = configsByOrigin.get(ConfigOrigin.DEFAULT)
268+
defaultSetting != null
269+
sysSetting.seqID > envSetting.seqID
270+
envSetting.seqID > defaultSetting.seqID
271+
}
251272
}

0 commit comments

Comments
 (0)