-
Notifications
You must be signed in to change notification settings - Fork 303
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 8 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
120 changes: 120 additions & 0 deletions
120
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,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); | ||
} | ||
} | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* 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) { | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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) { | ||
mtoffl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
...api/src/test/groovy/datadog/trace/bootstrap/config/provider/StableConfigSourceTest.groovy
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,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) | ||
} | ||
} |
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.