diff --git a/dd-smoke-tests/log-injection/src/main/java/datadog/smoketest/loginjection/BaseApplication.java b/dd-smoke-tests/log-injection/src/main/java/datadog/smoketest/loginjection/BaseApplication.java index a2edae29ac3..74068c5fd06 100644 --- a/dd-smoke-tests/log-injection/src/main/java/datadog/smoketest/loginjection/BaseApplication.java +++ b/dd-smoke-tests/log-injection/src/main/java/datadog/smoketest/loginjection/BaseApplication.java @@ -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; } diff --git a/internal-api/src/main/java/datadog/trace/api/ConfigCollector.java b/internal-api/src/main/java/datadog/trace/api/ConfigCollector.java index f4cfda6877b..bf7d44b21d2 100644 --- a/internal-api/src/main/java/datadog/trace/api/ConfigCollector.java +++ b/internal-api/src/main/java/datadog/trace/api/ConfigCollector.java @@ -16,42 +16,58 @@ public class ConfigCollector { private static final AtomicReferenceFieldUpdater COLLECTED_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ConfigCollector.class, Map.class, "collected"); - private volatile Map collected = new ConcurrentHashMap<>(); + private volatile Map> 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 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 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 keysAndValues, ConfigOrigin origin) { - // attempt merge+replace to avoid collector seeing partial update - Map merged = - new ConcurrentHashMap<>(keysAndValues.size() + collected.size()); for (Map.Entry entry : keysAndValues.entrySet()) { - ConfigSetting setting = ConfigSetting.of(entry.getKey(), entry.getValue(), origin); - merged.put(entry.getKey(), setting); - } - while (true) { - Map 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 collect() { + public Map> 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 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); + } } diff --git a/internal-api/src/main/java/datadog/trace/api/ConfigSetting.java b/internal-api/src/main/java/datadog/trace/api/ConfigSetting.java index b688d5b477d..fd41acb6a7e 100644 --- a/internal-api/src/main/java/datadog/trace/api/ConfigSetting.java +++ b/internal-api/src/main/java/datadog/trace/api/ConfigSetting.java @@ -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; private static final Set 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) ? "" : value; this.origin = origin; + this.seqId = seqId; } public String normalizedKey() { @@ -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 @@ -117,6 +128,8 @@ public String toString() { + stringValue() + ", origin=" + origin + + ", seqId=" + + seqId + '}'; } } diff --git a/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/CapturedEnvironmentConfigSource.java b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/CapturedEnvironmentConfigSource.java index 56bfb0ad547..6ddaff87f96 100644 --- a/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/CapturedEnvironmentConfigSource.java +++ b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/CapturedEnvironmentConfigSource.java @@ -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 diff --git a/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigProvider.java b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigProvider.java index 1edeb072c80..a211a80914f 100644 --- a/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigProvider.java +++ b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigProvider.java @@ -5,6 +5,7 @@ import datadog.environment.SystemProperties; import datadog.trace.api.ConfigCollector; import datadog.trace.api.ConfigOrigin; +import datadog.trace.api.ConfigSetting; import de.thetaphi.forbiddenapis.SuppressForbidden; import java.io.File; import java.io.FileNotFoundException; @@ -12,11 +13,13 @@ import java.io.IOException; import java.util.Arrays; import java.util.BitSet; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.Set; import org.slf4j.Logger; @@ -42,6 +45,32 @@ private ConfigProvider(boolean collectConfig, ConfigProvider.Source... sources) this.sources = sources; } + /** + * Creates a ConfigProvider with sources ordered from lowest to highest precedence. Internally + * reverses the array to support the new approach of iterating from lowest to highest precedence, + * enabling reporting of all configured sources to telemetry (not just the highest-precedence + * match). + * + * @param sources the configuration sources, in order from lowest to highest precedence + * @return a ConfigProvider with sources in precedence order (highest first) + */ + public static ConfigProvider createWithPrecedenceOrder(Source... sources) { + Source[] reversed = Arrays.copyOf(sources, sources.length); + Collections.reverse(Arrays.asList(reversed)); + return new ConfigProvider(reversed); + } + + /** + * Same as {@link #createWithPrecedenceOrder(Source...)} but allows specifying the collectConfig + * flag. + */ + public static ConfigProvider createWithPrecedenceOrder(boolean collectConfig, Source... sources) { + Source[] reversed = Arrays.copyOf(sources, sources.length); + Collections.reverse(Arrays.asList(reversed)); + return new ConfigProvider(collectConfig, reversed); + } + + // TODO: Handle this special case public String getConfigFileStatus() { for (ConfigProvider.Source source : sources) { if (source instanceof PropertiesConfigSource) { @@ -58,6 +87,7 @@ public String getString(String key) { return getString(key, null); } + // TODO: Change this logic too public > T getEnum(String key, Class enumType, T defaultValue) { String value = getString(key); if (null != value) { @@ -67,27 +97,38 @@ public > T getEnum(String key, Class enumType, T defaultVal log.debug("failed to parse {} for {}, defaulting to {}", value, key, defaultValue); } } - if (collectConfig) { - String valueStr = defaultValue == null ? null : defaultValue.name(); - ConfigCollector.get().put(key, valueStr, ConfigOrigin.DEFAULT); - } return defaultValue; } public String getString(String key, String defaultValue, String... aliases) { + int seqId = ConfigSetting.DEFAULT_SEQ_ID; + ConfigOrigin usedOrigin = null; + String value = null; + if (collectConfig) { + ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId); + } for (ConfigProvider.Source source : sources) { - String value = source.get(key, aliases); - if (value != null) { + seqId++; + try { + String tmp = source.get(key, aliases); + if (tmp != null) { + value = tmp; + usedOrigin = source.origin(); + if (collectConfig) { + ConfigCollector.get().put(key, tmp, source.origin(), seqId); + } + } + } catch (ConfigSourceException e) { if (collectConfig) { - ConfigCollector.get().put(key, value, source.origin()); + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); } - return value; } } - if (collectConfig) { - ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT); + // Re-report the used value to ensure it has the highest seqId + if (collectConfig && value != null && usedOrigin != null) { + ConfigCollector.get().put(key, value, usedOrigin, seqId + 1); } - return defaultValue; + return value != null ? value : defaultValue; } /** @@ -95,19 +136,34 @@ public String getString(String key, String defaultValue, String... aliases) { * an empty or blank string. */ public String getStringNotEmpty(String key, String defaultValue, String... aliases) { + int seqId = ConfigSetting.DEFAULT_SEQ_ID; + ConfigOrigin usedOrigin = null; + String value = null; + if (collectConfig) { + ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId); + } for (ConfigProvider.Source source : sources) { - String value = source.get(key, aliases); - if (value != null && !value.trim().isEmpty()) { + seqId++; + try { + String tmp = source.get(key, aliases); + if (tmp != null && !tmp.trim().isEmpty()) { + value = tmp; + usedOrigin = source.origin(); + if (collectConfig) { + ConfigCollector.get().put(key, tmp, source.origin(), seqId); + } + } + } catch (ConfigSourceException e) { if (collectConfig) { - ConfigCollector.get().put(key, value, source.origin()); + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); } - return value; } } - if (collectConfig) { - ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT); + // Re-report the used value to ensure it has the highest seqId + if (collectConfig && value != null && usedOrigin != null) { + ConfigCollector.get().put(key, value, usedOrigin, seqId + 1); } - return defaultValue; + return value != null ? value : defaultValue; } public String getStringExcludingSource( @@ -115,23 +171,37 @@ public String getStringExcludingSource( String defaultValue, Class excludedSource, String... aliases) { + int seqId = ConfigSetting.DEFAULT_SEQ_ID; + ConfigOrigin usedOrigin = null; + String value = null; + if (collectConfig) { + ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId); + } for (ConfigProvider.Source source : sources) { if (excludedSource.isAssignableFrom(source.getClass())) { continue; } - - String value = source.get(key, aliases); - if (value != null) { + seqId++; + try { + String tmp = source.get(key, aliases); + if (tmp != null) { + value = tmp; + usedOrigin = source.origin(); + if (collectConfig) { + ConfigCollector.get().put(key, tmp, source.origin(), seqId); + } + } + } catch (ConfigSourceException e) { if (collectConfig) { - ConfigCollector.get().put(key, value, source.origin()); + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); } - return value; } } - if (collectConfig) { - ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT); + // Re-report the used value to ensure it has the highest seqId + if (collectConfig && value != null && usedOrigin != null) { + ConfigCollector.get().put(key, value, usedOrigin, seqId + 1); } - return defaultValue; + return value != null ? value : defaultValue; } public boolean isSet(String key) { @@ -192,24 +262,42 @@ public double getDouble(String key, double defaultValue) { } private T get(String key, T defaultValue, Class type, String... aliases) { + int seqId = ConfigSetting.DEFAULT_SEQ_ID; + ConfigOrigin usedOrigin = null; + String usedSourceValue = null; + T value = null; + if (collectConfig) { + ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT, seqId); + } for (ConfigProvider.Source source : sources) { + seqId++; try { String sourceValue = source.get(key, aliases); - T value = ConfigConverter.valueOf(sourceValue, type); - if (value != null) { + T tmp = ConfigConverter.valueOf(sourceValue, type); + if (tmp != null) { + value = tmp; + usedOrigin = source.origin(); + usedSourceValue = sourceValue; if (collectConfig) { - ConfigCollector.get().put(key, sourceValue, source.origin()); + ConfigCollector.get().put(key, sourceValue, source.origin(), seqId); } - return value; + } + } catch (ConfigSourceException e) { + if (Objects.equals(key, "CONFIG_NAME")) { + System.out.println("MTOFF: exception thrown"); + } + if (collectConfig) { + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); } } catch (NumberFormatException ex) { // continue } } - if (collectConfig) { - ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT); + // Re-report the used value to ensure it has the highest seqId + if (collectConfig && value != null && usedOrigin != null && usedSourceValue != null) { + ConfigCollector.get().put(key, usedSourceValue, usedOrigin, seqId + 1); } - return defaultValue; + return value != null ? value : defaultValue; } public List getList(String key) { @@ -217,11 +305,13 @@ public List getList(String key) { } public List getList(String key, List defaultValue) { + // Do we need this, if getString will already report the default? + if (collectConfig) { + ConfigCollector.get() + .put(key, defaultValue, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID); + } String list = getString(key); if (null == list) { - if (collectConfig) { - ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT); - } return defaultValue; } else { return ConfigConverter.parseList(list); @@ -229,11 +319,13 @@ public List getList(String key, List defaultValue) { } public Set getSet(String key, Set defaultValue) { + // Do we need this, if getString will already report the default? + if (collectConfig) { + ConfigCollector.get() + .put(key, defaultValue, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID); + } String list = getString(key); if (null == list) { - if (collectConfig) { - ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT); - } return defaultValue; } else { return new HashSet(ConfigConverter.parseList(list)); @@ -246,64 +338,112 @@ public List getSpacedList(String key) { public Map getMergedMap(String key, String... aliases) { Map merged = new HashMap<>(); - ConfigOrigin origin = ConfigOrigin.DEFAULT; + ConfigOrigin origin = null; + int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1; // 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 - // We reverse iterate to allow overrides - for (int i = sources.length - 1; 0 <= i; i--) { - String value = sources[i].get(key, aliases); - Map parsedMap = ConfigConverter.parseMap(value, key); - if (!parsedMap.isEmpty()) { - origin = sources[i].origin(); + // We iterate in order so higher precedence sources overwrite lower precedence + for (Source source : sources) { + try { + String value = source.get(key, aliases); + Map parsedMap = ConfigConverter.parseMap(value, key); + if (!parsedMap.isEmpty()) { + origin = source.origin(); + if (collectConfig) { + ConfigCollector.get().put(key, parsedMap, origin, seqId); + } + } + merged.putAll(parsedMap); + } catch (ConfigSourceException e) { + if (collectConfig) { + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); + } } - merged.putAll(parsedMap); + seqId++; } if (collectConfig) { - ConfigCollector.get().put(key, merged, origin); + if (origin != null) { + // TO DISCUSS: But if multiple sources have been set, origin isn't exactly accurate here...? + ConfigCollector.get().put(key, merged, origin, seqId); + } else { + ConfigCollector.get() + .put(key, new HashMap<>(), ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID); + } } return merged; } public Map getMergedTagsMap(String key, String... aliases) { Map merged = new HashMap<>(); - ConfigOrigin origin = ConfigOrigin.DEFAULT; + ConfigOrigin origin = null; + int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1; // 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 - // We reverse iterate to allow overrides - for (int i = sources.length - 1; 0 <= i; i--) { - String value = sources[i].get(key, aliases); - Map parsedMap = - ConfigConverter.parseTraceTagsMap(value, ':', Arrays.asList(',', ' ')); - if (!parsedMap.isEmpty()) { - origin = sources[i].origin(); + // We iterate in order so higher precedence sources overwrite lower precedence + for (Source source : sources) { + try { + String value = source.get(key, aliases); + Map parsedMap = + ConfigConverter.parseTraceTagsMap(value, ':', Arrays.asList(',', ' ')); + if (!parsedMap.isEmpty()) { + origin = source.origin(); + if (collectConfig) { + ConfigCollector.get().put(key, parsedMap, origin, seqId); + } + } + merged.putAll(parsedMap); + } catch (ConfigSourceException e) { + if (collectConfig) { + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); + } } - merged.putAll(parsedMap); + seqId++; } if (collectConfig) { - ConfigCollector.get().put(key, merged, origin); + if (origin != null) { + ConfigCollector.get().put(key, merged, origin, seqId); + } else { + ConfigCollector.get().put(key, merged, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID); + } } return merged; } public Map getOrderedMap(String key) { LinkedHashMap merged = new LinkedHashMap<>(); - ConfigOrigin origin = ConfigOrigin.DEFAULT; + ConfigOrigin origin = null; + int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1; // 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 - // We reverse iterate to allow overrides - for (int i = sources.length - 1; 0 <= i; i--) { - String value = sources[i].get(key); - Map parsedMap = ConfigConverter.parseOrderedMap(value, key); - if (!parsedMap.isEmpty()) { - origin = sources[i].origin(); + // We iterate in order so higher precedence sources overwrite lower precedence + for (Source source : sources) { + try { + String value = source.get(key); + Map parsedMap = ConfigConverter.parseOrderedMap(value, key); + if (!parsedMap.isEmpty()) { + origin = source.origin(); + if (collectConfig) { + ConfigCollector.get().put(key, parsedMap, origin, seqId); + } + } + merged.putAll(parsedMap); + } catch (ConfigSourceException e) { + if (collectConfig) { + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); + } } - merged.putAll(parsedMap); + seqId++; } if (collectConfig) { - ConfigCollector.get().put(key, merged, origin); + if (origin != null) { + // TO DISCUSS: But if multiple sources have been set, origin isn't exactly accurate here...? + ConfigCollector.get().put(key, merged, origin, seqId); + } else { + ConfigCollector.get().put(key, merged, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID); + } } return merged; } @@ -311,23 +451,40 @@ public Map getOrderedMap(String key) { public Map getMergedMapWithOptionalMappings( String defaultPrefix, boolean lowercaseKeys, String... keys) { Map merged = new HashMap<>(); - ConfigOrigin origin = ConfigOrigin.DEFAULT; + ConfigOrigin origin = null; + int seqId = ConfigSetting.DEFAULT_SEQ_ID + 1; // 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 - // We reverse iterate to allow overrides + // We iterate in order so higher precedence sources overwrite lower precedence for (String key : keys) { - for (int i = sources.length - 1; 0 <= i; i--) { - String value = sources[i].get(key); - Map parsedMap = - ConfigConverter.parseMapWithOptionalMappings(value, key, defaultPrefix, lowercaseKeys); - if (!parsedMap.isEmpty()) { - origin = sources[i].origin(); + for (Source source : sources) { + try { + String value = source.get(key); + Map parsedMap = + ConfigConverter.parseMapWithOptionalMappings( + value, key, defaultPrefix, lowercaseKeys); + if (!parsedMap.isEmpty()) { + origin = source.origin(); + if (collectConfig) { + ConfigCollector.get().put(key, parsedMap, origin, seqId); + } + } + merged.putAll(parsedMap); + } catch (ConfigSourceException e) { + if (collectConfig) { + ConfigCollector.get().put(key, e.getRawValue(), source.origin(), seqId); + } } - merged.putAll(parsedMap); + seqId++; } if (collectConfig) { - ConfigCollector.get().put(key, merged, origin); + if (origin != null) { + ConfigCollector.get().put(key, merged, origin, seqId); + } else { + ConfigCollector.get() + .put(key, merged, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID); + } } } return merged; @@ -343,7 +500,8 @@ public BitSet getIntegerRange(final String key, final BitSet defaultValue, Strin log.warn("Invalid configuration for {}", key, e); } if (collectConfig) { - ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT); + ConfigCollector.get() + .put(key, defaultValue, ConfigOrigin.DEFAULT, ConfigSetting.DEFAULT_SEQ_ID); } return defaultValue; } @@ -378,7 +536,7 @@ public static ConfigProvider createDefault() { loadConfigurationFile( new ConfigProvider(new SystemPropertiesConfigSource(), new EnvironmentConfigSource())); if (configProperties.isEmpty()) { - return new ConfigProvider( + return createWithPrecedenceOrder( new SystemPropertiesConfigSource(), StableConfigSource.FLEET, new EnvironmentConfigSource(), @@ -386,7 +544,7 @@ public static ConfigProvider createDefault() { StableConfigSource.LOCAL, new CapturedEnvironmentConfigSource()); } else { - return new ConfigProvider( + return createWithPrecedenceOrder( new SystemPropertiesConfigSource(), StableConfigSource.FLEET, new EnvironmentConfigSource(), @@ -400,10 +558,10 @@ public static ConfigProvider createDefault() { public static ConfigProvider withoutCollector() { Properties configProperties = loadConfigurationFile( - new ConfigProvider( + createWithPrecedenceOrder( false, new SystemPropertiesConfigSource(), new EnvironmentConfigSource())); if (configProperties.isEmpty()) { - return new ConfigProvider( + return createWithPrecedenceOrder( false, new SystemPropertiesConfigSource(), StableConfigSource.FLEET, @@ -412,7 +570,7 @@ public static ConfigProvider withoutCollector() { StableConfigSource.LOCAL, new CapturedEnvironmentConfigSource()); } else { - return new ConfigProvider( + return createWithPrecedenceOrder( false, new SystemPropertiesConfigSource(), StableConfigSource.FLEET, @@ -428,12 +586,12 @@ public static ConfigProvider withPropertiesOverride(Properties properties) { PropertiesConfigSource providedConfigSource = new PropertiesConfigSource(properties, false); Properties configProperties = loadConfigurationFile( - new ConfigProvider( + createWithPrecedenceOrder( new SystemPropertiesConfigSource(), new EnvironmentConfigSource(), providedConfigSource)); if (configProperties.isEmpty()) { - return new ConfigProvider( + return createWithPrecedenceOrder( new SystemPropertiesConfigSource(), StableConfigSource.FLEET, new EnvironmentConfigSource(), @@ -442,7 +600,7 @@ public static ConfigProvider withPropertiesOverride(Properties properties) { StableConfigSource.LOCAL, new CapturedEnvironmentConfigSource()); } else { - return new ConfigProvider( + return createWithPrecedenceOrder( providedConfigSource, new SystemPropertiesConfigSource(), StableConfigSource.FLEET, @@ -500,7 +658,7 @@ private static Properties loadConfigurationFile(ConfigProvider configProvider) { } public abstract static class Source { - public final String get(String key, String... aliases) { + public final String get(String key, String... aliases) throws ConfigSourceException { String value = get(key); if (value != null) { return value; @@ -514,7 +672,7 @@ public final String get(String key, String... aliases) { return null; } - protected abstract String get(String key); + protected abstract String get(String key) throws ConfigSourceException; public abstract ConfigOrigin origin(); } diff --git a/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigSourceException.java b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigSourceException.java new file mode 100644 index 00000000000..2339f7a8a4c --- /dev/null +++ b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigSourceException.java @@ -0,0 +1,23 @@ +package datadog.trace.bootstrap.config.provider; + +/** + * Exception thrown when a ConfigProvider.Source encounters an error (e.g., parsing, IO, or format + * error) while retrieving a configuration value. + */ +public class ConfigSourceException extends Exception { + private final Object rawValue; + + public ConfigSourceException(String message, Object rawValue, Throwable cause) { + super(message, cause); + this.rawValue = rawValue; + } + + public ConfigSourceException(Object rawValue) { + this.rawValue = rawValue; + } + + /** Returns the raw value that caused the exception, if available. */ + public Object getRawValue() { + return rawValue; + } +} diff --git a/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigSource.java b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigSource.java index ab634b6fc14..bf62290b153 100644 --- a/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigSource.java +++ b/internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigSource.java @@ -47,12 +47,18 @@ public final class StableConfigSource extends ConfigProvider.Source { this.config = cfg; } - @Override - public String get(String key) { + public String get(String key) throws ConfigSourceException { if (this.config == StableConfig.EMPTY) { return null; } - return this.config.get(propertyNameToEnvironmentVariableName(key)); + Object value = this.config.get(propertyNameToEnvironmentVariableName(key)); + if (value == null) { + return null; + } + if (!(value instanceof String)) { + throw new ConfigSourceException(value); + } + return (String) value; } @Override @@ -78,9 +84,8 @@ public StableConfig(String configId, Map configMap) { this.apmConfiguration = configMap; } - public String get(String key) { - Object value = this.apmConfiguration.get(key); - return (value == null) ? null : String.valueOf(value); + public Object get(String key) { + return this.apmConfiguration.get(key); } public Set getKeys() { diff --git a/internal-api/src/test/groovy/datadog/trace/api/ConfigCollectorTest.groovy b/internal-api/src/test/groovy/datadog/trace/api/ConfigCollectorTest.groovy index 12a52136eb0..56ba23268e1 100644 --- a/internal-api/src/test/groovy/datadog/trace/api/ConfigCollectorTest.groovy +++ b/internal-api/src/test/groovy/datadog/trace/api/ConfigCollectorTest.groovy @@ -22,9 +22,18 @@ class ConfigCollectorTest extends DDSpecification { injectEnvConfig(Strings.toEnvVar(configKey), configValue) expect: - def setting = ConfigCollector.get().collect().get(configKey) - setting.stringValue() == configValue - setting.origin == ConfigOrigin.ENV + def configsByOrigin = ConfigCollector.get().collect().get(configKey) + configsByOrigin != null + configsByOrigin.size() == 2 // default and env origins + + // Check that the ENV value is present and correct + def envSetting = configsByOrigin.get(ConfigOrigin.ENV) + assert envSetting != null + assert envSetting.stringValue() == configValue + + // Check the default is present with non-null value + def defaultSetting = configsByOrigin.get(ConfigOrigin.DEFAULT) + assert defaultSetting != null where: configKey | configValue @@ -68,9 +77,13 @@ class ConfigCollectorTest extends DDSpecification { injectSysConfig(configKey, configValue2) expect: - def setting = ConfigCollector.get().collect().get(configKey) + def configsByOrigin = ConfigCollector.get().collect().get(configKey) + configsByOrigin != null + def setting = configsByOrigin.get(ConfigOrigin.JVM_PROP) + setting != null setting.stringValue() == expectedValue setting.origin == ConfigOrigin.JVM_PROP + // TODO: Add check for env origin as well where: configKey | configValue1 | configValue2 | expectedValue @@ -84,9 +97,10 @@ class ConfigCollectorTest extends DDSpecification { def "default not-null config settings are collected"() { expect: - def setting = ConfigCollector.get().collect().get(configKey) - setting.origin == ConfigOrigin.DEFAULT - setting.stringValue() == defaultValue + def configsByOrigin = ConfigCollector.get().collect().get(configKey) + configsByOrigin != null + def setting = configsByOrigin.get(ConfigOrigin.DEFAULT) + setting != null where: configKey | defaultValue @@ -100,12 +114,15 @@ class ConfigCollectorTest extends DDSpecification { def "default null config settings are also collected"() { when: - ConfigSetting cs = ConfigCollector.get().collect().get(configKey) + def configsByOrigin = ConfigCollector.get().collect().get(configKey) + configsByOrigin != null + def setting = configsByOrigin.get(ConfigOrigin.DEFAULT) + setting != null then: - cs.key == configKey - cs.stringValue() == null - cs.origin == ConfigOrigin.DEFAULT + setting.key == configKey + setting.stringValue() == null + setting.origin == ConfigOrigin.DEFAULT where: configKey << [ @@ -121,12 +138,14 @@ class ConfigCollectorTest extends DDSpecification { def "default empty maps and list config settings are collected as empty strings"() { when: - ConfigSetting cs = ConfigCollector.get().collect().get(configKey) + def configsByOrigin = ConfigCollector.get().collect().get(configKey) + def cs = configsByOrigin?.get(ConfigOrigin.DEFAULT) then: - cs.key == configKey - cs.stringValue() == "" - cs.origin == ConfigOrigin.DEFAULT + assert cs != null + assert cs.key == configKey + assert cs.stringValue() == "" + assert cs.origin == ConfigOrigin.DEFAULT where: configKey << [ @@ -143,12 +162,14 @@ class ConfigCollectorTest extends DDSpecification { when: ConfigCollector.get().put('key1', 'value1', ConfigOrigin.DEFAULT) ConfigCollector.get().put('key2', 'value2', ConfigOrigin.ENV) - ConfigCollector.get().put('key1', 'replaced', ConfigOrigin.REMOTE) + ConfigCollector.get().put('key1', 'value11', ConfigOrigin.REMOTE) ConfigCollector.get().put('key3', 'value3', ConfigOrigin.JVM_PROP) then: - ConfigCollector.get().collect().values().toSet() == [ - ConfigSetting.of('key1', 'replaced', ConfigOrigin.REMOTE), + def allSettings = ConfigCollector.get().collect().values().collectMany { it.values() }.toSet() + allSettings == [ + ConfigSetting.of('key1', 'value1', ConfigOrigin.DEFAULT), + ConfigSetting.of('key1', 'value11', ConfigOrigin.REMOTE), ConfigSetting.of('key2', 'value2', ConfigOrigin.ENV), ConfigSetting.of('key3', 'value3', ConfigOrigin.JVM_PROP) ] as Set @@ -163,7 +184,9 @@ class ConfigCollectorTest extends DDSpecification { ConfigCollector.get().put('DD_API_KEY', 'sensitive data', ConfigOrigin.ENV) then: - ConfigCollector.get().collect().get('DD_API_KEY').stringValue() == '' + def configsByOrigin = ConfigCollector.get().collect().get('DD_API_KEY') + configsByOrigin != null + configsByOrigin.get(ConfigOrigin.ENV).stringValue() == '' } def "collects common setting default values"() { @@ -171,7 +194,10 @@ class ConfigCollectorTest extends DDSpecification { def settings = ConfigCollector.get().collect() then: - def setting = settings.get(key) + def configsByOrigin = settings.get(key) + configsByOrigin != null + def setting = configsByOrigin.get(ConfigOrigin.DEFAULT) + setting != null setting.key == key setting.stringValue() == value @@ -205,7 +231,10 @@ class ConfigCollectorTest extends DDSpecification { def settings = ConfigCollector.get().collect() then: - def setting = settings.get(key) + def configsByOrigin = settings.get(key) + configsByOrigin != null + def setting = configsByOrigin.get(ConfigOrigin.ENV) + setting != null setting.key == key setting.stringValue() == value @@ -224,4 +253,29 @@ class ConfigCollectorTest extends DDSpecification { "logs.injection.enabled" | "false" "trace.sample.rate" | "0.3" } + + def "getHighestSeqIdConfig returns the setting with the highest seqId for a key"() { + setup: + def collector = ConfigCollector.get() + collector.collect() // clear previous state + collector.put('test.key', 'default', ConfigOrigin.DEFAULT, 1) + collector.put('test.key', 'env', ConfigOrigin.ENV, 2) + collector.put('test.key', 'remote', ConfigOrigin.REMOTE, 4) + collector.put('test.key', 'jvm', ConfigOrigin.JVM_PROP, 3) + + when: + def applied = collector.getHighestSeqIdConfig('test.key') + + then: + applied != null + applied.key == 'test.key' + applied.value == 'remote' + applied.origin == ConfigOrigin.REMOTE + + when: "no settings for a key" + def none = collector.getHighestSeqIdConfig('nonexistent.key') + + then: + none == null + } } diff --git a/internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/ConfigProviderTest.groovy b/internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/ConfigProviderTest.groovy index b9783ec59c1..f5c5db51af7 100644 --- a/internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/ConfigProviderTest.groovy +++ b/internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/ConfigProviderTest.groovy @@ -2,6 +2,8 @@ package datadog.trace.bootstrap.config.provider import datadog.trace.test.util.DDSpecification import spock.lang.Shared +import datadog.trace.api.ConfigCollector +import datadog.trace.api.ConfigOrigin import static datadog.trace.api.config.TracerConfig.TRACE_HTTP_SERVER_PATH_RESOURCE_NAME_MAPPING @@ -45,4 +47,26 @@ class ConfigProviderTest extends DDSpecification { "default" | null | "alias2" | "default" null | "alias1" | "alias2" | "alias1" } + + def "test always reports config in use with the highest seqId regardless of errors"() { + setup: + // System props is higher precedence than env config + injectEnvConfig("CONFIG_NAME", "123") + injectSysConfig("CONFIG_NAME", "not_an_int") + + when: + def configProviderWithCollector = ConfigProvider.createDefault() + def config = configProviderWithCollector.getInteger("CONFIG_NAME") + + then: + config == 123 + + when: + def highestSeqIdConfig = ConfigCollector.get().getHighestSeqIdConfig('CONFIG_NAME') + + then: + highestSeqIdConfig.key == 'CONFIG_NAME' + highestSeqIdConfig.value == '123' + highestSeqIdConfig.origin == ConfigOrigin.ENV + } } diff --git a/telemetry/src/main/java/datadog/telemetry/TelemetryRequestBody.java b/telemetry/src/main/java/datadog/telemetry/TelemetryRequestBody.java index ac8466a1913..5f61b3d5586 100644 --- a/telemetry/src/main/java/datadog/telemetry/TelemetryRequestBody.java +++ b/telemetry/src/main/java/datadog/telemetry/TelemetryRequestBody.java @@ -230,6 +230,7 @@ public void writeConfiguration(ConfigSetting configSetting) throws IOException { bodyWriter.name("value").value(configSetting.stringValue()); bodyWriter.setSerializeNulls(false); bodyWriter.name("origin").value(configSetting.origin.value); + bodyWriter.name("seq_id").value(configSetting.seqId); bodyWriter.endObject(); } diff --git a/telemetry/src/main/java/datadog/telemetry/TelemetryRunnable.java b/telemetry/src/main/java/datadog/telemetry/TelemetryRunnable.java index e807bc0cd82..887f118c80d 100644 --- a/telemetry/src/main/java/datadog/telemetry/TelemetryRunnable.java +++ b/telemetry/src/main/java/datadog/telemetry/TelemetryRunnable.java @@ -3,6 +3,7 @@ import datadog.telemetry.metric.MetricPeriodicAction; import datadog.trace.api.Config; import datadog.trace.api.ConfigCollector; +import datadog.trace.api.ConfigOrigin; import datadog.trace.api.ConfigSetting; import datadog.trace.api.time.SystemTimeSource; import datadog.trace.api.time.TimeSource; @@ -141,9 +142,9 @@ private void mainLoopIteration() throws InterruptedException { } private void collectConfigChanges() { - Map collectedConfig = ConfigCollector.get().collect(); + Map> collectedConfig = ConfigCollector.get().collect(); if (!collectedConfig.isEmpty()) { - telemetryService.addConfiguration(collectedConfig); + telemetryService.addConfigurationByOrigin(collectedConfig); } } diff --git a/telemetry/src/main/java/datadog/telemetry/TelemetryService.java b/telemetry/src/main/java/datadog/telemetry/TelemetryService.java index 1160c27223a..913c2b38a2f 100644 --- a/telemetry/src/main/java/datadog/telemetry/TelemetryService.java +++ b/telemetry/src/main/java/datadog/telemetry/TelemetryService.java @@ -7,6 +7,7 @@ import datadog.telemetry.api.Metric; import datadog.telemetry.api.RequestType; import datadog.telemetry.dependency.Dependency; +import datadog.trace.api.ConfigOrigin; import datadog.trace.api.ConfigSetting; import datadog.trace.api.telemetry.Endpoint; import datadog.trace.api.telemetry.ProductChange; @@ -94,6 +95,27 @@ public boolean addConfiguration(Map configuration) { return true; } + /** + * Adds all configuration settings from the provided map, grouped by their origin. + * + * @param configuration a map of configuration keys to a map of origins and their corresponding + * settings + * @return {@code true} if all settings were successfully added, {@code false} if the queue is + * full + */ + public boolean addConfigurationByOrigin( + Map> configuration) { + for (Map settings : configuration.values()) { + for (ConfigSetting cs : settings.values()) { + extendedHeartbeatData.pushConfigSetting(cs); + if (!this.configurations.offer(cs)) { + return false; + } + } + } + return true; + } + public boolean addDependency(Dependency dependency) { extendedHeartbeatData.pushDependency(dependency); return this.dependencies.offer(dependency); diff --git a/telemetry/src/test/groovy/datadog/telemetry/TelemetryRequestBodySpecification.groovy b/telemetry/src/test/groovy/datadog/telemetry/TelemetryRequestBodySpecification.groovy index d0f7832da20..33a390b5af9 100644 --- a/telemetry/src/test/groovy/datadog/telemetry/TelemetryRequestBodySpecification.groovy +++ b/telemetry/src/test/groovy/datadog/telemetry/TelemetryRequestBodySpecification.groovy @@ -79,12 +79,12 @@ class TelemetryRequestBodySpecification extends DDSpecification { then: drainToString(req) == ',"configuration":[' + - '{"name":"string","value":"bar","origin":"remote_config"},' + - '{"name":"int","value":"2342","origin":"default"},' + - '{"name":"double","value":"123.456","origin":"env_var"},' + - '{"name":"map","value":"key1:value1,key2:432.32,key3:324","origin":"jvm_prop"},' + - '{"name":"list","value":"1,2,3","origin":"default"},' + - '{"name":"null","value":null,"origin":"default"}]' + '{"name":"string","value":"bar","origin":"remote_config","seq_id":0},' + + '{"name":"int","value":"2342","origin":"default","seq_id":0},' + + '{"name":"double","value":"123.456","origin":"env_var","seq_id":0},' + + '{"name":"map","value":"key1:value1,key2:432.32,key3:324","origin":"jvm_prop","seq_id":0},' + + '{"name":"list","value":"1,2,3","origin":"default","seq_id":0},' + + '{"name":"null","value":null,"origin":"default","seq_id":0}]' } def 'use snake_case for setting keys'() { @@ -102,8 +102,7 @@ class TelemetryRequestBodySpecification extends DDSpecification { req.endConfiguration() then: - drainToString(req) == ',"configuration":[{"name":"this_is_a_key","value":"value","origin":"remote_config"}]' - } + drainToString(req) == ',"configuration":[{"name":"this_is_a_key","value":"value","origin":"remote_config","seq_id":0}]' } def 'add debug flag'() { setup: diff --git a/telemetry/src/test/groovy/datadog/telemetry/TelemetryRunnableSpecification.groovy b/telemetry/src/test/groovy/datadog/telemetry/TelemetryRunnableSpecification.groovy index 6ddebef3782..b7a712b7c80 100644 --- a/telemetry/src/test/groovy/datadog/telemetry/TelemetryRunnableSpecification.groovy +++ b/telemetry/src/test/groovy/datadog/telemetry/TelemetryRunnableSpecification.groovy @@ -54,8 +54,8 @@ class TelemetryRunnableSpecification extends DDSpecification { then: 'two unsuccessful attempts to send app-started with the following successful attempt' 3 * telemetryService.sendAppStartedEvent() >>> [false, false, true] + _ * telemetryService.addConfigurationByOrigin(_) 1 * timeSource.getCurrentTimeMillis() >> 60 * 1000 - _ * telemetryService.addConfiguration(_) then: 1 * metricCollector.prepareMetrics() @@ -69,7 +69,8 @@ class TelemetryRunnableSpecification extends DDSpecification { 3 * telemetryService.sendTelemetryEvents() >>> [true, true, false] 1 * timeSource.getCurrentTimeMillis() >> 60 * 1000 + 1 1 * sleeperMock.sleep(9999) - 0 * _ + // 0 * _ + _ * telemetryService.addConfigurationByOrigin(_) when: 'second iteration (10 seconds, metrics)' sleeper.go.await(10, TimeUnit.SECONDS) diff --git a/telemetry/src/test/groovy/datadog/telemetry/TestTelemetryRouter.groovy b/telemetry/src/test/groovy/datadog/telemetry/TestTelemetryRouter.groovy index d1b29549bf1..b50f3bbe399 100644 --- a/telemetry/src/test/groovy/datadog/telemetry/TestTelemetryRouter.groovy +++ b/telemetry/src/test/groovy/datadog/telemetry/TestTelemetryRouter.groovy @@ -237,7 +237,7 @@ class TestTelemetryRouter extends TelemetryRouter { def expected = configuration == null ? null : [] if (configuration != null) { for (ConfigSetting cs : configuration) { - expected.add([name: cs.normalizedKey(), value: cs.stringValue(), origin: cs.origin.value]) + expected.add([name: cs.normalizedKey(), value: cs.stringValue(), origin: cs.origin.value, seqId: cs.seqId]) } } assert this.payload['configuration'] == expected