Allow configuring endpoint with environment variable. (#67)
This commit is contained in:
parent
e1050fae60
commit
9b9514e122
|
@ -9,13 +9,24 @@ import io.opentelemetry.sdk.autoconfigure.ConfigProperties;
|
|||
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<String, String> params = config.getCommaSeparatedMap("otel.traces.sampler.arg");
|
||||
|
||||
String endpoint = params.get("endpoint");
|
||||
if (endpoint != null) {
|
||||
builder.setEndpoint(endpoint);
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -6,19 +6,59 @@ package io.opentelemetry.contrib.awsxray;
|
|||
|
||||
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<ConfigurableSamplerProvider> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue