Skip to content

Commit 7634811

Browse files
committed
Add YAML config parsing from file
1 parent 9896041 commit 7634811

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/AwsApplicationSignalsCustomizerProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
import io.opentelemetry.sdk.trace.SpanProcessor;
4848
import io.opentelemetry.sdk.trace.export.SpanExporter;
4949
import io.opentelemetry.sdk.trace.samplers.Sampler;
50+
import java.io.IOException;
51+
import java.nio.charset.StandardCharsets;
52+
import java.nio.file.Files;
53+
import java.nio.file.Path;
54+
import java.nio.file.Paths;
5055
import java.time.Duration;
5156
import java.util.ArrayList;
5257
import java.util.Arrays;
@@ -487,6 +492,18 @@ static AwsXrayAdaptiveSamplingConfig parseConfigString(String config)
487492
if (config == null) {
488493
return null;
489494
}
495+
496+
// Check if the config is a file path and the file exists
497+
Path path = Paths.get(config);
498+
if (Files.exists(path)) {
499+
try {
500+
config = String.join("\n", Files.readAllLines(path, StandardCharsets.UTF_8));
501+
} catch (IOException e) {
502+
throw new IllegalArgumentException(
503+
"Failed to read adaptive sampling configuration file: " + e.getMessage(), e);
504+
}
505+
}
506+
490507
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
491508
Map<String, Object> configMap =
492509
yamlMapper.readValue(config, new TypeReference<Map<String, Object>>() {});

awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/AwsApplicationSignalsCustomizerProviderTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import static org.assertj.core.api.Assertions.assertThatNoException;
2121

2222
import com.fasterxml.jackson.core.JsonProcessingException;
23+
import io.opentelemetry.contrib.awsxray.AwsXrayAdaptiveSamplingConfig;
24+
import java.io.File;
25+
import java.net.URISyntaxException;
26+
import java.net.URL;
2327
import org.junit.jupiter.api.Test;
2428

2529
class AwsApplicationSignalsCustomizerProviderTest {
@@ -57,4 +61,42 @@ void setAdaptiveSamplingConfigFromString_invalidYaml() {
5761
AwsApplicationSignalsCustomizerProvider.parseConfigString(
5862
"{version: 1, invalid: yaml: structure}"));
5963
}
64+
65+
@Test
66+
void setAdaptiveSamplingConfigFromFile_validYaml()
67+
throws JsonProcessingException, URISyntaxException {
68+
// Get the resource file path
69+
URL resourceUrl =
70+
getClass().getClassLoader().getResource("adaptive-sampling-config-valid.yaml");
71+
assertThat(resourceUrl).isNotNull();
72+
73+
// Get the absolute file path
74+
File configFile = new File(resourceUrl.toURI());
75+
String absolutePath = configFile.getAbsolutePath();
76+
77+
// Parse the config using the file path
78+
AwsXrayAdaptiveSamplingConfig config =
79+
AwsApplicationSignalsCustomizerProvider.parseConfigString(absolutePath);
80+
81+
// Assert the configuration was parsed correctly
82+
assertThat(config).isNotNull();
83+
assertThat(config.getVersion()).isEqualTo(1);
84+
assertThat(config.getErrorCaptureLimit().getErrorSpansPerSecond()).isEqualTo(10);
85+
}
86+
87+
@Test
88+
void setAdaptiveSamplingConfigFromFile_invalidYaml() throws URISyntaxException {
89+
// Get the resource file path
90+
URL resourceUrl =
91+
getClass().getClassLoader().getResource("adaptive-sampling-config-invalid.yaml");
92+
assertThat(resourceUrl).isNotNull();
93+
94+
// Get the absolute file path
95+
File configFile = new File(resourceUrl.toURI());
96+
String absolutePath = configFile.getAbsolutePath();
97+
98+
// Parse the config using the file path
99+
assertThatException()
100+
.isThrownBy(() -> AwsApplicationSignalsCustomizerProvider.parseConfigString(absolutePath));
101+
}
60102
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 1.0
2+
anomalyConditions:
3+
- errorCodeRegex: "^5\\d\\d$"
4+
usage: both
5+
- errorCodeRegex: "^4\\d\\d$"
6+
usage: both
7+
- errorCodeRegex: "^3\\d\\d$"
8+
usage: both
9+
- errorCodeRegex: "^2\\d\\d$"
10+
operations: invalid part of config
11+
usage: both
12+
errorCaptureLimit:
13+
errorSpansPerSecond: 10
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 1.0
2+
anomalyConditions:
3+
- errorCodeRegex: "^5\\d\\d$"
4+
usage: both
5+
- errorCodeRegex: "^4\\d\\d$"
6+
usage: both
7+
- errorCodeRegex: "^3\\d\\d$"
8+
usage: both
9+
- errorCodeRegex: "^2\\d\\d$"
10+
usage: both
11+
errorCaptureLimit:
12+
errorSpansPerSecond: 10

0 commit comments

Comments
 (0)