Skip to content

Commit

Permalink
Fix OpenTelemetry wrapper for the version 0.9.1 upgrade. (#8835)
Browse files Browse the repository at this point in the history
* Fix OpenTelemetry wrapper for the version 0.9.1 upgrade.

* Update tracing.txt for OpenTelemetry 0.9.1.
  • Loading branch information
pujagani committed Nov 4, 2020
1 parent 474d116 commit c8f03a4
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 513 deletions.
50 changes: 0 additions & 50 deletions java/client/src/org/openqa/selenium/remote/tracing/SpanId.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface TraceContext {

Span createSpan(String name);

SpanId getId();
String getId();

Runnable wrap(Runnable runnable);

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

import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.Span;
import org.openqa.selenium.remote.tracing.SpanId;
import org.openqa.selenium.remote.tracing.TraceContext;

import java.util.UUID;
import java.util.concurrent.Callable;

public class NullContext implements TraceContext {

private final SpanId id = new SpanId(UUID.randomUUID());
private final String id = UUID.randomUUID().toString();

@Override
public SpanId getId() {
public String getId() {
return id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.opentelemetry.trace.TracingContextUtils;

import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.SpanId;
import org.openqa.selenium.remote.tracing.TraceContext;

import java.util.Objects;
Expand All @@ -45,16 +44,16 @@ public OpenTelemetryContext(Tracer tracer, Context context) {
}

@Override
public SpanId getId() {
return new SpanId(spanContext.getSpanId());
public String getId() {
return spanContext.getSpanIdAsHexString();
}

@SuppressWarnings("MustBeClosedChecker")
@Override
public OpenTelemetrySpan createSpan(String name) {
Require.nonNull("Name", name);

Span span = tracer.spanBuilder(name).setParent(spanContext).startSpan();
Span span = tracer.spanBuilder(name).setParent(context).startSpan();
Context prev = Context.current();

// Now update the context
Expand Down Expand Up @@ -98,12 +97,11 @@ public int hashCode() {

@Override
public String toString() {

return "OpenTelemetryContext{" +
"tracer=" + tracer +
", context=" + this.context +
", span id=" + spanContext.getSpanId() +
", trace id=" + spanContext.getTraceId() +
", span id=" + spanContext.getSpanIdAsHexString() +
", trace id=" + spanContext.getTraceIdAsHexString() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public <C> OpenTelemetryContext extractContext(

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Primitives;
import io.grpc.Context;
import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.StatusCanonicalCode;
import io.opentelemetry.trace.Tracer;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.EventAttributeValue;
Expand Down Expand Up @@ -97,35 +97,35 @@ public Span addEvent(String name, Map<String, EventAttributeValue> attributeMap)
Require.nonNull("Event Attribute Value", value);
switch (value.getAttributeType()) {
case BOOLEAN:
otAttributes.setAttribute(key, AttributeValue.booleanAttributeValue(value.getBooleanValue()));
otAttributes.setAttribute(key, value.getBooleanValue());
break;

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

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

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

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

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

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

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

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

private static final Map<Status.Kind, io.opentelemetry.trace.Status> statuses
= new ImmutableMap.Builder<Status.Kind, io.opentelemetry.trace.Status>()
.put(Status.Kind.ABORTED, io.opentelemetry.trace.Status.ABORTED)
.put(Status.Kind.CANCELLED, io.opentelemetry.trace.Status.CANCELLED)
.put(Status.Kind.NOT_FOUND, io.opentelemetry.trace.Status.NOT_FOUND)
.put(Status.Kind.OK, io.opentelemetry.trace.Status.OK)
.put(Status.Kind.RESOURCE_EXHAUSTED, io.opentelemetry.trace.Status.RESOURCE_EXHAUSTED)
.put(Status.Kind.UNKNOWN, io.opentelemetry.trace.Status.UNKNOWN)
.put(Status.Kind.INVALID_ARGUMENT,io.opentelemetry.trace.Status.INVALID_ARGUMENT)
.put(Status.Kind.DEADLINE_EXCEEDED,io.opentelemetry.trace.Status.DEADLINE_EXCEEDED)
.put(Status.Kind.ALREADY_EXISTS,io.opentelemetry.trace.Status.ALREADY_EXISTS)
.put(Status.Kind.PERMISSION_DENIED,io.opentelemetry.trace.Status.PERMISSION_DENIED)
.put(Status.Kind.OUT_OF_RANGE,io.opentelemetry.trace.Status.OUT_OF_RANGE)
.put(Status.Kind.UNIMPLEMENTED,io.opentelemetry.trace.Status.UNIMPLEMENTED)
.put(Status.Kind.INTERNAL,io.opentelemetry.trace.Status.INTERNAL)
.put(Status.Kind.UNAVAILABLE,io.opentelemetry.trace.Status.UNAVAILABLE)
.put(Status.Kind.UNAUTHENTICATED,io.opentelemetry.trace.Status.UNAUTHENTICATED)
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)
.build();

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

io.opentelemetry.trace.Status otStatus = statuses.get(status.getKind());
if (otStatus == null) {
StatusCanonicalCode statusCanonicalCode = statuses.get(status.getKind());
if (statusCanonicalCode == null) {
throw new IllegalArgumentException("Unrecognized status kind: " + status.getKind());
}

otStatus.withDescription(status.getDescription());

span.setStatus(otStatus);
span.setStatus(statusCanonicalCode,
"Kind: " + status.getKind().toString()
+ " Description:"
+ status.getDescription());

return this;
}
Expand All @@ -185,9 +186,9 @@ public String toString() {
SpanContext context = span.getContext();

return "OpenTelemetrySpan{traceId=" +
context.getTraceId() +
context.getTraceIdAsHexString() +
",spanId=" +
context.getSpanId() +
context.getSpanIdAsHexString() +
"}";
}

Expand All @@ -205,8 +206,8 @@ public boolean equals(Object o) {
SpanContext thisContext = this.span.getContext();
SpanContext thatContext = that.span.getContext();

return Objects.equals(thisContext.getSpanId(), thatContext.getSpanId()) &&
Objects.equals(thisContext.getTraceId(), thatContext.getTraceId());
return Objects.equals(thisContext.getSpanIdAsHexString(), thatContext.getSpanIdAsHexString()) &&
Objects.equals(thisContext.getTraceIdAsHexString(), thatContext.getTraceIdAsHexString());
}

@Override
Expand Down
Loading

0 comments on commit c8f03a4

Please sign in to comment.