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 8 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
2 changes: 2 additions & 0 deletions internal-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ dependencies {
// it contains annotations that are also present in the instrumented application classes
api "com.datadoghq:dd-javac-plugin-client:0.2.2"

implementation 'org.yaml:snakeyaml:2.0'

testImplementation project(":utils:test-utils")
testImplementation("org.assertj:assertj-core:3.20.2")
testImplementation libs.bundles.junit5
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 @@ -353,19 +353,29 @@ public static ConfigProvider createDefault() {
Properties configProperties =
loadConfigurationFile(
new ConfigProvider(new SystemPropertiesConfigSource(), new EnvironmentConfigSource()));
// MIKAYLA: What is the significance of configProperties, of this isEmpty check?
if (configProperties.isEmpty()) {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG),
new EnvironmentConfigSource(),
new OtelEnvironmentConfigSource(),
new CapturedEnvironmentConfigSource());
new CapturedEnvironmentConfigSource(), // MIKAYLA: what is
// CapturedEnvironmentConfigSource?
new StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG));
} else {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG),
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
new CapturedEnvironmentConfigSource());
new CapturedEnvironmentConfigSource(),
new StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG));
}
}

Expand All @@ -378,20 +388,29 @@ public static ConfigProvider withoutCollector() {
return new ConfigProvider(
false,
new SystemPropertiesConfigSource(),
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG),
new EnvironmentConfigSource(),
new OtelEnvironmentConfigSource(),
new CapturedEnvironmentConfigSource());
new CapturedEnvironmentConfigSource(),
new StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG));
} else {
return new ConfigProvider(
false,
new SystemPropertiesConfigSource(),
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG),
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
new CapturedEnvironmentConfigSource());
new CapturedEnvironmentConfigSource(),
new StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG));
}
}

