Skip to content

Commit 39a540b

Browse files
committed
Migrate from snakeyaml to snakeyaml-engine
1 parent d5d53cd commit 39a540b

File tree

16 files changed

+79
-120
lines changed

16 files changed

+79
-120
lines changed

components/cli/build.gradle.kts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
plugins {
2-
id("me.champeau.jmh")
3-
}
4-
51
apply(from = "$rootDir/gradle/java.gradle")
6-
7-
jmh {
8-
version = "1.28"
9-
}

components/context/build.gradle.kts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
plugins {
2-
id("me.champeau.jmh")
3-
}
4-
51
apply(from = "$rootDir/gradle/java.gradle")
62

7-
jmh {
8-
version = "1.28"
9-
}
10-
113
val excludedClassesInstructionCoverage by extra {
124
listOf("datadog.context.ContextProviders") // covered by forked test
135
}

components/yaml/build.gradle.kts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ plugins {
44

55
apply(from = "$rootDir/gradle/java.gradle")
66

7-
jmh {
8-
version = "1.28"
9-
}
10-
11-
// https://repo1.maven.org/maven2/org/yaml/snakeyaml/2.4/snakeyaml-2.4.pom
127
dependencies {
13-
implementation("org.yaml", "snakeyaml", "2.4")
8+
implementation("org.snakeyaml", "snakeyaml-engine", "2.9")
149
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package datadog.yaml;
22

3-
import org.yaml.snakeyaml.Yaml;
3+
import org.snakeyaml.engine.v2.api.Load;
4+
import org.snakeyaml.engine.v2.api.LoadSettings;
45

56
public class YamlParser {
6-
// Supports clazz == null for default yaml parsing
7-
public static <T> T parse(String content, Class<T> clazz) {
8-
Yaml yaml = new Yaml();
9-
if (clazz == null) {
10-
return yaml.load(content);
11-
} else {
12-
return yaml.loadAs(content, clazz);
13-
}
7+
public static Object parse(String content) {
8+
LoadSettings settings = LoadSettings.builder().setAllowDuplicateKeys(true).build();
9+
Load yaml = new Load(settings);
10+
return yaml.loadFromString(content);
1411
}
1512
}

dd-java-agent/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ext.generalShadowJarConfig = {
3838
// used to report our own dependencies, but we should remove the top-level metadata
3939
// of vendored packages because those could trigger unwanted framework checks.
4040
exclude '/META-INF/maven/org.slf4j/**'
41-
exclude '/META-INF/maven/org.yaml/**'
41+
exclude '/META-INF/maven/org.snakeyaml/**'
4242
exclude '**/META-INF/maven/**/pom.xml'
4343
exclude '**/META-INF/proguard/'
4444
exclude '**/META-INF/*.kotlin_module'
@@ -66,7 +66,7 @@ ext.generalShadowJarConfig = {
6666

6767
// Prevents conflict with other instances, but doesn't relocate instrumentation
6868
if (!projectName.equals('instrumentation')) {
69-
relocate 'org.yaml.snakeyaml', 'datadog.snakeyaml'
69+
relocate 'org.snakeyaml.engine', 'datadog.snakeyaml.engine'
7070
relocate 'okhttp3', 'datadog.okhttp3'
7171
relocate 'okio', 'datadog.okio'
7272
}

dd-java-agent/instrumentation/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ subprojects { Project subProj ->
6060
jdkCompile = "main_${name}Implementation"
6161
}
6262
configurations.muzzleBootstrap {
63-
exclude group: 'org.yaml', module : 'snakeyaml' // we vendor this in the agent jar
63+
exclude group: 'org.snakeyaml', module : 'snakeyaml-engine' // we vendor this in the agent jar
6464
}
6565
dependencies {
6666
// Apply common dependencies for instrumentation.

dd-java-agent/testing/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ excludedClassesCoverage += [
3939
]
4040

4141
configurations.api {
42-
exclude group: 'org.yaml', module: 'snakeyaml' // we vendor this in the agent jar
42+
exclude group: 'org.snakeyaml', module: 'snakeyaml-engine' // we vendor this in the agent jar
4343
}
4444

4545
dependencies {

gradle/dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final class CachedData {
5252
exclude(dependency('cafe.cryptography::'))
5353

5454
// snakeyaml and its transitives
55-
exclude(dependency('org.yaml:snakeyaml'))
55+
exclude(dependency('org.snakeyaml:snakeyaml'))
5656
}
5757
]
5858
}

internal-api/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ dependencies {
251251
// it contains annotations that are also present in the instrumented application classes
252252
api "com.datadoghq:dd-javac-plugin-client:0.2.2"
253253

254-
testImplementation("org.yaml:snakeyaml:2.4")
254+
testImplementation("org.snakeyaml:snakeyaml-engine:2.9")
255255
testImplementation project(":utils:test-utils")
256256
testImplementation("org.assertj:assertj-core:3.20.2")
257257
testImplementation libs.bundles.junit5

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package datadog.trace.bootstrap.config.provider;
22

3-
import datadog.trace.bootstrap.config.provider.stableconfigyaml.ConfigurationMap;
43
import datadog.trace.bootstrap.config.provider.stableconfigyaml.Rule;
54
import datadog.trace.bootstrap.config.provider.stableconfigyaml.Selector;
65
import datadog.trace.bootstrap.config.provider.stableconfigyaml.StableConfigYaml;
@@ -10,8 +9,9 @@
109
import java.nio.file.Files;
1110
import java.nio.file.Paths;
1211
import java.util.Collections;
13-
import java.util.HashMap;
12+
import java.util.LinkedHashMap;
1413
import java.util.List;
14+
import java.util.Map;
1515
import java.util.function.BiPredicate;
1616
import org.slf4j.Logger;
1717
import org.slf4j.LoggerFactory;
@@ -41,10 +41,11 @@ public static StableConfigSource.StableConfig parse(String filePath) throws IOEx
4141
try {
4242
String content = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
4343
String processedContent = processTemplate(content);
44-
StableConfigYaml data = YamlParser.parse(processedContent, StableConfigYaml.class);
44+
Object parsedYaml = YamlParser.parse(processedContent);
45+
StableConfigYaml data = new StableConfigYaml(parsedYaml);
4546

4647
String configId = data.getConfig_id();
47-
ConfigurationMap configMap = data.getApm_configuration_default();
48+
Map<String, Object> configMap = data.getApm_configuration_default();
4849
List<Rule> rules = data.getApm_configuration_rules();
4950

5051
if (!rules.isEmpty()) {
@@ -53,14 +54,16 @@ public static StableConfigSource.StableConfig parse(String filePath) throws IOEx
5354
if (doesRuleMatch(rule)) {
5455
// Merge configs found in apm_configuration_rules with those found in
5556
// apm_configuration_default
56-
configMap.putAll(rule.getConfiguration());
57-
return createStableConfig(configId, configMap);
57+
Map<String, Object> mergedConfigMap = new LinkedHashMap<>(configMap);
58+
mergedConfigMap.putAll(rule.getConfiguration());
59+
return new StableConfigSource.StableConfig(configId, mergedConfigMap);
5860
}
5961
}
6062
}
6163
// If configs were found in apm_configuration_default, use them
6264
if (!configMap.isEmpty()) {
63-
return createStableConfig(configId, configMap);
65+
return new StableConfigSource.StableConfig(
66+
configId, new LinkedHashMap<String, Object>(configMap));
6467
}
6568

6669
// If there's a configId but no configMap, use configId but return an empty map
@@ -91,12 +94,6 @@ private static boolean doesRuleMatch(Rule rule) {
9194
return true; // Return true if all selectors match
9295
}
9396

94-
/** Creates a StableConfig object from the provided configId and configMap. */
95-
private static StableConfigSource.StableConfig createStableConfig(
96-
String configId, ConfigurationMap configMap) {
97-
return new StableConfigSource.StableConfig(configId, new HashMap<>(configMap));
98-
}
99-
10097
private static boolean validOperatorForLanguageOrigin(String operator) {
10198
operator = operator.toLowerCase();
10299
// "exists" is not valid

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

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
package datadog.trace.bootstrap.config.provider.stableconfigyaml;
22

33
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
45
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Objects;
8+
import java.util.stream.Collectors;
59

610
// Rule represents a set of selectors and their corresponding configurations found in stable
711
// configuration files
812
public class Rule {
913
private List<Selector> selectors;
10-
private ConfigurationMap configuration;
14+
private Map<String, Object> configuration;
1115

1216
public Rule() {
1317
this.selectors = new ArrayList<>();
14-
this.configuration = new ConfigurationMap();
18+
this.configuration = new LinkedHashMap<>();
1519
}
1620

17-
public Rule(List<Selector> selectors, ConfigurationMap configuration) {
21+
public Rule(List<Selector> selectors, Map<String, Object> configuration) {
1822
this.selectors = selectors;
1923
this.configuration = configuration;
2024
}
2125

22-
// Getters and setters
23-
public List<Selector> getSelectors() {
24-
return selectors;
26+
public Rule(Object yaml) {
27+
Map map = (Map) yaml;
28+
selectors =
29+
((List<Object>) map.get("selectors"))
30+
.stream().filter(Objects::nonNull).map(Selector::new).collect(Collectors.toList());
31+
configuration = (Map<String, Object>) map.get("configuration");
2532
}
2633

27-
public void setSelectors(List<Selector> selectors) {
28-
this.selectors = selectors;
34+
public List<Selector> getSelectors() {
35+
return selectors;
2936
}
3037

31-
public ConfigurationMap getConfiguration() {
38+
public Map<String, Object> getConfiguration() {
3239
return configuration;
3340
}
34-
35-
public void setConfiguration(ConfigurationMap configuration) {
36-
this.configuration = configuration;
37-
}
3841
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package datadog.trace.bootstrap.config.provider.stableconfigyaml;
22

3-
import java.util.ArrayList;
43
import java.util.List;
4+
import java.util.Map;
55

66
public class Selector {
77
private String origin;
88
private String key;
99
private List<String> matches;
1010
private String operator;
1111

12-
public Selector() {
13-
this.origin = null;
14-
this.key = null;
15-
this.matches = new ArrayList<>();
16-
this.operator = null;
17-
}
18-
1912
public Selector(String origin, String key, List<String> matches, String operator) {
2013
this.origin = origin;
2114
this.key = key;
2215
this.matches = matches;
2316
this.operator = operator;
2417
}
2518

26-
// Getters and setters
19+
public Selector(Object yaml) {
20+
Map map = (Map) yaml;
21+
origin = (String) map.get("origin");
22+
key = (String) map.get("key");
23+
matches = (List<String>) map.get("matches");
24+
operator = (String) map.get("operator");
25+
}
26+
2727
public String getOrigin() {
2828
return origin;
2929
}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
package datadog.trace.bootstrap.config.provider.stableconfigyaml;
22

33
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
45
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
58

69
public class StableConfigYaml {
710
private String config_id; // optional
8-
private ConfigurationMap apm_configuration_default;
11+
private Map<String, Object> apm_configuration_default;
912
private List<Rule> apm_configuration_rules; // optional
1013

14+
public StableConfigYaml(Object yaml) {
15+
Map<Object, Object> map = (Map<Object, Object>) yaml;
16+
this.config_id = String.valueOf(map.get("config_id"));
17+
this.apm_configuration_default =
18+
(Map<String, Object>) map.getOrDefault("apm_configuration_default", new LinkedHashMap<>());
19+
this.apm_configuration_rules =
20+
((List<Object>) map.getOrDefault("apm_configuration_rules", new ArrayList<>()))
21+
.stream().map(Rule::new).collect(Collectors.toList());
22+
}
23+
1124
public StableConfigYaml() {
1225
this.config_id = null;
13-
this.apm_configuration_default = new ConfigurationMap();
26+
this.apm_configuration_default = new LinkedHashMap<>();
1427
this.apm_configuration_rules = new ArrayList<>();
1528
}
1629

17-
// Getters and setters
1830
public String getConfig_id() {
1931
return config_id;
2032
}
@@ -23,19 +35,15 @@ public void setConfig_id(String config_id) {
2335
this.config_id = config_id;
2436
}
2537

26-
public ConfigurationMap getApm_configuration_default() {
38+
public Map<String, Object> getApm_configuration_default() {
2739
return apm_configuration_default;
2840
}
2941

30-
public void setApm_configuration_default(ConfigurationMap apm_configuration_default) {
42+
public void setApm_configuration_default(Map<String, Object> apm_configuration_default) {
3143
this.apm_configuration_default = apm_configuration_default;
3244
}
3345

3446
public List<Rule> getApm_configuration_rules() {
3547
return apm_configuration_rules;
3648
}
37-
38-
public void setApm_configuration_rules(List<Rule> apm_configuration_rules) {
39-
this.apm_configuration_rules = apm_configuration_rules;
40-
}
4149
}

0 commit comments

Comments
 (0)