-
Notifications
You must be signed in to change notification settings - Fork 304
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
Changes from 14 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
c512b78
Initial commit
paullegranddc dc449e0
Initial tests for StableConfigSource
mtoffl01 6d72f81
Add @Override for stableconfig get and configorigin methods
mtoffl01 92c81fd
apply github code qual suggestions
mtoffl01 635ba8a
passing initial tests
mtoffl01 d5bf963
support config_id
mtoffl01 9f03e04
Additional tests for invalid files
mtoffl01 06d572d
Introduce StableConfigSource to the list of ConfigProvider sources
mtoffl01 a03562c
add comments to current tests
mtoffl01 ac7b387
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 908a72d
fix typo
mtoffl01 abfc789
Merge branch 'mtoff/stable_config' of github.com:DataDog/dd-trace-jav…
mtoffl01 38355fe
Refactor: Introduce StableConfigSource sub-class, StableConfig
mtoffl01 aebebbe
nits: docs and cleanup
mtoffl01 90b52e9
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 cdd8df8
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 1fed1da
add doc for getStableConfig
mtoffl01 9f2b252
Merge branch 'mtoff/stable_config' of github.com:DataDog/dd-trace-jav…
mtoffl01 2da99f2
Merge branch 'master' into mtoff/stable_config
mtoffl01 ae6036b
Optimize: lazily load configuration information from file, cache results
mtoffl01 e3d6c80
nits: variable renaming, comments to describe variables
mtoffl01 a7bf5ba
nit: move comment above variable
mtoffl01 0c8ef6a
fix typo in managed file path
mtoffl01 049da8b
Initial StableConfigParser implementation and testing
mtoffl01 39fa0e2
Finalize StableConfigParser and tests
mtoffl01 53dec92
User parser in StableconfigSource; no more lazy loading
mtoffl01 96881d0
Add new logs for testing
mtoffl01 09e22a7
remove snakeyaml from dependency tree in internal-api
mtoffl01 519c443
Fix class initialization order
mcculls 9a519e1
update parser test to use raw file input as opposed to map
mtoffl01 a9f968f
make ParserTest handle unexpected input format as well
mtoffl01 7c93f15
Revert fileretries stuff
mtoffl01 36a7e8a
Change managed path to reflect system tests
mtoffl01 9598e11
remove superfluous comments/logs used for testing
mtoffl01 bf9efad
Merge branch 'master' into mtoff/stable_config
mtoffl01 a34a546
snakeyaml is only used for testing now
mcculls a3be88d
Register StableConfigSource with native-image since we need it at bui…
mcculls 99cb07b
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 cf99c4f
Address PR comments
mtoffl01 3d1a1d8
Change javadoc on getStableConfig function
mtoffl01 41fd3bc
Update dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/boot…
mtoffl01 9dd51fe
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 f143632
Update internal-api/src/main/java/datadog/trace/bootstrap/config/prov…
mtoffl01 ea8d9f7
Apply PR suggestions round 2
mtoffl01 4366cab
Apply PR suggestions round 3
mtoffl01 eb7618b
Merge branch 'master' into mtoff/stable_config
mtoffl01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static StableConfig newStableConfigFromFile(String file) { | ||
try { | ||
HashMap<String, Object> data = readYamlFromFile(file); | ||
return buildStableConfig(data); | ||
} catch (IOException e) { | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 { | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)); | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.