// MIKAYLA: What is providedConfigSource, and how should it stand up against stableconfig?
public static ConfigProvider withPropertiesOverride(Properties properties) {
PropertiesConfigSource providedConfigSource = new PropertiesConfigSource(properties, false);
Properties configProperties =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package datadog.trace.bootstrap.config.provider;

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

import datadog.trace.api.ConfigOrigin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

public final class StableConfigSource extends ConfigProvider.Source {
static final String USER_STABLE_CONFIG_PATH = "/etc/datadog-agent/application_monitoring.yaml";
static final String MANAGED_STABLE_CONFIG_PATH =
"/etc/datadog-agent/managed/datadog-apm-libraries/stable/application_monitoring.yaml ";
private static final Logger log = LoggerFactory.getLogger(StableConfigSource.class);

private final ConfigOrigin fileOrigin;
private final Map<String, Object> configuration;
private final String configId;

StableConfigSource(String file, ConfigOrigin origin) {
this.fileOrigin = origin;
HashMap<String, Object> data = readYamlFromFile(file);
if (data == null) {
this.configuration = new HashMap<>();
this.configId = null;
} else {
this.configId = (String) data.get("config_id");
this.configuration = parseStableConfig(data);
}
}

/**
* Reads configuration data from the YAML file located at the specified file path.
*
* <p>If the file is in a valid YAML format, this method returns a {@link HashMap} containing the
* configuration information.
*
* <p>If the file is in an invalid format, the method returns <code>null</code>.
*
* @param filePath The path to the YAML file to be read.
* @return A {@link HashMap} containing the configuration data if the file is valid, or <code>null
* </code> if the file is in an invalid format.
*/
private static HashMap<String, Object> readYamlFromFile(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
log.debug(
"Stable configuration file does not exist at the specified path: {}, ignoring", filePath);
return null;
}

Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
InputStream input;
try {
input = Files.newInputStream(new File(filePath).toPath());
} catch (IOException e) {
// throw new RuntimeException(e); // Do we want to do this? Or fail more gracefully?
log.error("Unable to read from stable config file {}, dropping input", filePath);
return null;
}
try {
return yaml.load(input);
} catch (Exception e) {
log.error("YAML parsing error in stable config file {}: {}", filePath, e.getMessage());
return null;
}
}

private static Map<String, Object> parseStableConfig(HashMap<String, Object> data) {
HashMap<String, Object> config = new HashMap<>();
Object apmConfig = data.get("apm_configuration_default");
if (apmConfig == null) {

return config;
}
if (apmConfig instanceof HashMap<?, ?>) {
HashMap<?, ?> tempConfig = (HashMap<?, ?>) apmConfig;
for (Map.Entry<?, ?> entry : tempConfig.entrySet()) {
if (entry.getKey() instanceof String && entry.getValue() != null) {
String key = String.valueOf(entry.getKey());
Object value = entry.getValue();
config.put(key, value);
} else {
log.debug("Config key {} in unexpected format", entry.getKey());
}
}
} else {
// do something
log.debug("File in unexpected format");
}
return config;
};

@Override
public String get(String key) {
return (String) this.configuration.get(propertyNameToEnvironmentVariableName(key));
}

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

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

public String getConfigId() {
return this.configId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package datadog.trace.bootstrap.config.provider

import datadog.trace.api.ConfigOrigin
import datadog.trace.test.util.DDSpecification
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
import spock.lang.Shared

import java.nio.file.Path
import java.nio.file.Files
import java.nio.file.StandardOpenOption

class StableConfigSourceTest extends DDSpecification {

def "test file doesn't exist"() {
setup:
StableConfigSource config = new StableConfigSource(StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG)

expect:
config.getKeys().size() == 0
config.getConfigId() == null
}

def "test empty file"() {
// test empty file
when:
Path filePath
StableConfigSource config
try {
filePath = Files.createTempFile("testFile_", ".yaml")
} catch (IOException e) {
println "Error creating file: ${e.message}"
e.printStackTrace()
return // or throw new RuntimeException("File creation failed", e)
}
if (filePath != null) {
config = new StableConfigSource(filePath.toString(), ConfigOrigin.USER_STABLE_CONFIG)
} else {
return
}

then:
config.getKeys().size() == 0
config.getConfigId() == null
}

def "test get"() {
when:
Path filePath = tempFile()
if (filePath == null) {
return // fail?
}

def configs = new HashMap<>() << ["DD_SERVICE": "svc", "DD_ENV": "env", "CONFIG_NO_DD": "value123"]

try {
writeFileYaml(filePath, "12345", configs)
} catch (IOException e) {
println "Error writing to file: ${e.message}"
return // fail?
}

StableConfigSource cfg = new StableConfigSource(filePath.toString(), ConfigOrigin.USER_STABLE_CONFIG)

then:
cfg.getKeys().size() == 3
cfg.get("service") == "svc"
cfg.get("env") == "env"
cfg.get("config_no_dd") == null
cfg.get("config_nonexistent") == null
}

def "test file invalid format"() {
when:
Path filePath = tempFile()
if (filePath == null) {
return // fail?
}

try {
writeFileRaw(filePath, configId, configs)
} catch (IOException e) {
println "Error writing to file: ${e.message}"
return // fail?
}

StableConfigSource stableCfg = new StableConfigSource(filePath.toString(), ConfigOrigin.USER_STABLE_CONFIG)

then:
stableCfg.getConfigId() == null
stableCfg.getKeys().size() == 0
Files.delete(filePath)

where:
configId | configs
null | corruptYaml
"12345" | "this is not yaml format!"
}

def "test file valid format"() {
when:
Path filePath = tempFile()
if (filePath == null) {
return // fail?
}

try {
writeFileYaml(filePath, configId, configs)
} catch (IOException e) {
println "Error writing to file: ${e.message}"
return // fail?
}

StableConfigSource stableCfg = new StableConfigSource(filePath.toString(), ConfigOrigin.USER_STABLE_CONFIG)

then:
stableCfg.getConfigId() == configId
stableCfg.getKeys().size() == configs.size()
for (key in stableCfg.getKeys()) {
key = key.substring(4) // Cut `DD_`
stableCfg.get(key) == configs.get(key)
}
Files.delete(filePath)

where:
configId | configs
"" | new HashMap<>()
"12345" | new HashMap<>() << ["DD_KEY_ONE": "one", "DD_KEY_TWO": "two"]
}
// Corrupt YAML string variable used for testing, defined outside the 'where' block for readability
@Shared
def corruptYaml = '''
abc: 123
def:
ghi: "jkl"
lmn: 456
'''

Path tempFile() {
try {
return Files.createTempFile("testFile_", ".yaml")
} catch (IOException e) {
println "Error creating file: ${e.message}"
e.printStackTrace()
return null // or throw new RuntimeException("File creation failed", e)
}
}

def writeFileYaml(Path filePath, String configId, Map configs) {
DumperOptions options = new DumperOptions()
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)

// Prepare to write the data map to the file in yaml format
Yaml yaml = new Yaml(options)
String yamlString
Map<String, Object> data = new HashMap<>()
if (configId != null) {
data.put("config_id", configId)
}
if (configs instanceof HashMap<?, ?>) {
data.put("apm_configuration_default", configs)
}

yamlString = yaml.dump(data)

StandardOpenOption[] openOpts = [StandardOpenOption.WRITE] as StandardOpenOption[]
Files.write(filePath, yamlString.getBytes(), openOpts)
}

def writeFileRaw(Path filePath, String configId, String configs) {
String data = configId + "\n" + configs
StandardOpenOption[] openOpts = [StandardOpenOption.WRITE] as StandardOpenOption[]
Files.write(filePath, data.getBytes(), openOpts)
}
}
Loading