Skip to content

Commit

Permalink
update to 0.17.1
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Feb 22, 2021
1 parent c9f5c52 commit edb3dd1
Show file tree
Hide file tree
Showing 19 changed files with 283 additions and 166 deletions.
6 changes: 3 additions & 3 deletions eng/versioning/external_dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ com.microsoft.azure:azure-storage;8.0.0
com.microsoft.azure:msal4j;1.8.1
com.microsoft.azure:msal4j-persistence-extension;1.0.0
com.sun.activation:jakarta.activation;1.2.2
io.opentelemetry:opentelemetry-api;0.14.1
io.opentelemetry:opentelemetry-sdk;0.14.1
io.opentelemetry:opentelemetry-semconv;0.14.1
io.opentelemetry:opentelemetry-api;0.17.1
io.opentelemetry:opentelemetry-sdk;0.17.1
com.google.errorprone:error_prone_annotations;2.5.1
io.projectreactor:reactor-test;3.3.12.RELEASE
junit:junit;4.13.1
commons-cli:commons-cli;1.3
Expand Down
1 change: 1 addition & 0 deletions eng/versioning/version_client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ com.azure.resourcemanager:azure-resourcemanager-digitaltwins;1.0.0-beta.1;1.0.0-
unreleased_com.azure:azure-messaging-eventhubs;5.5.0
unreleased_com.azure:azure-messaging-eventhubs-checkpointstore-blob;1.5.0
unreleased_com.azure:azure-core-management;1.2.0-beta.1
unreleased_com.azure:azure-core-tracing-opentelemetry;1.0.0-beta.8

# Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current
# version and set the version to the released beta. Released beta dependencies are only valid
Expand Down
7 changes: 4 additions & 3 deletions sdk/core/azure-core-tracing-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Release History

## 1.0.0-beta.8 (Unreleased)

- Updated versions of `opentelemetry-api` to `0.17.1` version.
More detailed information about the new OpenTelemetry API version can be found in [OpenTelemetry changelog](https://github.com/open-telemetry/opentelemetry-java/blob/main/CHANGELOG.md#version-0171---2021-02-19)

## 1.0.0-beta.7 (2021-02-05)

### Dependency Updates
- Updated versions of `opentelemetry-api` to `0.14.1` version.
More detailed information about the new OpenTelemetry API version can be found in [OpenTelemetry changelog](https://github.com/open-telemetry/opentelemetry-java/blob/master/CHANGELOG.md#version-0141---2021-01-14)
- Updated versions of `opentelemetry-api` to `0.15.0` version.
More detailed information about the new OpenTelemetry API version can be found in [OpenTelemetry changelog](https://github.com/open-telemetry/opentelemetry-java/blob/main/CHANGELOG.md#version-0150---2021-01-29)

## 1.0.0-beta.6 (2020-08-07)
- Update `opentelemetry-api` dependency version to `0.6.0` and included `io.grpc:grpc-context[1.30.0]` external
Expand Down
31 changes: 22 additions & 9 deletions sdk/core/azure-core-tracing-opentelemetry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,25 @@
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<version>0.14.1</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-api;external_dependency} -->
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>0.14.1</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-semconv;external_dependency} -->
<version>0.17.1</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-api;external_dependency} -->
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.14.0-beta.1</version> <!-- {x-version-update;com.azure:azure-core;current} -->
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<version>2.5.1</version> <!-- {x-version-update;com.google.errorprone:error_prone_annotations;external_dependency} -->
<scope>provided</scope>
</dependency>

