|
1 | 1 | package datadog.trace.bootstrap.config.provider;
|
2 | 2 |
|
3 |
| -import java.io.File; |
4 |
| -import java.io.FileInputStream; |
5 |
| -import java.io.InputStream; |
6 |
| -import java.util.HashMap; |
7 |
| - |
8 | 3 | import static datadog.trace.util.Strings.propertyNameToEnvironmentVariableName;
|
9 | 4 |
|
10 | 5 | import datadog.trace.api.ConfigOrigin;
|
11 |
| -import datadog.trace.bootstrap.config.provider.ConfigProvider; |
12 |
| - |
13 |
| -import org.yaml.snakeyaml.Yaml; |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
14 | 15 | import org.yaml.snakeyaml.LoaderOptions;
|
| 16 | +import org.yaml.snakeyaml.Yaml; |
15 | 17 | import org.yaml.snakeyaml.constructor.SafeConstructor;
|
16 | 18 |
|
17 |
| -final public class StableConfigSource extends ConfigProvider.Source { |
18 |
| - static final String MANAGED_STABLE_CONFIGURATION_PATH = "/etc/datadog-agent/managed/datadog-agent/stable/datadog_apm.yaml"; |
19 |
| - static final String LOCAL_STABLE_CONFIGURATION_PATH = "/etc/datadog-agent/datadog_apm.yaml"; |
| 19 | +public final class StableConfigSource extends ConfigProvider.Source { |
| 20 | + static final String USER_STABLE_CONFIG_PATH = "/etc/datadog-agent/application_monitoring.yaml"; |
| 21 | + static final String MANAGED_STABLE_CONFIG_PATH = |
| 22 | + "/etc/datadog-agent/managed/datadog-apm-libraries/stable/application_monitoring.yaml "; |
| 23 | + private static final Logger log = LoggerFactory.getLogger(StableConfigSource.class); |
20 | 24 |
|
21 | 25 | final ConfigOrigin fileOrigin;
|
22 |
| - HashMap<String, String> configuration; |
| 26 | + HashMap<String, Object> configuration; |
23 | 27 |
|
24 | 28 | StableConfigSource(String file, ConfigOrigin origin) {
|
25 | 29 | this.fileOrigin = origin;
|
26 | 30 | try {
|
27 | 31 | configuration = parseStableConfig(file);
|
28 | 32 | } catch (Exception e) {
|
29 |
| - configuration = new HashMap(); |
| 33 | + configuration = new HashMap<>(); |
30 | 34 | }
|
31 | 35 | }
|
32 | 36 |
|
33 |
| - private static final HashMap<String, String> parseStableConfig(String filePath) { |
| 37 | + private static HashMap<String, Object> parseStableConfig(String filePath) throws IOException { |
| 38 | + HashMap<String, Object> config = new HashMap<>(); |
| 39 | + |
| 40 | + // Check if the file exists |
| 41 | + File file = new File(filePath); |
| 42 | + if (!file.exists()) { |
| 43 | + log.error("Stable configuration file does not exist at the specified path: {}", filePath); |
| 44 | + return config; // Exit early or take other action as necessary |
| 45 | + } |
| 46 | + |
34 | 47 | Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
|
35 |
| - InputStream input = new FileInputStream(new File(filePath)); |
36 |
| - Object data = yaml.load(input); |
| 48 | + InputStream input = Files.newInputStream(new File(filePath).toPath()); |
| 49 | + HashMap<String, Object> data = yaml.load(input); |
37 | 50 |
|
38 |
| - HashMap<Sting, Object> config = new HashMap(); |
39 |
| - |
40 |
| - return config; |
| 51 | + Object apmConfig = data.get("apm_configuration_default"); |
| 52 | + if (apmConfig instanceof HashMap<?, ?>) { |
| 53 | + HashMap<?, ?> tempConfig = (HashMap<?, ?>) apmConfig; |
| 54 | + for (Map.Entry<?, ?> entry : tempConfig.entrySet()) { |
| 55 | + if (entry.getKey() instanceof String && entry.getValue() != null) { |
| 56 | + String key = String.valueOf(entry.getKey()); |
| 57 | + Object value = entry.getValue(); |
| 58 | + config.put(key, value); |
| 59 | + } else { |
| 60 | + log.debug("Configuration {} in unexpected format", entry.getKey()); |
| 61 | + } |
| 62 | + } |
| 63 | + } else { |
| 64 | + // do something |
| 65 | + log.debug("File {} in unexpected format", filePath); |
| 66 | + } |
| 67 | + return config; |
41 | 68 | };
|
42 | 69 |
|
43 |
| - public final String get(String key) { |
44 |
| - return configuration.get(propertyNameToEnvironmentVariableName(key)); |
| 70 | + public String get(String key) { |
| 71 | + return (String) configuration.get(propertyNameToEnvironmentVariableName(key)); |
| 72 | + } |
| 73 | + |
| 74 | + public Set<String> getKeys() { |
| 75 | + return this.configuration.keySet(); |
45 | 76 | }
|
46 | 77 |
|
47 |
| - public final ConfigOrigin origin() { |
| 78 | + public ConfigOrigin origin() { |
48 | 79 | return fileOrigin;
|
49 | 80 | }
|
50 | 81 |
|
51 |
| - private class StableConfig { |
| 82 | + private static class StableConfig { |
52 | 83 | private String config_id;
|
53 | 84 | private HashMap<String, Object> apm_configuration_default;
|
54 | 85 |
|
|
0 commit comments