Skip to content

Commit

Permalink
Allow configuring endpoint with environment variable. (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Agrawal authored Aug 9, 2021
1 parent e1050fa commit 9b9514e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> params = config.getCommaSeparatedMap("otel.traces.sampler.arg");

String endpoint = params.get("endpoint");
if (endpoint != null) {
builder.setEndpoint(endpoint);
}

return builder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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");
}
}
}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9b9514e

Please sign in to comment.