<!--test dependencies -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>0.14.1</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-sdk;external_dependency} -->
<version>0.17.1</version> <!-- {x-version-update;io.opentelemetry:opentelemetry-sdk;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -85,8 +86,8 @@
<rules>
<bannedDependencies>
<includes>
<include>io.opentelemetry:opentelemetry-api:[0.14.1]</include> <!-- {x-include-update;io.opentelemetry:opentelemetry-api;external_dependency} -->
<include>io.opentelemetry:opentelemetry-semconv:[0.14.1]</include> <!-- {x-include-update;io.opentelemetry:opentelemetry-semconv;external_dependency} -->
<include>io.opentelemetry:opentelemetry-api:[0.17.1]</include> <!-- {x-include-update;io.opentelemetry:opentelemetry-api;external_dependency} -->
<include>com.google.errorprone:error_prone_annotations:[2.5.1]</include> <!-- {x-include-update;com.google.errorprone:error_prone_annotations;external_dependency} -->
</includes>
</bannedDependencies>
</rules>
Expand All @@ -103,6 +104,18 @@
</compilerArgs>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<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 causing failures in mocked tests. -->
<junit.jupiter.execution.parallel.enabled>false</junit.jupiter.execution.parallel.enabled>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentelemetry.context.propagation.TextMapSetter;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Signal;
import reactor.util.context.Context;
Expand Down Expand Up @@ -49,6 +50,10 @@ public HttpPipelinePolicy create() {
private static final Tracer TRACER = GlobalOpenTelemetry.getTracer("Azure-OpenTelemetry");

// standard attributes with http call information
private static final String HTTP_USER_AGENT = "http.user_agent";
private static final String HTTP_METHOD = "http.method";
private static final String HTTP_URL = "http.url";
private static final String HTTP_STATUS_CODE = "http.status_code";
private static final String REQUEST_ID = "x-ms-request-id";

// This helper class implements W3C distributed tracing protocol and injects SpanContext into the outgoing http
Expand All @@ -71,7 +76,7 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
.setParent(io.opentelemetry.context.Context.current().with(parentSpan));

// A span's kind can be SERVER (incoming request) or CLIENT (outgoing request);
spanBuilder.setSpanKind(Span.Kind.CLIENT);
spanBuilder.setSpanKind(SpanKind.CLIENT);

// Starting the span makes the sampling decision (nothing is logged at this time)
Span span = spanBuilder.startSpan();
Expand All @@ -95,21 +100,21 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN

private static void addSpanRequestAttributes(Span span, HttpRequest request,
HttpPipelineCallContext context) {
putAttributeIfNotEmptyOrNull(span, SemanticAttributes.HTTP_USER_AGENT,
putAttributeIfNotEmptyOrNull(span, HTTP_USER_AGENT,
request.getHeaders().getValue("User-Agent"));
putAttributeIfNotEmptyOrNull(span, SemanticAttributes.HTTP_METHOD, request.getHttpMethod().toString());
putAttributeIfNotEmptyOrNull(span, SemanticAttributes.HTTP_URL, request.getUrl().toString());
putAttributeIfNotEmptyOrNull(span, HTTP_METHOD, request.getHttpMethod().toString());
putAttributeIfNotEmptyOrNull(span, HTTP_URL, request.getUrl().toString());
Optional<Object> tracingNamespace = context.getData(AZ_TRACING_NAMESPACE_KEY);
if (tracingNamespace.isPresent()) {
putAttributeIfNotEmptyOrNull(span, AttributeKey.stringKey(OpenTelemetryTracer.AZ_NAMESPACE_KEY),
putAttributeIfNotEmptyOrNull(span, OpenTelemetryTracer.AZ_NAMESPACE_KEY,
tracingNamespace.get().toString());
}
}

private static void putAttributeIfNotEmptyOrNull(Span span, AttributeKey<String> key, String value) {
private static void putAttributeIfNotEmptyOrNull(Span span, String key, String value) {
// AttributeValue will throw an error if the value is null.
if (!CoreUtils.isNullOrEmpty(value)) {
span.setAttribute(key, value);
span.setAttribute(AttributeKey.stringKey(key), value);
}
}

Expand Down Expand Up @@ -163,8 +168,8 @@ private static void spanEnd(Span span, HttpResponse response, Throwable error) {
requestId = response.getHeaderValue(REQUEST_ID);
}

putAttributeIfNotEmptyOrNull(span, AttributeKey.stringKey(REQUEST_ID), requestId);
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, statusCode);
putAttributeIfNotEmptyOrNull(span, REQUEST_ID, requestId);
span.setAttribute(HTTP_STATUS_CODE, statusCode);
span = HttpTraceUtil.setSpanStatus(span, statusCode, error);
}

Expand All @@ -173,6 +178,6 @@ private static void spanEnd(Span span, HttpResponse response, Throwable error) {
}

// lambda that actually injects arbitrary header into the request
private final TextMapPropagator.Setter<HttpRequest> contextSetter =
private final TextMapSetter<HttpRequest> contextSetter =
(request, key, value) -> request.getHeaders().set(key, value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.Tracer;

import java.time.OffsetDateTime;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.MESSAGING_DESTINATION;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.PEER_SERVICE;

/**
* Basic tracing implementation class for use with REST and AMQP Service Clients to create {@link Span} and in-process
* context propagation. Singleton OpenTelemetry tracer capable of starting and exporting spans.
Expand All @@ -38,6 +36,8 @@ public class OpenTelemetryTracer implements com.azure.core.util.tracing.Tracer {
private static final Tracer TRACER = GlobalOpenTelemetry.getTracer("Azure-OpenTelemetry");
// standard attributes with AMQP request
static final String AZ_NAMESPACE_KEY = "az.namespace";
static final String MESSAGE_BUS_DESTINATION = "message_bus.destination";
static final String PEER_ENDPOINT = "peer.address";

private final ClientLogger logger = new ClientLogger(OpenTelemetryTracer.class);

Expand Down Expand Up @@ -79,15 +79,15 @@ public Context start(String spanName, Context context, ProcessKind processKind)
if (spanBuilder == null) {
return Context.NONE;
}
span = spanBuilder.setSpanKind(Span.Kind.CLIENT).startSpan();
span = spanBuilder.setSpanKind(SpanKind.CLIENT).startSpan();
if (span.isRecording()) {
// If span is sampled in, add additional request attributes
addSpanRequestAttributes(span, context, spanName);
}
return context.addData(PARENT_SPAN_KEY, span);
case MESSAGE:
spanBuilder = getSpanBuilder(spanName, context);
span = spanBuilder.setSpanKind(Span.Kind.PRODUCER).startSpan();
span = spanBuilder.setSpanKind(SpanKind.PRODUCER).startSpan();
if (span.isRecording()) {
// If span is sampled in, add additional request attributes
addSpanRequestAttributes(span, context, spanName);
Expand Down Expand Up @@ -266,7 +266,7 @@ private Context startScopedSpan(String spanName, Context context) {
span = startSpanWithRemoteParent(spanName, spanContext);
} else {
SpanBuilder spanBuilder = getSpanBuilder(spanName, context);
span = spanBuilder.setSpanKind(Span.Kind.CONSUMER).startSpan();
span = spanBuilder.setSpanKind(SpanKind.CONSUMER).startSpan();
}
if (span.isRecording()) {
// If span is sampled in, add additional request attributes
Expand All @@ -287,7 +287,7 @@ private static Span startSpanWithRemoteParent(String spanName, SpanContext spanC
SpanBuilder spanBuilder = TRACER.spanBuilder(spanName)
.setParent(io.opentelemetry.context.Context.root().with(Span.wrap(spanContext)));

spanBuilder.setSpanKind(Span.Kind.CONSUMER);
spanBuilder.setSpanKind(SpanKind.CONSUMER);
return spanBuilder.startSpan();
}

Expand All @@ -301,6 +301,9 @@ private static Span startSpanWithRemoteParent(String spanName, SpanContext spanC
private static Context setContextData(Span span) {
SpanContext spanContext = span.getSpanContext();
final String traceparent = AmqpPropagationFormatUtil.getDiagnosticId(spanContext);
if (traceparent == null) {
return Context.NONE;
}
return new Context(DIAGNOSTIC_ID_KEY, traceparent).addData(SPAN_CONTEXT_KEY, spanContext);
}

Expand All @@ -315,11 +318,11 @@ private void addSpanRequestAttributes(Span span, Context context, String spanNam
Objects.requireNonNull(span, "'span' cannot be null.");
String entityPath = getOrDefault(context, ENTITY_PATH_KEY, null, String.class);
if (entityPath != null) {
span.setAttribute(MESSAGING_DESTINATION, entityPath);
span.setAttribute(MESSAGE_BUS_DESTINATION, entityPath);
}
String hostName = getOrDefault(context, HOST_NAME_KEY, null, String.class);
if (hostName != null) {
span.setAttribute(PEER_SERVICE, hostName);
span.setAttribute(PEER_ENDPOINT, hostName);
}
Long messageEnqueuedTime = getOrDefault(context, MESSAGE_ENQUEUED_TIME, null, Long.class);
if (messageEnqueuedTime != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@

public final class AmqpPropagationFormatUtil {

private AmqpPropagationFormatUtil() { }
private static final String VERSION = "00";
private static final int VERSION_SIZE = 2;
private static final char TRACEPARENT_DELIMITER = '-';
private static final int TRACEPARENT_DELIMITER_SIZE = 1;
private static final int TRACE_ID_HEX_SIZE = TraceId.getLength();
private static final int SPAN_ID_HEX_SIZE = SpanId.getLength();
private static final int TRACE_OPTION_HEX_SIZE = TraceFlags.getLength();
private static final int TRACE_ID_OFFSET = VERSION_SIZE + TRACEPARENT_DELIMITER_SIZE;
private static final int SPAN_ID_OFFSET =
TRACE_ID_OFFSET + TRACE_ID_HEX_SIZE + TRACEPARENT_DELIMITER_SIZE;
private static final int TRACE_OPTION_OFFSET =
SPAN_ID_OFFSET + SPAN_ID_HEX_SIZE + TRACEPARENT_DELIMITER_SIZE;
private static final int TRACEPARENT_HEADER_SIZE = TRACE_OPTION_OFFSET + TRACE_OPTION_HEX_SIZE;

private AmqpPropagationFormatUtil() {
}

/**
* This method is called to extract the Span Context information from the received event's diagnostic Id.
Expand All @@ -30,57 +45,77 @@ public static Context extractContext(String diagnosticId, Context context) {
/**
* The trace-parent HTTP header field identifies the incoming request in a tracing system with four fields:
* version, trace-id, parent-id, trace-flags.
*
* <p>
* Please refer to the <a href=https://www.w3.org/TR/trace-context/#traceparent-header>Trace-parent Header</a>
* for more information on the conversion of these fields to Span Context format.
*
* @param spanContext is a specification defines an agreed-upon format for the exchange of trace context propagation
* data.
* @return The diagnostic Id providing an unique identifier for individual traces and requests,
* @return The diagnostic Id providing an unique identifier for individual traces and requests,
* allowing trace data of multiple providers to be linked together.
* @throws NullPointerException if {@code spanContext} is {@code null}.
*/
public static String getDiagnosticId(SpanContext spanContext) {
Objects.requireNonNull(spanContext, "'spanContext' cannot be null.");
final char[] chars = new char[55];
chars[0] = '0';
chars[1] = '0';
chars[2] = '-';
TraceId.copyHexInto(spanContext.getTraceIdBytes(), chars, 3);
chars[35] = '-';
String spanId = spanContext.getSpanIdAsHexString();
if (!spanContext.isValid()) {
return null;
}

char[] chars = new char[TRACEPARENT_HEADER_SIZE];
chars[0] = VERSION.charAt(0);
chars[1] = VERSION.charAt(1);
chars[2] = TRACEPARENT_DELIMITER;

String traceId = spanContext.getTraceId();
for (int i = 0; i < traceId.length(); i++) {
chars[TRACE_ID_OFFSET + i] = traceId.charAt(i);
}

chars[SPAN_ID_OFFSET - 1] = TRACEPARENT_DELIMITER;

String spanId = spanContext.getSpanId();
for (int i = 0; i < spanId.length(); i++) {
chars[36 + i] = spanId.charAt(i);
chars[SPAN_ID_OFFSET + i] = spanId.charAt(i);
}
chars[52] = '-';
spanContext.copyTraceFlagsHexTo(chars, 53);
return new String(chars);

chars[TRACE_OPTION_OFFSET - 1] = TRACEPARENT_DELIMITER;
String traceFlagsHex = spanContext.getTraceFlags().asHex();
chars[TRACE_OPTION_OFFSET] = traceFlagsHex.charAt(0);
chars[TRACE_OPTION_OFFSET + 1] = traceFlagsHex.charAt(1);
return new String(chars, 0, TRACEPARENT_HEADER_SIZE);
}

/**
* The trace-parent HTTP header field identifies the incoming request in a tracing system with four fields:
* version, trace-id, parent-id, trace-flags.
*
* <p>
* Please refer to the <a href=https://www.w3.org/TR/trace-context/#traceparent-header>Trace-parent Header</a>
* for more information on the conversion of these fields to Span Context format.
*
* @param diagnosticId provides a unique identifier for individual traces and requests,
* @param traceparent provides a unique identifier for individual traces and requests,
* @return SpanContext is a specification defines an agreed-upon format for the exchange of trace context
* propagation data
*/
private static SpanContext fromDiagnosticId(String diagnosticId) {
if (diagnosticId == null || diagnosticId.length() < 55 || !diagnosticId.startsWith("00")) {
private static SpanContext fromDiagnosticId(String traceparent) {
if (traceparent == null || traceparent.length() < 55 || !traceparent.startsWith(VERSION)) {
return SpanContext.create(
TraceId.getInvalid(),
SpanId.getInvalid(),
TraceFlags.getDefault(),
TraceState.getDefault()
);
}

String traceId =
traceparent.substring(TRACE_ID_OFFSET, TRACE_ID_OFFSET + TraceId.getLength());
String spanId = traceparent.substring(SPAN_ID_OFFSET, SPAN_ID_OFFSET + SpanId.getLength());

TraceFlags traceFlags = TraceFlags.fromHex(traceparent, TRACE_OPTION_OFFSET);

return SpanContext.create(
TraceId.bytesToHex(TraceId.bytesFromHex(diagnosticId, 3)),
SpanId.bytesToHex(SpanId.bytesFromHex(diagnosticId, 36)),
TraceFlags.byteFromHex(diagnosticId, 53),
traceId,
spanId,
traceFlags,
TraceState.builder().build());
}
}
Loading

0 comments on commit edb3dd1

Please sign in to comment.