Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable running tracing tests in parallel #24253

Merged
merged 8 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions sdk/core/azure-core-tracing-opentelemetry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<version>1.0.0</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-sdk;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.8.0-beta.1</version> <!-- {x-version-update;com.azure:azure-core-test;current} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
Expand Down Expand Up @@ -162,10 +168,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
<configuration>
<systemPropertyVariables>
<!-- Parallel runs disabled due to concurrency issues when registering the global tracer. -->
<junit.jupiter.execution.parallel.enabled>false</junit.jupiter.execution.parallel.enabled>
</systemPropertyVariables>
<parallel>all</parallel>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This configuration actually doesn't do anything when using JUnit 5, so the whole plugin could be removed

</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,26 @@ public HttpPipelinePolicy create() {
return this;
}

// Singleton OpenTelemetry tracer capable of starting and exporting spans.
private static final Tracer TRACER = GlobalOpenTelemetry.getTracer("Azure-OpenTelemetry");
// OpenTelemetry tracer capable of starting and exporting spans.
private final Tracer tracer;

/**
* Creates new OpenTelemetry {@link HttpPipelinePolicy} with default
* {@link GlobalOpenTelemetry#getTracer(String) global tracer}
*/
public OpenTelemetryHttpPolicy() {
this(GlobalOpenTelemetry.getTracer("Azure-OpenTelemetry"));
}

/**
* Creates new {@link OpenTelemetryHttpPolicy} that uses custom tracer.
* Use it for tests.
*
* @param tracer {@link io.opentelemetry.api.trace.Tracer} instance.
*/
OpenTelemetryHttpPolicy(Tracer tracer) {
lmolkova marked this conversation as resolved.
Show resolved Hide resolved
this.tracer = tracer;
}

// standard attributes with http call information
private static final String HTTP_USER_AGENT = "http.user_agent";
Expand All @@ -67,14 +85,15 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
return next.process();
}

io.opentelemetry.context.Context currentContext = io.opentelemetry.context.Context.current();
lmolkova marked this conversation as resolved.
Show resolved Hide resolved
Span parentSpan = (Span) context.getData(PARENT_SPAN_KEY).orElse(Span.current());
HttpRequest request = context.getHttpRequest();

// Build new child span representing this outgoing request.
final UrlBuilder urlBuilder = UrlBuilder.parse(context.getHttpRequest().getUrl());

SpanBuilder spanBuilder = TRACER.spanBuilder(urlBuilder.getPath())
.setParent(io.opentelemetry.context.Context.current().with(parentSpan));
SpanBuilder spanBuilder = tracer.spanBuilder(urlBuilder.getPath())
.setParent(currentContext.with(parentSpan));

// A span's kind can be SERVER (incoming request) or CLIENT (outgoing request);
spanBuilder.setSpanKind(SpanKind.CLIENT);
Expand All @@ -90,7 +109,7 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
// For no-op tracer, SpanContext is INVALID; inject valid span headers onto outgoing request
SpanContext spanContext = span.getSpanContext();
if (spanContext.isValid()) {
traceContextFormat.inject(io.opentelemetry.context.Context.current(), request, contextSetter);
traceContextFormat.inject(currentContext.with(span), request, contextSetter);
}

// run the next policy and handle success and error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,26 @@
*/
public class OpenTelemetryTracer implements com.azure.core.util.tracing.Tracer {

private final Tracer tracer = GlobalOpenTelemetry.getTracer("Azure-OpenTelemetry");
private final Tracer tracer;

/**
* Creates new {@link OpenTelemetryTracer} using default global tracer -
* {@link GlobalOpenTelemetry#getTracer(String)}
*
*/
public OpenTelemetryTracer() {
this(GlobalOpenTelemetry.getTracer("Azure-OpenTelemetry"));
}

/**
* Creates new {@link OpenTelemetryTracer} that wraps {@link io.opentelemetry.api.trace.Tracer}.
* Use it for tests.
*
* @param tracer {@link io.opentelemetry.api.trace.Tracer} instance.
*/
OpenTelemetryTracer(Tracer tracer) {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
this.tracer = tracer;
}

static final String AZ_NAMESPACE_KEY = "az.namespace";

Expand Down Expand Up @@ -285,14 +304,14 @@ private Context startSpanInternal(SpanBuilder spanBuilder,

/**
* Returns a {@link SpanBuilder} to create and start a new child {@link Span} with parent being the designated
* {@code Span}.
* {@link Span}.
*
* @param spanName The name of the returned Span.
* @param remoteParentContext Remote parent context if any, or {@code null} otherwise.
* @param spanKind Kind of the span to create.
* @param beforeSaplingAttributes Optional attributes available when span starts and important for sampling.
* @param context The context containing the span and the span name.
* @return A {@code Span.SpanBuilder} to create and start a new {@code Span}.
* @return A {@link SpanBuilder} to create and start a new {@link Span}.
*/
@SuppressWarnings("unchecked")
private SpanBuilder createSpanBuilder(String spanName,
Expand Down Expand Up @@ -419,7 +438,7 @@ private Context setDiagnosticId(Context context) {
}

/**
* Extracts request attributes from the given {@code context} and adds it to the started span.
* Extracts request attributes from the given {@link Context} and adds it to the started span.
*
* @param span The span to which request attributes are to be added.
* @param context The context containing the request attributes.
Expand All @@ -445,7 +464,7 @@ private void addMessagingAttributes(Span span, Context context) {
/**
* Returns the value of the specified key from the context.
*
* @param key The name of the attribute that needs to be extracted from the {@code Context}.
* @param key The name of the attribute that needs to be extracted from the {@link Context}.
* @param defaultValue the value to return in data not found.
* @param clazz clazz the type of raw class to find data for.
* @param context The context containing the specified key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,144 +3,154 @@

package com.azure.core.tracing.opentelemetry;

import com.azure.core.annotation.ExpectedResponses;
import com.azure.core.annotation.Get;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpMethod;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.policy.HttpPolicyProviders;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.tracing.opentelemetry.implementation.AmqpPropagationFormatUtil;
import com.azure.core.test.http.MockHttpResponse;
import com.azure.core.util.Context;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.azure.core.util.tracing.Tracer.AZ_TRACING_NAMESPACE_KEY;
import static com.azure.core.util.tracing.Tracer.PARENT_SPAN_KEY;
import static com.azure.core.util.tracing.Tracer.SPAN_CONTEXT_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Unit tests for {@link OpenTelemetryHttpPolicy}.
*/
public class OpenTelemetryHttpPolicyTests {

private static final String X_MS_REQUEST_ID = "response id";
private static final int RESPONSE_STATUS_CODE = 201;
private TestExporter exporter;
private Tracer tracer;

@BeforeEach
public void setUp(TestInfo testInfo) {
exporter = new TestExporter();
SdkTracerProvider otelProvider = SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(exporter)).build();

tracer = OpenTelemetrySdk.builder().setTracerProvider(otelProvider).build().getTracer(testInfo.getDisplayName());
}

@Test
public void addAfterPolicyTest() {
// Arrange & Act
final HttpPipeline pipeline = createHttpPipeline();
final List<HttpPipelinePolicy> policies = new ArrayList<>();
HttpPolicyProviders.addAfterRetryPolicies(policies);

// Assert
assertEquals(1, pipeline.getPolicyCount());
assertEquals(OpenTelemetryHttpPolicy.class, pipeline.getPolicy(0).getClass());
}

@Host("https://httpbin.org")
@ServiceInterface(name = "TestService")
interface TestService {
@Get("anything")
@ExpectedResponses({200})
HttpBinJSON getAnything(Context context);
assertEquals(1, policies.size());
assertEquals(OpenTelemetryHttpPolicy.class, policies.get(0).getClass());
}

@Test
public void openTelemetryHttpPolicyTest() {
// Arrange
// reset the global object before attempting to register
GlobalOpenTelemetry.resetForTest();
// Get the global singleton Tracer object.
Tracer tracer = OpenTelemetrySdk.builder().build().getTracer("TracerSdkTest");
// Start user parent span.
Span parentSpan = tracer.spanBuilder(PARENT_SPAN_KEY).startSpan();
Scope scope = parentSpan.makeCurrent();
// Add parent span to tracingContext
Context tracingContext = new Context(PARENT_SPAN_KEY, parentSpan);

Span expectedSpan = tracer
.spanBuilder("/anything")
.setParent(io.opentelemetry.context.Context.current().with(parentSpan))
.setSpanKind(SpanKind.CLIENT)
.startSpan();
// Add parent span to tracingContext
Context tracingContext = new Context(PARENT_SPAN_KEY, parentSpan)
.addData(AZ_TRACING_NAMESPACE_KEY, "foo");

// Act
HttpBinJSON response = RestProxy
.create(OpenTelemetryHttpPolicyTests.TestService.class, createHttpPipeline()).getAnything(tracingContext);
HttpRequest request = new HttpRequest(HttpMethod.GET, "https://httpbin.org/hello?there#otel");
request.setHeader("User-Agent", "user-agent");
HttpResponse response = createHttpPipeline(tracer).send(request, tracingContext).block();

// Assert
String diagnosticId = response.headers().get("Traceparent");
assertNotNull(diagnosticId);
SpanContext returnedSpanContext = getNonRemoteSpanContext(diagnosticId);
verifySpanContextAttributes(expectedSpan.getSpanContext(), returnedSpanContext);
scope.close();
List<SpanData> exportedSpans = exporter.getSpans();
// rest proxy span is not exported as global otel is not configured
assertEquals(1, exportedSpans.size());

SpanData httpSpan = exportedSpans.get(0);

assertEquals(request.getHeaders().getValue("Traceparent"), String.format("00-%s-%s-01", httpSpan.getTraceId(), httpSpan.getSpanId()));
assertEquals(((ReadableSpan) parentSpan).getSpanContext().getSpanId(), httpSpan.getParentSpanId());
assertEquals("/hello", httpSpan.getName());

Map<String, Object> httpAttributes = getAttributes(httpSpan);

assertEquals(6, httpAttributes.size());
assertEquals("https://httpbin.org/hello?there#otel", httpAttributes.get("http.url"));
assertEquals("GET", httpAttributes.get("http.method"));
assertEquals("user-agent", httpAttributes.get("http.user_agent"));
assertEquals("foo", httpAttributes.get(AZ_TRACING_NAMESPACE_KEY));
assertEquals(Long.valueOf(RESPONSE_STATUS_CODE), httpAttributes.get("http.status_code"));
assertEquals(X_MS_REQUEST_ID, httpAttributes.get("x-ms-request-id"));
}

private static HttpPipeline createHttpPipeline() {
final HttpClient httpClient = HttpClient.createDefault();
final List<HttpPipelinePolicy> policies = new ArrayList<>();
HttpPolicyProviders.addAfterRetryPolicies(policies);
private Map<String, Object> getAttributes(SpanData span) {
Map<String, Object> attributes = new HashMap<>();
span.getAttributes().forEach((k, v) -> attributes.put(k.getKey(), v));

return attributes;
}

private static HttpPipeline createHttpPipeline(Tracer tracer) {
final HttpPipeline httpPipeline = new HttpPipelineBuilder()
.httpClient(httpClient)
.policies(policies.toArray(new HttpPipelinePolicy[0]))
.httpClient(new SimpleMockHttpClient())
.policies(new OpenTelemetryHttpPolicy(tracer))
.build();
return httpPipeline;
}

private static SpanContext getNonRemoteSpanContext(String diagnosticId) {
Context updatedContext = AmqpPropagationFormatUtil.extractContext(diagnosticId, Context.NONE);
SpanContext spanContext = (SpanContext) updatedContext.getData(SPAN_CONTEXT_KEY).get();
return SpanContext.create(spanContext.getTraceId(), spanContext.getSpanId(),
spanContext.getTraceFlags(), spanContext.getTraceState());
}
private static class SimpleMockHttpClient implements HttpClient {

@Override
public Mono<HttpResponse> send(HttpRequest request) {
HttpHeaders headers = new HttpHeaders()
.set("x-ms-request-id", X_MS_REQUEST_ID);

private static void verifySpanContextAttributes(SpanContext expectedSpanContext, SpanContext actualSpanContext) {
assertEquals(expectedSpanContext.getTraceId(), actualSpanContext.getTraceId());
assertNotEquals(expectedSpanContext.getSpanId(), actualSpanContext.getSpanId());
assertEquals(expectedSpanContext.getTraceFlags(), actualSpanContext.getTraceFlags());
assertEquals(expectedSpanContext.getTraceState(), actualSpanContext.getTraceState());
assertEquals(expectedSpanContext.isValid(), actualSpanContext.isValid());
assertEquals(expectedSpanContext.isRemote(), actualSpanContext.isRemote());
return Mono.just(new MockHttpResponse(request, RESPONSE_STATUS_CODE, headers));
}
}

/**
* Maps to the JSON return values from http://httpbin.org.
*/
static class HttpBinJSON {

@JsonProperty()
private Map<String, String> headers;

/**
* Gets the response headers.
*
* @return The response headers.
*/
public Map<String, String> headers() {
return headers;
static class TestExporter implements SpanExporter {

private final List<SpanData> exportedSpans = new ArrayList<>();
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
exportedSpans.addAll(spans);
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode flush() {
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode shutdown() {
return CompletableResultCode.ofSuccess();
}

/**
* Sets the response headers.
*
* @param headers The response headers.
*/
public void headers(Map<String, String> headers) {
this.headers = headers;
public List<SpanData> getSpans() {
return exportedSpans;
}
}

Expand Down
Loading