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 14 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,13 +33,15 @@
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;
import datadog.trace.bootstrap.instrumentation.jfr.InstrumentationBasedProfiling;
import datadog.trace.util.AgentTaskScheduler;
import datadog.trace.util.AgentThreadFactory.AgentThread;
import datadog.trace.util.throwable.FatalAgentMisconfigurationError;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -1203,9 +1205,18 @@ 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);
// MIKAYLA: Where to write tests for this?
if (featureEnabled == null) {
featureEnabled =
getStableConfig(featureEnabledSysprop, StableConfigSource.MANAGED_STABLE_CONFIG_PATH);
}
if (featureEnabled == null) {
featureEnabled = ddGetEnv(featureEnabledSysprop);
}
if (featureEnabled == null) {
featureEnabled =
getStableConfig(featureEnabledSysprop, StableConfigSource.USER_STABLE_CONFIG_PATH);
}

if (feature.isEnabledByDefault()) {
// true unless it's explicitly set to "false"
Expand All @@ -1215,6 +1226,7 @@ private static boolean isFeatureEnabled(AgentFeature feature) {
// We need this hack because profiling in SSI can receive 'auto' value in
// the enablement config
return ProfilingEnablement.of(featureEnabled).isActive();
// MIKAYLA: How does this order of precedence compete with stable config?
}
// false unless it's explicitly set to "true"
return Boolean.parseBoolean(featureEnabled) || "1".equals(featureEnabled);
Expand Down Expand Up @@ -1342,6 +1354,18 @@ private static String ddGetProperty(final String sysProp) {
return value;
}

private static String getStableConfig(final String sysProp, String filepath) {
try {
return StableConfigSource.findAndReturnEarly(
StableConfigSource.readYamlFromFile(filepath), toEnvVar(sysProp));
} catch (IOException e) {
log.error("Error reading from file: {}", e.getMessage());
} catch (Exception e) {
log.error("Error processing file: {}", e.getMessage());
}
return null;
}

/** 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
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 @@ -356,15 +356,23 @@ public static ConfigProvider createDefault() {
if (configProperties.isEmpty()) {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG),
new EnvironmentConfigSource(),
new OtelEnvironmentConfigSource(),
new StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG),
new CapturedEnvironmentConfigSource());
} 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 StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG),
new CapturedEnvironmentConfigSource());
}
}
Expand All @@ -378,16 +386,24 @@ 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 StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG),
new CapturedEnvironmentConfigSource());
} 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 StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG),
new CapturedEnvironmentConfigSource());
}
}
Expand All @@ -398,22 +414,31 @@ public static ConfigProvider withPropertiesOverride(Properties properties) {
loadConfigurationFile(
new ConfigProvider(
new SystemPropertiesConfigSource(),
// MIKAYLA: To add StableConfig?
new EnvironmentConfigSource(),
providedConfigSource));
if (configProperties.isEmpty()) {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG),
new EnvironmentConfigSource(),
providedConfigSource,
new OtelEnvironmentConfigSource(),
new StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG),
new CapturedEnvironmentConfigSource());
} else {
return new ConfigProvider(
providedConfigSource,
new SystemPropertiesConfigSource(),
new StableConfigSource(
StableConfigSource.MANAGED_STABLE_CONFIG_PATH, ConfigOrigin.MANAGED_STABLE_CONFIG),
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
new StableConfigSource(
StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG),
new CapturedEnvironmentConfigSource());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
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 {
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-apm-libraries/stable/application_monitoring.yaml ";

private static final Logger log = LoggerFactory.getLogger(StableConfigSource.class);

private final StableConfig config;
private final ConfigOrigin fileOrigin;

StableConfigSource(String file, ConfigOrigin origin) {
this.fileOrigin = origin;
this.config = newStableConfigFromFile(file);
}

private static StableConfig newStableConfigFromFile(String file) {
try {
HashMap<String, Object> data = readYamlFromFile(file);
return buildStableConfig(data);
} catch (IOException e) {
log.error("Error reading from file: {}", e.getMessage());
} catch (Exception e) {
log.error("Error processing file: {}", e.getMessage());
}
return new StableConfig();
}

/**
* Reads a YAML file from the specified file path and returns its contents as a HashMap.
*
* @param filePath The path to the YAML file to be read. It must be a valid path to an existing
* file.
* @return A HashMap<String, Object> containing the parsed data from the YAML file.
* @throws IOException If the specified file does not exist or cannot be accessed.
* @throws Exception If there is an error during YAML parsing, including invalid formatting or
* structure.
*/
public static HashMap<String, Object> readYamlFromFile(String filePath) throws Exception {
File file = new File(filePath);
if (!file.exists()) {
throw new IOException(
"Stable configuration file does not exist at the specified path: " + filePath);
}
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
InputStream input = Files.newInputStream(new File(filePath).toPath());
try {
return yaml.load(input);
} catch (Exception e) {
throw new Exception(
"YAML parsing error in stable config file " + filePath + ": " + e.getMessage(), e);
}
}

/**
* Creates a StableConfig object based on the provided data map.
*
* <p>This method extracts the "config_id" and "apm_configuration_default" values from the input
* data map. If "apm_configuration_default" is a valid HashMap, it processes the entries and
* constructs a new configuration map.
*
* @param data A HashMap<String, Object> containing the configuration data. It should contain keys
* such as "config_id" and "apm_configuration_default".
* @return A StableConfig object constructed with the "config_id" if available and, if applicable,
* the parsed configuration data.
*/
private static StableConfig buildStableConfig(HashMap<String, Object> data) {
if (data == null) {
return new StableConfig();
}

String configId = (String) data.get("config_id");
Object apmConfig = data.get("apm_configuration_default");

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

/**
* Searches for a specific key in the provided map and returns its associated value if found.
*
* <p>This method is designed to quickly check if a specific key exists within the
* "apm_configuration_default" section of the provided data map and returns its associated value
* as a string if found.
*
* @param data A HashMap<String, Object> containing the configuration data. The key
* "apm_configuration_default" should be present, containing a nested map.
* @param key The key to search for within the "apm_configuration_default" section.
* @return The value associated with the specified key as a String if found; otherwise, null.
*/
public static String findAndReturnEarly(HashMap<String, Object> data, String key) {
if (data == null) {
return null;
}
Object apmConfig = data.get("apm_configuration_default");
if (apmConfig instanceof HashMap<?, ?>) {
HashMap<?, ?> tempConfig = (HashMap<?, ?>) apmConfig;
for (Map.Entry<?, ?> entry : tempConfig.entrySet()) {
if (entry.getKey() == key) {
return (String) entry.getValue();
}
}
}
return null;
}

@Override
public String get(String key) {
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();
}

private static class StableConfig {
private final Map<String, Object> apmConfiguration;
private final String configId;

StableConfig() {
this.apmConfiguration = new HashMap<>();
this.configId = null;
}

StableConfig(String configId) {
this.apmConfiguration = new HashMap<>();
this.configId = configId;
}

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

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

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

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