Skip to content

Commit

Permalink
Upgrade OpenTelemetry to 0.10.0. Update tracing.txt.
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Nov 23, 2020
1 parent 4975741 commit 057fd66
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 511 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ java_library(
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/remote/http",
artifact("io.opentelemetry:opentelemetry-api"),
artifact("io.opentelemetry:opentelemetry-context-prop"),
artifact("io.opentelemetry:opentelemetry-context"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ java_library(
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/remote/tracing",
artifact("com.google.guava:guava"),
artifact("io.grpc:grpc-context"),
artifact("io.opentelemetry:opentelemetry-api"),
artifact("io.opentelemetry:opentelemetry-context-prop"),
artifact("io.opentelemetry:opentelemetry-context"),
artifact("io.opentelemetry:opentelemetry-sdk"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
package org.openqa.selenium.remote.tracing.opentelemetry;

import com.google.common.annotations.VisibleForTesting;
import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.TracingContextUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.Tracer;

import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.TraceContext;
Expand All @@ -40,7 +39,7 @@ public OpenTelemetryContext(Tracer tracer, Context context) {
this.tracer = Require.nonNull("Tracer", tracer);
this.context = Require.nonNull("Context", context);

spanContext = TracingContextUtils.getSpan(context).getContext();
spanContext = Span.fromContext(context).getSpanContext();
}

@Override
Expand All @@ -57,7 +56,7 @@ public OpenTelemetrySpan createSpan(String name) {
Context prev = Context.current();

// Now update the context
Scope scope = tracer.withSpan(span);
Scope scope = span.makeCurrent();

if (prev.equals(Context.current())) {
throw new IllegalStateException("Context has not been changed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

package org.openqa.selenium.remote.tracing.opentelemetry;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.TracingContextUtils;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.Span;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.Propagator;
import org.openqa.selenium.remote.tracing.TraceContext;
Expand All @@ -45,8 +44,9 @@ public <C> void inject(TraceContext toInject, C carrier, Setter<C> setter) {
Require.nonNull("Setter", setter);
Require.argument("Trace context", toInject).instanceOf(OpenTelemetryContext.class);

httpTextFormat.inject(
((OpenTelemetryContext) toInject).getContext(), carrier, setter::set);
TextMapPropagator.Setter<C> propagatorSetter = setter::set;

httpTextFormat.inject(((OpenTelemetryContext) toInject).getContext(), carrier, propagatorSetter);
}

@Override
Expand All @@ -57,14 +57,27 @@ public <C> OpenTelemetryContext extractContext(
Require.nonNull("Getter", getter);
Require.argument("Trace context", existing).instanceOf(OpenTelemetryContext.class);

TextMapPropagator.Getter<C> propagatorGetter = new TextMapPropagator.Getter<C>() {

@Override
public Iterable<String> keys(C carrier) {
return null;
}

@Override
public String get(C carrier, String key) {
return getter.apply(carrier, key);
}
};

Context extracted =
httpTextFormat.extract(
((OpenTelemetryContext) existing).getContext(), carrier, getter::apply);
((OpenTelemetryContext) existing).getContext(), carrier, propagatorGetter);

// If the extracted context is the root context, then we continue to be a
// child span of the existing context.
String id = TracingContextUtils.getSpan(extracted).getContext().getSpanIdAsHexString();
if (DefaultSpan.getInvalid().getContext().getSpanIdAsHexString().equals(id)) {
String id = Span.fromContext(extracted).getSpanContext().getSpanIdAsHexString();
if (Span.getInvalid().getSpanContext().getSpanIdAsHexString().equals(id)) {
return (OpenTelemetryContext) existing;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Primitives;
import io.grpc.Context;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.StatusCanonicalCode;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.EventAttributeValue;
import org.openqa.selenium.remote.tracing.Span;
Expand All @@ -35,10 +35,10 @@

class OpenTelemetrySpan extends OpenTelemetryContext implements AutoCloseable, Span {

private final io.opentelemetry.trace.Span span;
private final io.opentelemetry.api.trace.Span span;
private final Scope scope;

public OpenTelemetrySpan(Tracer tracer, Context context, io.opentelemetry.trace.Span span, Scope scope) {
public OpenTelemetrySpan(Tracer tracer, Context context, io.opentelemetry.api.trace.Span span, Scope scope) {
super(tracer, context);
this.span = Require.nonNull("Span", span);
this.scope = Require.nonNull("Scope", scope);
Expand Down Expand Up @@ -90,42 +90,42 @@ public Span addEvent(String name) {
public Span addEvent(String name, Map<String, EventAttributeValue> attributeMap) {
Require.nonNull("Name", name);
Require.nonNull("Event Attribute Map", attributeMap);
Attributes.Builder otAttributes = Attributes.newBuilder();
Attributes.Builder otAttributes = Attributes.builder();

attributeMap.forEach(
(key, value) -> {
Require.nonNull("Event Attribute Value", value);
switch (value.getAttributeType()) {
case BOOLEAN:
otAttributes.setAttribute(key, value.getBooleanValue());
otAttributes.put(key, value.getBooleanValue());
break;

case BOOLEAN_ARRAY:
otAttributes.setAttribute(key, value.getBooleanArrayValue());
otAttributes.put(key, value.getBooleanArrayValue());
break;

case DOUBLE:
otAttributes.setAttribute(key, value.getNumberValue().doubleValue());
otAttributes.put(key, value.getNumberValue().doubleValue());
break;

case DOUBLE_ARRAY:
otAttributes.setAttribute(key, value.getDoubleArrayValue());
otAttributes.put(key, value.getDoubleArrayValue());
break;

case LONG:
otAttributes.setAttribute(key, value.getNumberValue().longValue());
otAttributes.put(key, value.getNumberValue().longValue());
break;

case LONG_ARRAY:
otAttributes.setAttribute(key, value.getLongArrayValue());
otAttributes.put(key, value.getLongArrayValue());
break;

case STRING:
otAttributes.setAttribute(key, value.getStringValue());
otAttributes.put(key, value.getStringValue());
break;

case STRING_ARRAY:
otAttributes.setAttribute(key, value.getStringArrayValue());
otAttributes.put(key, value.getStringArrayValue());
break;

default:
Expand All @@ -139,35 +139,35 @@ public Span addEvent(String name, Map<String, EventAttributeValue> attributeMap)
return this;
}

private static final Map<Status.Kind, StatusCanonicalCode> statuses
= new ImmutableMap.Builder<Status.Kind, StatusCanonicalCode>()
.put(Status.Kind.ABORTED, StatusCanonicalCode.ERROR)
.put(Status.Kind.CANCELLED, StatusCanonicalCode.ERROR)
.put(Status.Kind.NOT_FOUND, StatusCanonicalCode.ERROR)
.put(Status.Kind.OK, StatusCanonicalCode.OK)
.put(Status.Kind.RESOURCE_EXHAUSTED, StatusCanonicalCode.ERROR)
.put(Status.Kind.UNKNOWN, StatusCanonicalCode.ERROR)
.put(Status.Kind.INVALID_ARGUMENT, StatusCanonicalCode.ERROR)
.put(Status.Kind.DEADLINE_EXCEEDED, StatusCanonicalCode.ERROR)
.put(Status.Kind.ALREADY_EXISTS, StatusCanonicalCode.ERROR)
.put(Status.Kind.PERMISSION_DENIED, StatusCanonicalCode.ERROR)
.put(Status.Kind.OUT_OF_RANGE, StatusCanonicalCode.ERROR)
.put(Status.Kind.UNIMPLEMENTED, StatusCanonicalCode.ERROR)
.put(Status.Kind.INTERNAL, StatusCanonicalCode.ERROR)
.put(Status.Kind.UNAVAILABLE, StatusCanonicalCode.ERROR)
.put(Status.Kind.UNAUTHENTICATED, StatusCanonicalCode.ERROR)
private static final Map<Status.Kind, StatusCode> statuses
= new ImmutableMap.Builder<Status.Kind, StatusCode>()
.put(Status.Kind.ABORTED, StatusCode.ERROR)
.put(Status.Kind.CANCELLED, StatusCode.ERROR)
.put(Status.Kind.NOT_FOUND, StatusCode.ERROR)
.put(Status.Kind.OK, StatusCode.OK)
.put(Status.Kind.RESOURCE_EXHAUSTED, StatusCode.ERROR)
.put(Status.Kind.UNKNOWN, StatusCode.ERROR)
.put(Status.Kind.INVALID_ARGUMENT, StatusCode.ERROR)
.put(Status.Kind.DEADLINE_EXCEEDED, StatusCode.ERROR)
.put(Status.Kind.ALREADY_EXISTS, StatusCode.ERROR)
.put(Status.Kind.PERMISSION_DENIED, StatusCode.ERROR)
.put(Status.Kind.OUT_OF_RANGE, StatusCode.ERROR)
.put(Status.Kind.UNIMPLEMENTED, StatusCode.ERROR)
.put(Status.Kind.INTERNAL, StatusCode.ERROR)
.put(Status.Kind.UNAVAILABLE, StatusCode.ERROR)
.put(Status.Kind.UNAUTHENTICATED, StatusCode.ERROR)
.build();

@Override
public Span setStatus(Status status) {
Require.nonNull("Status", status);

StatusCanonicalCode statusCanonicalCode = statuses.get(status.getKind());
if (statusCanonicalCode == null) {
StatusCode statusCode = statuses.get(status.getKind());
if (statusCode == null) {
throw new IllegalArgumentException("Unrecognized status kind: " + status.getKind());
}

span.setStatus(statusCanonicalCode,
span.setStatus(statusCode,
"Kind: " + status.getKind().toString()
+ " Description:"
+ status.getDescription());
Expand All @@ -183,7 +183,7 @@ public void close() {

@Override
public String toString() {
SpanContext context = span.getContext();
SpanContext context = span.getSpanContext();

return "OpenTelemetrySpan{traceId=" +
context.getTraceIdAsHexString() +
Expand All @@ -203,8 +203,8 @@ public boolean equals(Object o) {
}

OpenTelemetrySpan that = (OpenTelemetrySpan) o;
SpanContext thisContext = this.span.getContext();
SpanContext thatContext = that.span.getContext();
SpanContext thisContext = this.span.getSpanContext();
SpanContext thatContext = that.span.getSpanContext();

return Objects.equals(thisContext.getSpanIdAsHexString(), thatContext.getSpanIdAsHexString()) &&
Objects.equals(thisContext.getTraceIdAsHexString(), thatContext.getTraceIdAsHexString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package org.openqa.selenium.remote.tracing.opentelemetry;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.api.trace.Tracer;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.Propagator;
import org.openqa.selenium.remote.tracing.TraceContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ java_library(
"//java/client/src/org/openqa/selenium/remote/tracing",
"//java/client/src/org/openqa/selenium/remote/tracing/opentelemetry",
artifact("io.opentelemetry:opentelemetry-api"),
artifact("io.opentelemetry:opentelemetry-context"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@

package org.openqa.selenium.remote.tracing;

import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.DefaultContextPropagators;
import org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer;

public class DefaultTestTracer {

public static Tracer createTracer() {
ContextPropagators propagators = DefaultContextPropagators.builder().build();

return new OpenTelemetryTracer(
OpenTelemetry.getTracerProvider().get("default"),
OpenTelemetry.getPropagators().getTextMapPropagator());
OpenTelemetry.getGlobalTracer("default"),
propagators.getTextMapPropagator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ java_test_suite(
artifact("io.opentelemetry:opentelemetry-sdk"),
artifact("io.opentelemetry:opentelemetry-sdk-common"),
artifact("io.opentelemetry:opentelemetry-sdk-tracing"),
artifact("io.opentelemetry:opentelemetry-context-prop"),
artifact("io.opentelemetry:opentelemetry-context"),
artifact("junit:junit"),
artifact("org.assertj:assertj-core"),
],
Expand Down
Loading

0 comments on commit 057fd66

Please sign in to comment.