|
| 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 | +} |
0 commit comments