Skip to content

Support reading configurations from files #8338

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

Merged
merged 46 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c512b78
Initial commit
paullegranddc Feb 3, 2025
dc449e0
Initial tests for StableConfigSource
mtoffl01 Feb 4, 2025
6d72f81
Add @Override for stableconfig get and configorigin methods
mtoffl01 Feb 4, 2025
92c81fd
apply github code qual suggestions
mtoffl01 Feb 4, 2025
635ba8a
passing initial tests
mtoffl01 Feb 5, 2025
d5bf963
support config_id
mtoffl01 Feb 5, 2025
9f03e04
Additional tests for invalid files
mtoffl01 Feb 5, 2025
06d572d
Introduce StableConfigSource to the list of ConfigProvider sources
mtoffl01 Feb 6, 2025
a03562c
add comments to current tests
mtoffl01 Feb 7, 2025
ac7b387
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 Feb 7, 2025
908a72d
fix typo
mtoffl01 Feb 7, 2025
abfc789
Merge branch 'mtoff/stable_config' of github.com:DataDog/dd-trace-jav…
mtoffl01 Feb 7, 2025
38355fe
Refactor: Introduce StableConfigSource sub-class, StableConfig
mtoffl01 Feb 10, 2025
aebebbe
nits: docs and cleanup
mtoffl01 Feb 10, 2025
90b52e9
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 Feb 10, 2025
cdd8df8
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 Feb 10, 2025
1fed1da
add doc for getStableConfig
mtoffl01 Feb 10, 2025
9f2b252
Merge branch 'mtoff/stable_config' of github.com:DataDog/dd-trace-jav…
mtoffl01 Feb 10, 2025
2da99f2
Merge branch 'master' into mtoff/stable_config
mtoffl01 Feb 10, 2025
ae6036b
Optimize: lazily load configuration information from file, cache results
mtoffl01 Feb 12, 2025
e3d6c80
nits: variable renaming, comments to describe variables
mtoffl01 Feb 12, 2025
a7bf5ba
nit: move comment above variable
mtoffl01 Feb 12, 2025
0c8ef6a
fix typo in managed file path
mtoffl01 Feb 13, 2025
049da8b
Initial StableConfigParser implementation and testing
mtoffl01 Feb 13, 2025
39fa0e2
Finalize StableConfigParser and tests
mtoffl01 Feb 13, 2025
53dec92
User parser in StableconfigSource; no more lazy loading
mtoffl01 Feb 13, 2025
96881d0
Add new logs for testing
mtoffl01 Feb 13, 2025
09e22a7
remove snakeyaml from dependency tree in internal-api
mtoffl01 Feb 13, 2025
519c443
Fix class initialization order
mcculls Feb 14, 2025
9a519e1
update parser test to use raw file input as opposed to map
mtoffl01 Feb 15, 2025
a9f968f
make ParserTest handle unexpected input format as well
mtoffl01 Feb 18, 2025
7c93f15
Revert fileretries stuff
mtoffl01 Feb 18, 2025
36a7e8a
Change managed path to reflect system tests
mtoffl01 Feb 18, 2025
9598e11
remove superfluous comments/logs used for testing
mtoffl01 Feb 18, 2025
bf9efad
Merge branch 'master' into mtoff/stable_config
mtoffl01 Feb 18, 2025
a34a546
snakeyaml is only used for testing now
mcculls Feb 19, 2025
a3be88d
Register StableConfigSource with native-image since we need it at bui…
mcculls Feb 19, 2025
99cb07b
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 Feb 20, 2025
cf99c4f
Address PR comments
mtoffl01 Feb 20, 2025
3d1a1d8
Change javadoc on getStableConfig function
mtoffl01 Feb 20, 2025
41fd3bc
Update dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/boot…
mtoffl01 Feb 20, 2025
9dd51fe
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 Feb 20, 2025
f143632
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 Feb 20, 2025
ea8d9f7
Apply PR suggestions round 2
mtoffl01 Feb 20, 2025
4366cab
Apply PR suggestions round 3
mtoffl01 Feb 21, 2025
eb7618b
Merge branch 'master' into mtoff/stable_config
mtoffl01 Feb 24, 2025
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 @@ -33,6 +33,7 @@
import datadog.trace.api.profiling.ProfilingEnablement;
import datadog.trace.api.scopemanager.ScopeListener;
import datadog.trace.bootstrap.benchmark.StaticEventLogger;
import datadog.trace.bootstrap.config.provider.StableConfigSource;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.TracerAPI;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
Expand Down Expand Up @@ -1214,9 +1215,15 @@ private static boolean isFeatureEnabled(AgentFeature feature) {
// must be kept in sync with logic from Config!
final String featureEnabledSysprop = feature.getSystemProp();
String featureEnabled = System.getProperty(featureEnabledSysprop);
if (featureEnabled == null) {
featureEnabled = getStableConfig(StableConfigSource.MANAGED, featureEnabledSysprop);
}
if (featureEnabled == null) {
featureEnabled = ddGetEnv(featureEnabledSysprop);
}
if (featureEnabled == null) {
featureEnabled = getStableConfig(StableConfigSource.USER, featureEnabledSysprop);
}

if (feature.isEnabledByDefault()) {
// true unless it's explicitly set to "false"
Expand Down Expand Up @@ -1353,6 +1360,11 @@ private static String ddGetProperty(final String sysProp) {
return value;
}

/** Looks for sysProp in the Stable Configuration input */
private static String getStableConfig(StableConfigSource source, final String sysProp) {
return source.get(sysProp);
}

/** Looks for the "DD_" environment variable equivalent of the given "dd." system property. */
private static String ddGetEnv(final String sysProp) {
return System.getenv(toEnvVar(sysProp));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static void onEnter(@Advice.Argument(value = 0, readOnly = false) String[
+ "datadog.trace.api.env.CapturedEnvironment:build_time,"
+ "datadog.trace.api.ConfigCollector:rerun,"
+ "datadog.trace.api.ConfigDefaults:build_time,"
+ "datadog.trace.api.ConfigOrigin:build_time,"
+ "datadog.trace.api.ConfigSetting:build_time,"
+ "datadog.trace.api.EventTracker:build_time,"
+ "datadog.trace.api.InstrumenterConfig:build_time,"
Expand Down Expand Up @@ -106,6 +107,8 @@ public static void onEnter(@Advice.Argument(value = 0, readOnly = false) String[
+ "datadog.trace.bootstrap.config.provider.EnvironmentConfigSource:build_time,"
+ "datadog.trace.bootstrap.config.provider.OtelEnvironmentConfigSource:build_time,"
+ "datadog.trace.bootstrap.config.provider.SystemPropertiesConfigSource:build_time,"
+ "datadog.trace.bootstrap.config.provider.StableConfigSource:build_time,"
+ "datadog.trace.bootstrap.config.provider.StableConfigSource$StableConfig:build_time,"
+ "datadog.trace.bootstrap.Agent:build_time,"
+ "datadog.trace.bootstrap.BootstrapProxy:build_time,"
+ "datadog.trace.bootstrap.CallDepthThreadLocalMap:build_time,"
Expand Down
1 change: 1 addition & 0 deletions internal-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ dependencies {
testImplementation libs.commons.math
testImplementation libs.bundles.mockito
testImplementation libs.truth
testImplementation 'org.yaml:snakeyaml:2.0'
}

jmh {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public enum ConfigOrigin {
REMOTE("remote_config"),
/** configurations that are set through JVM properties */
JVM_PROP("jvm_prop"),
/** configuration read in the stable config file, managed by users */
USER_STABLE_CONFIG("user_stable_config"),
/** configuration read in the stable config file, managed by fleet */
MANAGED_STABLE_CONFIG("managed_stable_config"),
/** set when the user has not set any configuration for the key (defaults to a value) */
DEFAULT("default");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,19 @@ public static ConfigProvider createDefault() {
if (configProperties.isEmpty()) {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
StableConfigSource.MANAGED,
new EnvironmentConfigSource(),
new OtelEnvironmentConfigSource(),
StableConfigSource.USER,
new CapturedEnvironmentConfigSource());
} else {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
StableConfigSource.MANAGED,
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
StableConfigSource.USER,
new CapturedEnvironmentConfigSource());
}
}
Expand All @@ -378,16 +382,20 @@ public static ConfigProvider withoutCollector() {
return new ConfigProvider(
false,
new SystemPropertiesConfigSource(),
StableConfigSource.MANAGED,
new EnvironmentConfigSource(),
new OtelEnvironmentConfigSource(),
StableConfigSource.USER,
new CapturedEnvironmentConfigSource());
} else {
return new ConfigProvider(
false,
new SystemPropertiesConfigSource(),
StableConfigSource.MANAGED,
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
StableConfigSource.USER,
new CapturedEnvironmentConfigSource());
}
}
Expand All @@ -403,17 +411,21 @@ public static ConfigProvider withPropertiesOverride(Properties properties) {
if (configProperties.isEmpty()) {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
StableConfigSource.MANAGED,
new EnvironmentConfigSource(),
providedConfigSource,
new OtelEnvironmentConfigSource(),
StableConfigSource.USER,
new CapturedEnvironmentConfigSource());
} else {
return new ConfigProvider(
providedConfigSource,
new SystemPropertiesConfigSource(),
StableConfigSource.MANAGED,
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
StableConfigSource.USER,
new CapturedEnvironmentConfigSource());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package datadog.trace.bootstrap.config.provider;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StableConfigParser {
private static final Logger log = LoggerFactory.getLogger(StableConfigParser.class);
// Match config_id:<value>
private static final Pattern idPattern = Pattern.compile("^config_id\\s*:(.*)$");
// Match 'apm_configuration_default:'
private static final Pattern apmConfigPattern = Pattern.compile("^apm_configuration_default:$");
// Match indented (2 spaces) key-value pairs, either with double quotes or without
private static final Pattern keyValPattern =
Pattern.compile("^\\s{2}([^:]+):\\s*(\"[^\"]*\"|[^\"\\n]*)$");;

public static StableConfigSource.StableConfig parse(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
log.debug("Stable configuration file not available at specified path: {}", file);
return StableConfigSource.StableConfig.EMPTY;
}
Map<String, String> configMap = new HashMap<>();
String[] configId = new String[1];
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
int apmConfigNotFound = -1, apmConfigStarted = 0, apmConfigComplete = 1;
int[] apmConfigFound = {apmConfigNotFound};
lines.forEach(
line -> {
Matcher matcher = idPattern.matcher(line);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there only one configId per file? If so then we can store this in a String[1] array above and only call this matcher if we haven't yet found the configId

Copy link
Contributor Author

@mtoffl01 mtoffl01 Feb 20, 2025

Choose a reason for hiding this comment

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

Is there expected to only be one config_id per file? — Theoretically yes, but neither the spec nor the tests specify how to handle multiple the scenario where multiple config_ids are present.
With my implementation, the last config_id processed will be used. With your approach, the first one will be used.

I'm reaching out to the channel now to see if we want to standardize on this behavior.
Update: Consensus seems to be: throw an error if >1 config_id is found.
https://dd.slack.com/archives/C0838DD5SSJ/p1740074416283709

if (matcher.find()) {
// Do not allow duplicate config_id keys
if (configId[0] != null) {
throw new RuntimeException("Duplicate config_id keys found; file may be malformed");
}
configId[0] = trimQuotes(matcher.group(1).trim());
return; // go to next line
}
// TODO: Do not allow duplicate apm_configuration_default keys; and/or return early once
// apmConfigFound[0] == apmConfigComplete
if (apmConfigFound[0] == apmConfigNotFound
&& apmConfigPattern.matcher(line).matches()) {
apmConfigFound[0] = apmConfigStarted;
return; // go to next line
}
if (apmConfigFound[0] == apmConfigStarted) {
Matcher keyValueMatcher = keyValPattern.matcher(line);
if (keyValueMatcher.matches()) {
configMap.put(
keyValueMatcher.group(1).trim(),
trimQuotes(keyValueMatcher.group(2).trim())); // Store key-value pair in map
} else {
// If we encounter a non-indented or non-key-value line, stop processing
apmConfigFound[0] = apmConfigComplete;
}
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a shame that the Java 8 stream API doesn't have the takeWhile and dropWhile methods that were added in Java 9 (https://docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html#takeWhile-java.util.function.Predicate-) because that would have really helped here :)

It's possible to mimic those with a custom Spliterator as shown in https://www.baeldung.com/java-break-stream-foreach#custom-spliterator

but TBH I think the approach taken here is fine given the limited API available to us in Java 8

return new StableConfigSource.StableConfig(configId[0], configMap);
}
}

private static String trimQuotes(String value) {
if (value.length() > 1 && (value.startsWith("'") && value.endsWith("'"))
|| (value.startsWith("\"") && value.endsWith("\""))) {
return value.substring(1, value.length() - 1);
}
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package datadog.trace.bootstrap.config.provider;

import static datadog.trace.util.Strings.propertyNameToEnvironmentVariableName;

import datadog.trace.api.ConfigOrigin;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class StableConfigSource extends ConfigProvider.Source {
private static final Logger log = LoggerFactory.getLogger(StableConfigSource.class);

public static final String USER_STABLE_CONFIG_PATH =
"/etc/datadog-agent/application_monitoring.yaml";
public static final String MANAGED_STABLE_CONFIG_PATH =
"/etc/datadog-agent/managed/datadog-agent/stable/application_monitoring.yaml";
public static final StableConfigSource USER =
new StableConfigSource(USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG);
public static final StableConfigSource MANAGED =
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG);

private final ConfigOrigin fileOrigin;

private final StableConfig config;

StableConfigSource(String file, ConfigOrigin origin) {
this.fileOrigin = origin;
StableConfig cfg;
try {
cfg = StableConfigParser.parse(file);
} catch (Throwable e) {
log.debug("Stable configuration file not readable at specified path: {}", file);
cfg = StableConfig.EMPTY;
}
this.config = cfg;
}

@Override
public String get(String key) {
if (this.config == StableConfig.EMPTY) {
return null;
}
return this.config.get(propertyNameToEnvironmentVariableName(key));
}

@Override
public ConfigOrigin origin() {
return fileOrigin;
}

public Set<String> getKeys() {
return this.config.getKeys();
}

public String getConfigId() {
return this.config.getConfigId();
}

public static class StableConfig {
public static final StableConfig EMPTY = new StableConfig(null, Collections.emptyMap());
private final Map<String, String> apmConfiguration;
private final String configId;

StableConfig(String configId, Map<String, String> configMap) {
this.configId = configId;
this.apmConfiguration = configMap;
}

public String get(String key) {
return this.apmConfiguration.get(key);
}

public Set<String> getKeys() {
return this.apmConfiguration.keySet();
}

public String getConfigId() {
return this.configId;
}
}
}
Loading
Loading