Skip to content

Commit dc449e0

Browse files
committed
Initial tests for StableConfigSource
1 parent c512b78 commit dc449e0

File tree

3 files changed

+125
-26
lines changed

3 files changed

+125
-26
lines changed

internal-api/src/main/java/datadog/trace/api/ConfigOrigin.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public enum ConfigOrigin {
77
REMOTE("remote_config"),
88
/** configurations that are set through JVM properties */
99
JVM_PROP("jvm_prop"),
10-
/** configuration read in the stable config file, managed by customers */
11-
CUSTOMER_STABLE_CONFIG("customer_stable_config"),
10+
/** configuration read in the stable config file, managed by users */
11+
USER_STABLE_CONFIG("user_stable_config"),
1212
/** configuration read in the stable config file, managed by fleet */
13-
FLEET_STABLE_CONFIG("fleet_stable_config"),
13+
MANAGED_STABLE_CONFIG("managed_stable_config"),
1414
/** set when the user has not set any configuration for the key (defaults to a value) */
1515
DEFAULT("default");
1616

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigSource.java

+54-23
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,85 @@
11
package datadog.trace.bootstrap.config.provider;
22

3-
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.InputStream;
6-
import java.util.HashMap;
7-
83
import static datadog.trace.util.Strings.propertyNameToEnvironmentVariableName;
94

105
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;
1415
import org.yaml.snakeyaml.LoaderOptions;
16+
import org.yaml.snakeyaml.Yaml;
1517
import org.yaml.snakeyaml.constructor.SafeConstructor;
1618

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);
2024

2125
final ConfigOrigin fileOrigin;
22-
HashMap<String, String> configuration;
26+
HashMap<String, Object> configuration;
2327

2428
StableConfigSource(String file, ConfigOrigin origin) {
2529
this.fileOrigin = origin;
2630
try {
2731
configuration = parseStableConfig(file);
2832
} catch (Exception e) {
29-
configuration = new HashMap();
33+
configuration = new HashMap<>();
3034
}
3135
}
3236

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+
3447
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);
3750

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;
4168
};
4269

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();
4576
}
4677

47-
public final ConfigOrigin origin() {
78+
public ConfigOrigin origin() {
4879
return fileOrigin;
4980
}
5081

51-
private class StableConfig {
82+
private static class StableConfig {
5283
private String config_id;
5384
private HashMap<String, Object> apm_configuration_default;
5485

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package datadog.trace.bootstrap.config.provider
2+
3+
import datadog.trace.api.ConfigOrigin
4+
import datadog.trace.test.util.DDSpecification
5+
import org.yaml.snakeyaml.DumperOptions
6+
import org.yaml.snakeyaml.Yaml
7+
8+
class StableConfigSourceTest extends DDSpecification {
9+
10+
def "test file doesn't exist"() {
11+
setup:
12+
StableConfigSource config = new StableConfigSource(StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG)
13+
14+
expect:
15+
config.getKeys().size() == 0
16+
// How to check that the "file does not exist" error was logged?
17+
}
18+
19+
def "test valid file"() {
20+
// test empty file
21+
when:
22+
def path = StableConfigSource.USER_STABLE_CONFIG_PATH
23+
try {
24+
File file = new File(path)
25+
file.createNewFile()
26+
} catch (IOException e) {
27+
// fail fast?
28+
System.out.println("Error creating file")
29+
e.printStackTrace()
30+
}
31+
StableConfigSource config = new StableConfigSource(path, ConfigOrigin.USER_STABLE_CONFIG)
32+
33+
then:
34+
config.getKeys().size() == 0
35+
36+
// test populated file
37+
// when:
38+
// def key1 = "dd_first_key"
39+
// def val1 = "dd_first_val"
40+
// def key2 = "dd_second_key"
41+
// def val2 = "dd_second_val"
42+
// // Create the map that will be used to populate the config file
43+
// Map<String, Object> data = new HashMap<>();
44+
// data.put("apm_configuration_default", new HashMap<String, Object>() {{
45+
// put(key1, val1);
46+
// put(key2, val2);
47+
// }})
48+
//
49+
// DumperOptions options = new DumperOptions();
50+
// options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
51+
//
52+
// // Prepare to write the data map to the file in yaml format
53+
// Yaml yaml = new Yaml(options);
54+
//
55+
// try (FileWriter writer = new FileWriter(path)) {
56+
// yaml.dump(data, writer);
57+
// } catch (IOException e) {
58+
// System.err.println("Error writing to file: " + e.getMessage());
59+
// // fail fast?
60+
// }
61+
//
62+
// then:
63+
// StableConfigSource config2 = new StableConfigSource(StableConfigSource.USER_STABLE_CONFIG_PATH, ConfigOrigin.USER_STABLE_CONFIG);
64+
// config2.getKeys().size() == 2
65+
// config2.get(key1) == val1
66+
// config2.get(key2) == val2
67+
}
68+
}

0 commit comments

Comments
 (0)