Skip to content

Commit c7b5ca4

Browse files
authored
Migration of the stable config from snakeyaml to snakeyaml-engine (#8790)
1 parent 9830e20 commit c7b5ca4

File tree

19 files changed

+272
-354
lines changed

19 files changed

+272
-354
lines changed

components/yaml/build.gradle.kts

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

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

7-
// https://repo1.maven.org/maven2/org/yaml/snakeyaml/2.4/snakeyaml-2.4.pom
87
dependencies {
9-
implementation("org.yaml", "snakeyaml", "2.4")
8+
implementation("org.snakeyaml", "snakeyaml-engine", "2.9")
109
}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
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+
/**
8+
* Parses YAML content. Duplicate keys are not allowed and will result in a runtime exception..
9+
*
10+
* @param content - text context to be parsed as YAML
11+
* @return - a parsed representation as a composition of map and list objects.
12+
*/
13+
public static Object parse(String content) {
14+
LoadSettings settings = LoadSettings.builder().build();
15+
Load yaml = new Load(settings);
16+
return yaml.loadFromString(content);
1417
}
1518
}

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/instrumentation/graal/native-image/src/main/java/datadog/trace/instrumentation/graal/nativeimage/ResourcesFeatureInstrumentation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public static void onExit() {
5050
tracerResources.add("profiling/jfr/overrides/minimal.jfp");
5151

5252
// jmxfetch configs
53-
tracerResources.add(
54-
"metrics/project.properties"); // org.datadog.jmxfetch.AppConfig reads its version
53+
tracerResources.add("metrics/project.properties");
5554
tracerResources.add("metrics/org/datadog/jmxfetch/default-jmx-metrics.yaml");
5655
tracerResources.add("metrics/org/datadog/jmxfetch/new-gc-default-jmx-metrics.yaml");
5756
tracerResources.add("metrics/org/datadog/jmxfetch/old-gc-default-jmx-metrics.yaml");

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 {

dd-smoke-tests/spring-boot-3.0-native/application/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ if (hasProperty('agentPath')) {
3939
if (withProfiler && property('profiler') == 'true') {
4040
buildArgs.add("-J-Ddd.profiling.enabled=true")
4141
}
42-
buildArgs.add("--enable-monitoring=jmxserver")
4342
jvmArgs.add("-Xmx3072M")
4443
}
4544
}

gradle/dependencies.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ final class CachedData {
5151
// cafe_crypto and its transitives
5252
exclude(dependency('cafe.cryptography::'))
5353

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

internal-api/build.gradle

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

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

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

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

3-
import datadog.trace.bootstrap.config.provider.stableconfigyaml.ConfigurationMap;
4-
import datadog.trace.bootstrap.config.provider.stableconfigyaml.Rule;
5-
import datadog.trace.bootstrap.config.provider.stableconfigyaml.Selector;
6-
import datadog.trace.bootstrap.config.provider.stableconfigyaml.StableConfigYaml;
3+
import datadog.trace.bootstrap.config.provider.stableconfig.Rule;
4+
import datadog.trace.bootstrap.config.provider.stableconfig.Selector;
5+
import datadog.trace.bootstrap.config.provider.stableconfig.StableConfig;
76
import datadog.yaml.YamlParser;
87
import java.io.IOException;
98
import java.nio.charset.StandardCharsets;
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,26 +41,28 @@ 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+
StableConfig data = new StableConfig(parsedYaml);
4546

46-
String configId = data.getConfig_id();
47-
ConfigurationMap configMap = data.getApm_configuration_default();
48-
List<Rule> rules = data.getApm_configuration_rules();
47+
String configId = data.getConfigId();
48+
Map<String, Object> configMap = data.getApmConfigurationDefault();
49+
List<Rule> rules = data.getApmConfigurationRules();
4950

5051
if (!rules.isEmpty()) {
5152
for (Rule rule : rules) {
5253
// Use the first matching rule
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(configId, configMap);
6466
}
6567

6668
// If there's a configId but no configMap, use configId but return an empty map
@@ -69,10 +71,7 @@ public static StableConfigSource.StableConfig parse(String filePath) throws IOEx
6971
}
7072

7173
} catch (IOException e) {
72-
log.debug(
73-
"Stable configuration file either not found or not readable at filepath {}. Error: {}",
74-
filePath,
75-
e.getMessage());
74+
log.debug("Failed to read the stable configuration file: {}", filePath, e);
7675
}
7776
return StableConfigSource.StableConfig.EMPTY;
7877
}
@@ -91,12 +90,6 @@ private static boolean doesRuleMatch(Rule rule) {
9190
return true; // Return true if all selectors match
9291
}
9392

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-
10093
private static boolean validOperatorForLanguageOrigin(String operator) {
10194
operator = operator.toLowerCase();
10295
// "exists" is not valid

0 commit comments

Comments
 (0)