|
6 | 6 | package io.opentelemetry.contrib.awsxray; |
7 | 7 |
|
8 | 8 | import static org.assertj.core.api.Assertions.assertThat; |
9 | | -import static org.assertj.core.api.InstanceOfAssertFactories.type; |
10 | | -import static org.mockito.Mockito.when; |
11 | 9 |
|
12 | | -import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
13 | | -import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider; |
14 | | -import java.util.Collections; |
15 | | -import java.util.ServiceLoader; |
| 10 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 11 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 12 | +import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
16 | 15 | import org.junit.jupiter.api.Test; |
17 | 16 | import org.junit.jupiter.api.extension.ExtendWith; |
18 | | -import org.mockito.Mock; |
19 | 17 | import org.mockito.junit.jupiter.MockitoExtension; |
20 | 18 |
|
21 | 19 | @ExtendWith(MockitoExtension.class) |
22 | 20 | class AwsXrayRemoteSamplerProviderTest { |
23 | 21 |
|
24 | | - @Mock private ConfigProperties config; |
25 | | - |
26 | | - @Test |
27 | | - void serviceProvider() { |
28 | | - ServiceLoader<ConfigurableSamplerProvider> samplerProviders = |
29 | | - ServiceLoader.load(ConfigurableSamplerProvider.class); |
30 | | - assertThat(samplerProviders) |
31 | | - .singleElement(type(AwsXrayRemoteSamplerProvider.class)) |
32 | | - .satisfies( |
33 | | - provider -> { |
34 | | - assertThat(provider.getName()).isEqualTo("xray"); |
35 | | - }); |
36 | | - } |
37 | | - |
38 | 22 | @Test |
39 | | - void emptyConfig() { |
40 | | - try (AwsXrayRemoteSampler sampler = |
41 | | - (AwsXrayRemoteSampler) new AwsXrayRemoteSamplerProvider().createSampler(config)) { |
| 23 | + void serviceNameOnly() { |
| 24 | + Map<String, String> props = new HashMap<>(); |
| 25 | + props.put("otel.traces.sampler", "xray"); |
| 26 | + props.put("otel.service.name", "cat-service"); |
| 27 | + props.put("otel.traces.exporter", "none"); |
| 28 | + props.put("otel.metrics.exporter", "none"); |
| 29 | + try (SdkTracerProvider tracerProvider = |
| 30 | + AutoConfiguredOpenTelemetrySdk.builder() |
| 31 | + .addPropertiesSupplier(() -> props) |
| 32 | + .setResultAsGlobal(false) |
| 33 | + .build() |
| 34 | + .getOpenTelemetrySdk() |
| 35 | + .getSdkTracerProvider()) { |
42 | 36 | // Inspect implementation details for simplicity, otherwise we'd probably need to make a |
43 | 37 | // test HTTP server that records requests. |
44 | | - assertThat(sampler) |
45 | | - .extracting("client") |
46 | | - .extracting("getSamplingRulesEndpoint", type(String.class)) |
47 | | - .isEqualTo("http://localhost:2000/GetSamplingRules"); |
| 38 | + assertThat(tracerProvider) |
| 39 | + .extracting("sharedState") |
| 40 | + .extracting("sampler") |
| 41 | + .isInstanceOfSatisfying( |
| 42 | + AwsXrayRemoteSampler.class, |
| 43 | + sampler -> { |
| 44 | + assertThat(sampler.getClient().getSamplingRulesEndpoint()) |
| 45 | + .isEqualTo("http://localhost:2000/GetSamplingRules"); |
| 46 | + assertThat(sampler.getResource().getAttribute(ResourceAttributes.SERVICE_NAME)) |
| 47 | + .isEqualTo("cat-service"); |
| 48 | + }); |
48 | 49 | } |
49 | 50 | } |
50 | 51 |
|
51 | 52 | @Test |
52 | 53 | void setEndpoint() { |
53 | | - when(config.getMap("otel.resource.attributes")).thenReturn(Collections.emptyMap()); |
54 | | - when(config.getMap("otel.traces.sampler.arg")) |
55 | | - .thenReturn(Collections.singletonMap("endpoint", "http://localhost:3000")); |
56 | | - try (AwsXrayRemoteSampler sampler = |
57 | | - (AwsXrayRemoteSampler) new AwsXrayRemoteSamplerProvider().createSampler(config)) { |
| 54 | + Map<String, String> props = new HashMap<>(); |
| 55 | + props.put("otel.traces.sampler", "xray"); |
| 56 | + props.put("otel.traces.sampler.arg", "endpoint=http://localhost:3000"); |
| 57 | + props.put("otel.service.name", "cat-service"); |
| 58 | + props.put("otel.traces.exporter", "none"); |
| 59 | + props.put("otel.metrics.exporter", "none"); |
| 60 | + try (SdkTracerProvider tracerProvider = |
| 61 | + AutoConfiguredOpenTelemetrySdk.builder() |
| 62 | + .addPropertiesSupplier(() -> props) |
| 63 | + .setResultAsGlobal(false) |
| 64 | + .build() |
| 65 | + .getOpenTelemetrySdk() |
| 66 | + .getSdkTracerProvider()) { |
58 | 67 | // Inspect implementation details for simplicity, otherwise we'd probably need to make a |
59 | 68 | // test HTTP server that records requests. |
60 | | - assertThat(sampler) |
61 | | - .extracting("client") |
62 | | - .extracting("getSamplingRulesEndpoint", type(String.class)) |
63 | | - .isEqualTo("http://localhost:3000/GetSamplingRules"); |
| 69 | + assertThat(tracerProvider) |
| 70 | + .extracting("sharedState") |
| 71 | + .extracting("sampler") |
| 72 | + .isInstanceOfSatisfying( |
| 73 | + AwsXrayRemoteSampler.class, |
| 74 | + sampler -> { |
| 75 | + assertThat(sampler.getClient().getSamplingRulesEndpoint()) |
| 76 | + .isEqualTo("http://localhost:3000/GetSamplingRules"); |
| 77 | + assertThat(sampler.getResource().getAttribute(ResourceAttributes.SERVICE_NAME)) |
| 78 | + .isEqualTo("cat-service"); |
| 79 | + }); |
64 | 80 | } |
65 | 81 | } |
66 | 82 | } |
0 commit comments