diff --git a/aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProvider.java b/aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProvider.java index 92d1f244d..9d43d79f6 100644 --- a/aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProvider.java +++ b/aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProvider.java @@ -9,13 +9,24 @@ import io.opentelemetry.sdk.autoconfigure.OpenTelemetrySdkAutoConfiguration; import io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSamplerProvider; import io.opentelemetry.sdk.trace.samplers.Sampler; +import java.util.Map; @AutoService(ConfigurableSamplerProvider.class) public class AwsXrayRemoteSamplerProvider implements ConfigurableSamplerProvider { @Override public Sampler createSampler(ConfigProperties config) { - return AwsXrayRemoteSampler.newBuilder(OpenTelemetrySdkAutoConfiguration.getResource()).build(); + AwsXrayRemoteSamplerBuilder builder = + AwsXrayRemoteSampler.newBuilder(OpenTelemetrySdkAutoConfiguration.getResource()); + + Map params = config.getCommaSeparatedMap("otel.traces.sampler.arg"); + + String endpoint = params.get("endpoint"); + if (endpoint != null) { + builder.setEndpoint(endpoint); + } + + return builder.build(); } @Override diff --git a/aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProviderTest.java b/aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProviderTest.java index 7edde5104..9d9113ae8 100644 --- a/aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProviderTest.java +++ b/aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerProviderTest.java @@ -6,19 +6,59 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.InstanceOfAssertFactories.type; +import static org.mockito.Mockito.when; +import io.opentelemetry.sdk.autoconfigure.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSamplerProvider; +import java.util.Collections; import java.util.ServiceLoader; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +@ExtendWith(MockitoExtension.class) class AwsXrayRemoteSamplerProviderTest { + @Mock private ConfigProperties config; + @Test void serviceProvider() { ServiceLoader samplerProviders = ServiceLoader.load(ConfigurableSamplerProvider.class); assertThat(samplerProviders) .singleElement(type(AwsXrayRemoteSamplerProvider.class)) - .satisfies(provider -> assertThat(provider.getName()).isEqualTo("xray")); + .satisfies( + provider -> { + assertThat(provider.getName()).isEqualTo("xray"); + }); + } + + @Test + void emptyConfig() { + try (AwsXrayRemoteSampler sampler = + (AwsXrayRemoteSampler) new AwsXrayRemoteSamplerProvider().createSampler(config)) { + // Inspect implementation details for simplicity, otherwise we'd probably need to make a + // test HTTP server that records requests. + assertThat(sampler) + .extracting("client") + .extracting("getSamplingRulesEndpoint", type(String.class)) + .isEqualTo("http://localhost:2000/GetSamplingRules"); + } + } + + @Test + void setEndpoint() { + when(config.getCommaSeparatedMap("otel.traces.sampler.arg")) + .thenReturn(Collections.singletonMap("endpoint", "http://localhost:3000")); + try (AwsXrayRemoteSampler sampler = + (AwsXrayRemoteSampler) new AwsXrayRemoteSamplerProvider().createSampler(config)) { + // Inspect implementation details for simplicity, otherwise we'd probably need to make a + // test HTTP server that records requests. + assertThat(sampler) + .extracting("client") + .extracting("getSamplingRulesEndpoint", type(String.class)) + .isEqualTo("http://localhost:3000/GetSamplingRules"); + } } } diff --git a/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts index 4d17c978d..af53db6b2 100644 --- a/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts @@ -85,6 +85,7 @@ dependencies { testImplementation("org.awaitility:awaitility") testImplementation("org.junit.jupiter:junit-jupiter-api") testImplementation("org.junit.jupiter:junit-jupiter-params") + testImplementation("org.mockito:mockito-junit-jupiter") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") testRuntimeOnly("org.junit.vintage:junit-vintage-engine") diff --git a/dependencyManagement/build.gradle.kts b/dependencyManagement/build.gradle.kts index 8de7148c9..9dc2d328a 100644 --- a/dependencyManagement/build.gradle.kts +++ b/dependencyManagement/build.gradle.kts @@ -38,6 +38,11 @@ val DEPENDENCY_SETS = listOf( "0.11.0", listOf("simpleclient", "simpleclient_common", "simpleclient_httpserver") ), + DependencySet( + "org.mockito", + "3.10.0", + listOf("mockito-core", "mockito-junit-jupiter") + ), DependencySet( "org.slf4j", "1.7.30",