Skip to content

Report config_id for Hands Off Config files #9299

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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 @@ -27,6 +27,11 @@ public void put(String key, Object value, ConfigOrigin origin) {
collected.put(key, setting);
}

public void put(String key, Object value, ConfigOrigin origin, String configId) {
ConfigSetting setting = ConfigSetting.of(key, value, origin, configId);
collected.put(key, setting);
}

public void putAll(Map<String, Object> keysAndValues, ConfigOrigin origin) {
// attempt merge+replace to avoid collector seeing partial update
Map<String, ConfigSetting> merged =
Expand Down
20 changes: 16 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,26 @@ public final class ConfigSetting {
public final String key;
public final Object value;
public final ConfigOrigin origin;
/** The config ID associated with this setting, or {@code null} if not applicable. */
public final String configId;

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, null);
}

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

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

public String normalizedKey() {
Expand Down Expand Up @@ -99,12 +106,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
&& Objects.equals(configId, that.configId);
}

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

@Override
Expand All @@ -117,6 +127,8 @@ public String toString() {
+ stringValue()
+ ", origin="
+ origin
+ ", configId="
+ configId
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String getString(String key, String defaultValue, String... aliases) {
String value = source.get(key, aliases);
if (value != null) {
if (collectConfig) {
ConfigCollector.get().put(key, value, source.origin());
ConfigCollector.get().put(key, value, source.origin(), getConfigIdFromSource(source));
}
return value;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ private <T> T get(String key, T defaultValue, Class<T> type, String... aliases)
T value = ConfigConverter.valueOf(sourceValue, type);
if (value != null) {
if (collectConfig) {
ConfigCollector.get().put(key, sourceValue, source.origin());
ConfigCollector.get().put(key, value, source.origin(), getConfigIdFromSource(source));
}
return value;
}
Expand Down Expand Up @@ -247,6 +247,7 @@ public List<String> getSpacedList(String key) {
public Map<String, String> getMergedMap(String key, String... aliases) {
Map<String, String> merged = new HashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
String configId = null;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
Expand All @@ -256,18 +257,20 @@ public Map<String, String> getMergedMap(String key, String... aliases) {
Map<String, String> parsedMap = ConfigConverter.parseMap(value, key);
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
configId = getConfigIdFromSource(sources[i]);
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
ConfigCollector.get().put(key, merged, origin, configId);
}
return merged;
}

public Map<String, String> getMergedTagsMap(String key, String... aliases) {
Map<String, String> merged = new HashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
String configId = null;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
Expand All @@ -278,18 +281,20 @@ public Map<String, String> getMergedTagsMap(String key, String... aliases) {
ConfigConverter.parseTraceTagsMap(value, ':', Arrays.asList(',', ' '));
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
configId = getConfigIdFromSource(sources[i]);
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
ConfigCollector.get().put(key, merged, origin, configId);
}
return merged;
}

public Map<String, String> getOrderedMap(String key) {
LinkedHashMap<String, String> merged = new LinkedHashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
String configId = null;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
Expand All @@ -299,11 +304,12 @@ public Map<String, String> getOrderedMap(String key) {
Map<String, String> parsedMap = ConfigConverter.parseOrderedMap(value, key);
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
configId = getConfigIdFromSource(sources[i]);
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
ConfigCollector.get().put(key, merged, origin, configId);
}
return merged;
}
Expand All @@ -312,6 +318,7 @@ public Map<String, String> getMergedMapWithOptionalMappings(
String defaultPrefix, boolean lowercaseKeys, String... keys) {
Map<String, String> merged = new HashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
String configId = null;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
Expand All @@ -323,11 +330,12 @@ public Map<String, String> getMergedMapWithOptionalMappings(
ConfigConverter.parseMapWithOptionalMappings(value, key, defaultPrefix, lowercaseKeys);
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
configId = getConfigIdFromSource(sources[i]);
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
ConfigCollector.get().put(key, merged, origin, configId);
}
}
return merged;
Expand Down Expand Up @@ -499,6 +507,13 @@ private static Properties loadConfigurationFile(ConfigProvider configProvider) {
return properties;
}

private static String getConfigIdFromSource(Source source) {
if (source instanceof StableConfigSource) {
return ((StableConfigSource) source).getConfigId();
}
return null;
}

public abstract static class Source {
public final String get(String key, String... aliases) {
String value = get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class StableConfig {

public StableConfig(Object yaml) {
Map<Object, Object> map = (Map<Object, Object>) yaml;
this.configId = String.valueOf(map.get("config_id"));
this.configId = map.get("config_id") == null ? null : String.valueOf(map.get("config_id"));
this.apmConfigurationDefault =
unmodifiableMap(
(Map<String, Object>) map.getOrDefault("apm_configuration_default", emptyMap()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ public void writeConfiguration(ConfigSetting configSetting) throws IOException {
bodyWriter.name("value").value(configSetting.stringValue());
bodyWriter.setSerializeNulls(false);
bodyWriter.name("origin").value(configSetting.origin.value);
if (configSetting.configId != null) {
bodyWriter.name("config_id").value(configSetting.configId);
}
bodyWriter.endObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class EventSourceTest extends DDSpecification{

where:
eventType | eventQueueName | eventInstance
"Config Change" | "configChangeQueue" | new ConfigSetting("key", "value", ConfigOrigin.ENV)
"Config Change" | "configChangeQueue" | ConfigSetting.of("key", "value", ConfigOrigin.ENV)
"Integration" | "integrationQueue" | new Integration("name", true)
"Dependency" | "dependencyQueue" | new Dependency("name", "version", "type", null)
"Metric" | "metricQueue" | new Metric()
Expand Down
Loading