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.OpenTelemetrySdkAutoConfiguration;
|
||||||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSamplerProvider;
|
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSamplerProvider;
|
||||||
import io.opentelemetry.sdk.trace.samplers.Sampler;
|
import io.opentelemetry.sdk.trace.samplers.Sampler;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@AutoService(ConfigurableSamplerProvider.class)
|
@AutoService(ConfigurableSamplerProvider.class)
|
||||||
public class AwsXrayRemoteSamplerProvider implements ConfigurableSamplerProvider {
|
public class AwsXrayRemoteSamplerProvider implements ConfigurableSamplerProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Sampler createSampler(ConfigProperties config) {
|
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
|
@Override
|
||||||
|
|
|
@ -6,19 +6,59 @@ package io.opentelemetry.contrib.awsxray;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.InstanceOfAssertFactories.type;
|
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 io.opentelemetry.sdk.autoconfigure.spi.ConfigurableSamplerProvider;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
import org.junit.jupiter.api.Test;
|
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 {
|
class AwsXrayRemoteSamplerProviderTest {
|
||||||
|
|
||||||
|
@Mock private ConfigProperties config;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void serviceProvider() {
|
void serviceProvider() {
|
||||||
ServiceLoader<ConfigurableSamplerProvider> samplerProviders =
|
ServiceLoader<ConfigurableSamplerProvider> samplerProviders =
|
||||||
ServiceLoader.load(ConfigurableSamplerProvider.class);
|
ServiceLoader.load(ConfigurableSamplerProvider.class);
|
||||||
assertThat(samplerProviders)
|
assertThat(samplerProviders)
|
||||||
.singleElement(type(AwsXrayRemoteSamplerProvider.class))
|
.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.awaitility:awaitility")
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter-api")
|
testImplementation("org.junit.jupiter:junit-jupiter-api")
|
||||||
testImplementation("org.junit.jupiter:junit-jupiter-params")
|
testImplementation("org.junit.jupiter:junit-jupiter-params")
|
||||||
|
testImplementation("org.mockito:mockito-junit-jupiter")
|
||||||
|
|
||||||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
|
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
|
||||||
testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
|
testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
|
||||||
|
|
|
@ -38,6 +38,11 @@ val DEPENDENCY_SETS = listOf(
|
||||||
"0.11.0",
|
"0.11.0",
|
||||||
listOf("simpleclient", "simpleclient_common", "simpleclient_httpserver")
|
listOf("simpleclient", "simpleclient_common", "simpleclient_httpserver")
|
||||||
),
|
),
|
||||||
|
DependencySet(
|
||||||
|
"org.mockito",
|
||||||
|
"3.10.0",
|
||||||
|
listOf("mockito-core", "mockito-junit-jupiter")
|
||||||
|
),
|
||||||
DependencySet(
|
DependencySet(
|
||||||
"org.slf4j",
|
"org.slf4j",
|
||||||
"1.7.30",
|
"1.7.30",
|
||||||
|
|
Loading…
Reference in New Issue