Skip to content

Change ConfigProvider to iterate from lowest to highest precedence so… #9307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void run() throws InterruptedException {
}

private static Object getLogInjectionEnabled() {
ConfigSetting configSetting = ConfigCollector.get().collect().get(LOGS_INJECTION_ENABLED);
ConfigSetting configSetting =
ConfigCollector.get().getAppliedConfigSetting(LOGS_INJECTION_ENABLED);
if (configSetting == null) {
return null;
}
Expand Down
50 changes: 33 additions & 17 deletions internal-api/src/main/java/datadog/trace/api/ConfigCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,58 @@ public class ConfigCollector {
private static final AtomicReferenceFieldUpdater<ConfigCollector, Map> COLLECTED_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(ConfigCollector.class, Map.class, "collected");

private volatile Map<String, ConfigSetting> collected = new ConcurrentHashMap<>();
private volatile Map<String, Map<ConfigOrigin, ConfigSetting>> collected =
new ConcurrentHashMap<>();

public static ConfigCollector get() {
return INSTANCE;
}

/**
* Records the latest ConfigSetting for the given key and origin, replacing any previous value for
* that (key, origin) pair.
*/
public void put(String key, Object value, ConfigOrigin origin) {
ConfigSetting setting = ConfigSetting.of(key, value, origin);
collected.put(key, setting);
Map<ConfigOrigin, ConfigSetting> originMap =
collected.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
originMap.put(origin, setting); // replaces any previous value for this origin
}

public void put(String key, Object value, ConfigOrigin origin, int seqId) {
ConfigSetting setting = ConfigSetting.of(key, value, origin, seqId);
Map<ConfigOrigin, ConfigSetting> originMap =
collected.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
originMap.put(origin, setting); // replaces any previous value for this origin
}

// TODO: Does this need a counterpart with seqId?
public void putAll(Map<String, Object> keysAndValues, ConfigOrigin origin) {
// attempt merge+replace to avoid collector seeing partial update
Map<String, ConfigSetting> merged =
new ConcurrentHashMap<>(keysAndValues.size() + collected.size());
for (Map.Entry<String, Object> entry : keysAndValues.entrySet()) {
ConfigSetting setting = ConfigSetting.of(entry.getKey(), entry.getValue(), origin);
merged.put(entry.getKey(), setting);
}
while (true) {
Map<String, ConfigSetting> current = collected;
current.forEach(merged::putIfAbsent);
if (COLLECTED_UPDATER.compareAndSet(this, current, merged)) {
break; // success
}
// roll back to original update before next attempt
merged.keySet().retainAll(keysAndValues.keySet());
put(entry.getKey(), entry.getValue(), origin);
}
}

@SuppressWarnings("unchecked")
public Map<String, ConfigSetting> collect() {
public Map<String, Map<ConfigOrigin, ConfigSetting>> collect() {
if (!collected.isEmpty()) {
return COLLECTED_UPDATER.getAndSet(this, new ConcurrentHashMap<>());
} else {
return Collections.emptyMap();
}
}

/**
* Returns the {@link ConfigSetting} with the highest seq_id for the given key, or {@code null} if
* no setting exists for that key.
*/
public ConfigSetting getHighestSeqIdConfig(String key) {
Map<ConfigOrigin, ConfigSetting> originMap = collected.get(key);
if (originMap == null || originMap.isEmpty()) {
return null;
}
return originMap.values().stream()
.max(java.util.Comparator.comparingInt(setting -> setting.seqId))
.orElse(null);
}
}
21 changes: 17 additions & 4 deletions internal-api/src/main/java/datadog/trace/api/ConfigSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ public final class ConfigSetting {
public final String key;
public final Object value;
public final ConfigOrigin origin;
public final int seqId;

public static final int DEFAULT_SEQ_ID = 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hard to name -- it's actually the seq_id to represent the "default" configorigin, not the seq ID to be used in absence of a seqId.


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

public static ConfigSetting of(String key, Object value, ConfigOrigin origin) {
return new ConfigSetting(key, value, origin);
return new ConfigSetting(key, value, origin, 0);
}

public static ConfigSetting of(String key, Object value, ConfigOrigin origin, int seqId) {
return new ConfigSetting(key, value, origin, seqId);
}

private ConfigSetting(String key, Object value, ConfigOrigin origin) {
private ConfigSetting(String key, Object value, ConfigOrigin origin, int seqId) {
this.key = key;
this.value = CONFIG_FILTER_LIST.contains(key) ? "<hidden>" : value;
this.origin = origin;
this.seqId = seqId;
}

public String normalizedKey() {
Expand Down Expand Up @@ -99,12 +107,15 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConfigSetting that = (ConfigSetting) o;
return key.equals(that.key) && Objects.equals(value, that.value) && origin == that.origin;
return key.equals(that.key)
&& Objects.equals(value, that.value)
&& origin == that.origin
&& seqId == that.seqId;
}

@Override
public int hashCode() {
return Objects.hash(key, value, origin);
return Objects.hash(key, value, origin, seqId);
}

@Override
Expand All @@ -117,6 +128,8 @@ public String toString() {
+ stringValue()
+ ", origin="
+ origin
+ ", seqId="
+ seqId
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ public CapturedEnvironmentConfigSource(CapturedEnvironment env) {
}

@Override
protected String get(String key) {
return env.getProperties().get(key);
protected String get(String key) throws ConfigSourceException {
Object value = env.getProperties().get(key);
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConfigSourceException(value);
}
return (String) value;
}

@Override
Expand Down
Loading
Loading