|
7 | 7 |
|
8 | 8 | import static org.assertj.core.api.Assertions.assertThat;
|
9 | 9 |
|
| 10 | +import com.azure.core.annotation.ExpectedResponses; |
| 11 | +import com.azure.core.annotation.Get; |
| 12 | +import com.azure.core.annotation.Host; |
| 13 | +import com.azure.core.annotation.ServiceInterface; |
| 14 | +import com.azure.core.http.HttpClient; |
| 15 | +import com.azure.core.http.HttpPipeline; |
| 16 | +import com.azure.core.http.HttpPipelineBuilder; |
| 17 | +import com.azure.core.http.policy.HttpPipelinePolicy; |
| 18 | +import com.azure.core.http.policy.HttpPolicyProviders; |
| 19 | +import com.azure.core.http.rest.Response; |
| 20 | +import com.azure.core.http.rest.RestProxy; |
| 21 | +import com.azure.core.test.http.MockHttpResponse; |
| 22 | +import com.azure.core.util.ClientOptions; |
10 | 23 | import com.azure.core.util.Context;
|
| 24 | +import com.azure.core.util.TracingOptions; |
11 | 25 | import io.opentelemetry.api.common.Attributes;
|
12 | 26 | import io.opentelemetry.api.trace.SpanKind;
|
| 27 | +import io.opentelemetry.instrumentation.api.internal.SpanKey; |
13 | 28 | import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
|
14 | 29 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
|
15 | 30 | import io.opentelemetry.sdk.trace.data.StatusData;
|
| 31 | +import io.opentelemetry.semconv.HttpAttributes; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
16 | 35 | import org.junit.jupiter.api.Test;
|
17 | 36 | import org.junit.jupiter.api.extension.RegisterExtension;
|
| 37 | +import reactor.core.publisher.Mono; |
| 38 | +import reactor.test.StepVerifier; |
18 | 39 |
|
19 | 40 | class AzureSdkTest {
|
20 | 41 |
|
@@ -48,9 +69,94 @@ void testSpan() {
|
48 | 69 | .hasAttributesSatisfying(Attributes::isEmpty)));
|
49 | 70 | }
|
50 | 71 |
|
| 72 | + @Test |
| 73 | + void testPipelineAndSuppression() { |
| 74 | + AtomicBoolean hasClientAndHttpKeys = new AtomicBoolean(false); |
| 75 | + |
| 76 | + HttpClient mockClient = |
| 77 | + request -> |
| 78 | + Mono.defer( |
| 79 | + () -> { |
| 80 | + // check if suppression is working |
| 81 | + hasClientAndHttpKeys.set(hasClientAndHttpSpans()); |
| 82 | + return Mono.just(new MockHttpResponse(request, 200)); |
| 83 | + }); |
| 84 | + |
| 85 | + StepVerifier.create(createService(mockClient, true).testMethod()) |
| 86 | + .expectNextCount(1) |
| 87 | + .expectComplete() |
| 88 | + .verify(); |
| 89 | + |
| 90 | + assertThat(hasClientAndHttpKeys.get()).isTrue(); |
| 91 | + testing.waitAndAssertTracesWithoutScopeVersionVerification( |
| 92 | + trace -> |
| 93 | + trace.hasSpansSatisfyingExactly( |
| 94 | + span -> |
| 95 | + span.hasName("myService.testMethod") |
| 96 | + .hasKind(SpanKind.INTERNAL) |
| 97 | + .hasStatus(StatusData.unset()) |
| 98 | + .hasAttributes(Attributes.empty()), |
| 99 | + span -> |
| 100 | + span.hasKind(SpanKind.CLIENT) |
| 101 | + .hasName(Boolean.getBoolean("testLatestDeps") ? "GET" : "HTTP GET") |
| 102 | + .hasStatus(StatusData.unset()) |
| 103 | + .hasAttribute(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L))); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testDisabledTracingNoSuppression() { |
| 108 | + AtomicBoolean hasClientAndHttpKeys = new AtomicBoolean(false); |
| 109 | + |
| 110 | + HttpClient mockClient = |
| 111 | + request -> |
| 112 | + Mono.defer( |
| 113 | + () -> { |
| 114 | + // check no suppression |
| 115 | + hasClientAndHttpKeys.set(hasClientAndHttpSpans()); |
| 116 | + return Mono.just(new MockHttpResponse(request, 200)); |
| 117 | + }); |
| 118 | + |
| 119 | + StepVerifier.create(createService(mockClient, false).testMethod()) |
| 120 | + .expectNextCount(1) |
| 121 | + .expectComplete() |
| 122 | + .verify(); |
| 123 | + |
| 124 | + assertThat(hasClientAndHttpKeys.get()).isFalse(); |
| 125 | + } |
| 126 | + |
51 | 127 | private static com.azure.core.util.tracing.Tracer createAzTracer() {
|
52 | 128 | com.azure.core.util.tracing.TracerProvider azProvider =
|
53 | 129 | com.azure.core.util.tracing.TracerProvider.getDefaultProvider();
|
54 | 130 | return azProvider.createTracer("test-lib", "test-version", "otel.tests", null);
|
55 | 131 | }
|
| 132 | + |
| 133 | + private static TestInterface createService(HttpClient httpClient, boolean tracingEnabled) { |
| 134 | + List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 135 | + HttpPolicyProviders.addAfterRetryPolicies(policies); |
| 136 | + |
| 137 | + ClientOptions clientOptions = |
| 138 | + new ClientOptions().setTracingOptions(new TracingOptions().setEnabled(tracingEnabled)); |
| 139 | + HttpPipeline pipeline = |
| 140 | + new HttpPipelineBuilder() |
| 141 | + .policies(policies.toArray(new HttpPipelinePolicy[0])) |
| 142 | + .httpClient(httpClient) |
| 143 | + .clientOptions(clientOptions) |
| 144 | + .build(); |
| 145 | + |
| 146 | + return RestProxy.create(TestInterface.class, pipeline); |
| 147 | + } |
| 148 | + |
| 149 | + private static boolean hasClientAndHttpSpans() { |
| 150 | + io.opentelemetry.context.Context ctx = io.opentelemetry.context.Context.current(); |
| 151 | + return SpanKey.KIND_CLIENT.fromContextOrNull(ctx) != null |
| 152 | + && SpanKey.HTTP_CLIENT.fromContextOrNull(ctx) != null; |
| 153 | + } |
| 154 | + |
| 155 | + @Host("https://azure.com") |
| 156 | + @ServiceInterface(name = "myService") |
| 157 | + interface TestInterface { |
| 158 | + @Get("path") |
| 159 | + @ExpectedResponses({200}) |
| 160 | + Mono<Response<Void>> testMethod(); |
| 161 | + } |
56 | 162 | }
|
0 commit comments