Skip to content

Commit 84bef28

Browse files
author
Anuraag Agrawal
authored
Add manually run test code to exercise AWS X-Ray remote sampler end-to-end. (#56)
1 parent f2afb66 commit 84bef28

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

aws-xray/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
plugins {
22
id("otel.java-conventions")
3+
4+
id("org.unbroken-dome.test-sets")
35
}
46

57
description = "OpenTelemetry AWS X-Ray Support"
68

9+
testSets {
10+
create("awsTest")
11+
}
12+
713
dependencies {
814
api("io.opentelemetry:opentelemetry-api")
915
api("io.opentelemetry:opentelemetry-sdk-trace")
@@ -23,4 +29,7 @@ dependencies {
2329
testImplementation("com.google.guava:guava")
2430
testImplementation("org.slf4j:slf4j-simple")
2531
testImplementation("org.skyscreamer:jsonassert")
32+
33+
add("awsTestImplementation", "io.opentelemetry:opentelemetry-exporter-otlp-trace")
34+
add("awsTestImplementation", "org.testcontainers:junit-jupiter")
2635
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package io.opentelemetry.contrib.aws.xray;
6+
7+
import io.opentelemetry.api.common.Attributes;
8+
import io.opentelemetry.api.trace.SpanKind;
9+
import io.opentelemetry.context.Context;
10+
import io.opentelemetry.contrib.awsxray.AwsXrayRemoteSampler;
11+
import io.opentelemetry.sdk.resources.Resource;
12+
import io.opentelemetry.sdk.trace.IdGenerator;
13+
import java.time.Duration;
14+
import java.util.Collections;
15+
import org.junit.jupiter.api.Test;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
import org.testcontainers.containers.GenericContainer;
19+
import org.testcontainers.containers.output.Slf4jLogConsumer;
20+
import org.testcontainers.containers.wait.strategy.Wait;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
import org.testcontainers.utility.DockerImageName;
24+
import org.testcontainers.utility.MountableFile;
25+
26+
// We do not currently automate integration testing because it requires AWS credentials. If we were
27+
// to set up AWS credentials in CI in the future, this test could be improved to use the AWS API
28+
// to update sampling rules and assert rough ratios of sampling decisions. In the meantime, it
29+
// expects you to update the rules through the dashboard to see the effect on the sampling decisions
30+
// that are printed.
31+
@Testcontainers(disabledWithoutDocker = true)
32+
class AwsXrayRemoteSamplerIntegrationTest {
33+
34+
private static final Logger logger =
35+
LoggerFactory.getLogger(AwsXrayRemoteSamplerIntegrationTest.class);
36+
37+
@Container
38+
private static final GenericContainer<?> otelCollector =
39+
new GenericContainer<>(DockerImageName.parse("otel/opentelemetry-collector-contrib:latest"))
40+
.withExposedPorts(13133, 2000)
41+
.waitingFor(Wait.forHttp("/").forPort(13133))
42+
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("otel-collector")))
43+
.withCopyFileToContainer(
44+
MountableFile.forClasspathResource("/otel-collector.yml"), "/etc/otel-collector.yml")
45+
.withCommand("--config /etc/otel-collector.yml --log-level DEBUG")
46+
.withEnv("AWS_ACCESS_KEY_ID", System.getenv("AWS_ACCESS_KEY_ID"))
47+
.withEnv("AWS_SECRET_ACCESS_KEY", System.getenv("AWS_SECRET_ACCESS_KEY"))
48+
.withEnv("AWS_REGION", System.getenv("AWS_REGION"));
49+
50+
@Test
51+
void keepSampling() throws Exception {
52+
AwsXrayRemoteSampler sampler =
53+
AwsXrayRemoteSampler.newBuilder(Resource.getDefault())
54+
.setEndpoint("http://localhost:" + otelCollector.getMappedPort(2000))
55+
.setPollingInterval(Duration.ofSeconds(5))
56+
.build();
57+
58+
try {
59+
while (true) {
60+
logger.info(
61+
"Sampling Decision: {}",
62+
sampler
63+
.shouldSample(
64+
Context.root(),
65+
IdGenerator.random().generateTraceId(),
66+
"cat-service",
67+
SpanKind.SERVER,
68+
Attributes.empty(),
69+
Collections.emptyList())
70+
.getDecision());
71+
Thread.sleep(500);
72+
}
73+
} finally {
74+
sampler.close();
75+
}
76+
}
77+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
receivers:
2+
awsxray:
3+
4+
extensions:
5+
health_check:
6+
7+
exporters:
8+
logging:
9+
10+
service:
11+
extensions: [health_check]
12+
pipelines:
13+
traces:
14+
receivers: [awsxray]
15+
exporters: [logging]

dependencyManagement/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ val DEPENDENCY_BOMS = listOf(
1717
"org.junit:junit-bom:5.7.2",
1818
"com.linecorp.armeria:armeria-bom:1.9.1",
1919
"io.opentelemetry:opentelemetry-bom:1.4.1",
20-
"io.opentelemetry:opentelemetry-bom-alpha:1.4.1-alpha"
20+
"io.opentelemetry:opentelemetry-bom-alpha:1.4.1-alpha",
21+
"org.testcontainers:testcontainers-bom:1.16.0"
2122
)
2223

2324
val DEPENDENCY_SETS = listOf<DependencySet>(

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pluginManagement {
22
plugins {
33
id("com.github.ben-manes.versions") version "0.39.0"
44
id("com.github.johnrengelman.shadow") version "7.0.0"
5+
id("org.unbroken-dome.test-sets") version "4.0.0"
56
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
67
}
78
}

0 commit comments

Comments
 (0)