Skip to content

Commit 6022d36

Browse files
authored
Wire up xray sampler. (#72)
* Wire up xray sampler. * Comment
1 parent e222330 commit 6022d36

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

awsagentprovider/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies {
2828
compileOnly("org.slf4j:slf4j-api")
2929

3030
implementation("io.opentelemetry:opentelemetry-sdk-extension-aws")
31+
implementation("io.opentelemetry.contrib:opentelemetry-aws-xray")
3132

3233
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
3334

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
package software.amazon.opentelemetry.javaagent.providers;
1717

18+
import io.opentelemetry.contrib.awsxray.AwsXrayIdGenerator;
1819
import io.opentelemetry.sdk.autoconfigure.spi.SdkTracerProviderConfigurer;
19-
import io.opentelemetry.sdk.extension.aws.trace.AwsXrayIdGenerator;
2020
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
2121

2222
public class AwsTracerConfigurer implements SdkTracerProviderConfigurer {

dependencyManagement/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ val DEPENDENCIES = listOf(
6666
"commons-logging:commons-logging:1.2",
6767
"com.sparkjava:spark-core:2.9.3",
6868
"com.squareup.okhttp3:okhttp:4.9.1",
69+
"io.opentelemetry.contrib:opentelemetry-aws-xray:1.4.0",
6970
"io.opentelemetry.javaagent:opentelemetry-javaagent:${if (!TEST_SNAPSHOTS) "1.4.0" else "1.5.0-SNAPSHOT"}",
7071
"net.bytebuddy:byte-buddy:1.11.0"
7172
)

smoke-tests/runner/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ project.evaluationDependsOn(":otelagent")
3636

3737
val otelAgentJarTask = project(":otelagent").tasks.named<Jar>("shadowJar")
3838
tasks {
39-
named<Test>("test") {
39+
withType<Test>().configureEach {
4040
dependsOn(otelAgentJarTask)
4141

4242
jvmArgs(

smoke-tests/runner/src/test/java/io/awsobservability/instrumentation/smoketests/runner/SpringBootSmokeTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ public ExportTraceServiceRequest deserialize(
119119
.withEnv("OTEL_BSP_SCHEDULE_DELAY", "10")
120120
.withEnv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://backend:8080");
121121

122+
@Container
123+
private static final GenericContainer<?> applicationXraySampler =
124+
new GenericContainer<>("public.ecr.aws/u0d6r4y4/aws-otel-java-smoketests-springboot:latest")
125+
.dependsOn(backend)
126+
.withExposedPorts(8080)
127+
.withNetwork(network)
128+
.withLogConsumer(new Slf4jLogConsumer(applicationLogger))
129+
.withCopyFileToContainer(
130+
MountableFile.forHostPath(AGENT_PATH), "/opentelemetry-javaagent-all.jar")
131+
.withEnv("JAVA_TOOL_OPTIONS", "-javaagent:/opentelemetry-javaagent-all.jar")
132+
.withEnv("OTEL_BSP_MAX_EXPORT_BATCH", "1")
133+
.withEnv("OTEL_BSP_SCHEDULE_DELAY", "10")
134+
.withEnv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://backend:8080")
135+
.withEnv("OTEL_TRACES_SAMPLER", "xray");
136+
122137
private static final TypeReference<List<ExportTraceServiceRequest>>
123138
EXPORT_TRACE_SERVICE_REQUEST_LIST = new TypeReference<>() {};
124139

@@ -186,6 +201,35 @@ void hello() {
186201
});
187202
}
188203

204+
@Test
205+
void defaultSamplingRate() {
206+
int numRequests = 100;
207+
for (int i = 0; i < numRequests; i++) {
208+
appClient.get("/hello").aggregate().join();
209+
}
210+
211+
var exported = getExported();
212+
// 5 spans per request (1 CLIENT, 2 SERVER, 2 INTERNAL)
213+
assertThat(exported).hasSize(numRequests * 5);
214+
}
215+
216+
@Test
217+
void remoteSamplingRate() {
218+
// We just want to make sure OTEL_TRACES_SAMPLER=xray is using the XRay sampler. It will fail to
219+
// fetch rules and fallback to the default, which will not sample all requests.
220+
WebClient client =
221+
WebClient.of("http://localhost:" + applicationXraySampler.getMappedPort(8080));
222+
int numRequests = 100;
223+
for (int i = 0; i < numRequests; i++) {
224+
client.get("/hello").aggregate().join();
225+
}
226+
227+
var exported = getExported();
228+
// 5 spans per request (1 CLIENT, 2 SERVER, 2 INTERNAL) if sampled. The default sampler is
229+
// around 5%, we do a very rough check that there are no more than 10% sampled.
230+
assertThat(exported).hasSizeLessThanOrEqualTo(numRequests * 5 / 10);
231+
}
232+
189233
private List<Span> getExported() {
190234
List<ExportTraceServiceRequest> exported = ImmutableList.of();
191235
for (int i = 0; i < 100; i++) {

0 commit comments

Comments
 (0)