Skip to content

Commit

Permalink
Upgrade OpenTelemetry to 0.16.0 (#9187)
Browse files Browse the repository at this point in the history
* Upgrade OpenTelemetry to 0.15.0

* Upgrade OpenTelemetry to 0.16.0

* Remove unused import from JaegerTracing

Co-authored-by: David Burns <[email protected]>
Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
3 people authored Feb 20, 2021
1 parent 8dcfe79 commit d17ac96
Show file tree
Hide file tree
Showing 12 changed files with 567 additions and 531 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ java_library(
artifact("io.opentelemetry:opentelemetry-sdk"),
artifact("io.opentelemetry:opentelemetry-sdk-common"),
artifact("io.opentelemetry:opentelemetry-sdk-trace"),
artifact("io.opentelemetry:opentelemetry-semconv"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.lang.reflect.Method;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -100,15 +101,12 @@ private static Object createJaegerGrpcSpanExporter(Object jaegerChannel) throws

Class<?> builderClazz = builderObj.getClass();

Method setServiceName = builderClazz.getMethod("setServiceName", String.class);
builderObj = setServiceName.invoke(builderObj, System.getProperty("JAEGER_SERVICE_NAME", "selenium"));

Class<?> managedChannelClazz = Class.forName("io.grpc.ManagedChannel", true, cl);
Method setChannel = builderClazz.getMethod("setChannel", managedChannelClazz);
builderObj = setChannel.invoke(builderObj, jaegerChannel);

Method setDeadline = builderClazz.getMethod("setDeadlineMs", long.class);
builderObj = setDeadline.invoke(builderObj, 3000);
Method setTimeout = builderClazz.getMethod("setTimeout", long.class, TimeUnit.class);
builderObj = setTimeout.invoke(builderObj, 3000, TimeUnit.MILLISECONDS);

Method build = builderClazz.getMethod("build");
return build.invoke(builderObj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OpenTelemetryContext(Tracer tracer, Context context) {

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

@SuppressWarnings("MustBeClosedChecker")
Expand Down Expand Up @@ -99,8 +99,8 @@ public String toString() {
return "OpenTelemetryContext{" +
"tracer=" + tracer +
", context=" + this.context +
", span id=" + spanContext.getSpanIdAsHexString() +
", trace id=" + spanContext.getTraceIdAsHexString() +
", span id=" + spanContext.getSpanId() +
", trace id=" + spanContext.getTraceId() +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public String get(C carrier, String key) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ public String toString() {
SpanContext context = span.getSpanContext();

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

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.EventData;
Expand All @@ -34,6 +35,7 @@
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;

import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonOutput;
Expand Down Expand Up @@ -130,13 +132,19 @@ public CompletableResultCode shutdown() {
maybeJaeger.ifPresent(
exporter -> exporters.add(SimpleSpanProcessor.create(exporter)));

Resource serviceNameResource =
Resource.create(Attributes.of(
ResourceAttributes.SERVICE_NAME,
System.getProperty("JAEGER_SERVICE_NAME", "selenium")));

// OpenTelemetry default propagators are no-op since version 0.9.0.
// Hence, required propagators need to defined and added.
ContextPropagators propagators =
ContextPropagators.create((W3CTraceContextPropagator.getInstance()));

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(SpanProcessor.composite(exporters))
.setResource(Resource.getDefault().merge(serviceNameResource))
.build();

OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
Expand All @@ -145,7 +153,7 @@ public CompletableResultCode shutdown() {
.buildAndRegisterGlobal();

Runtime.getRuntime()
.addShutdownHook(new Thread(() -> openTelemetrySdk.getTracerManagement().shutdown()));
.addShutdownHook(new Thread(sdkTracerProvider::close));

return new OpenTelemetryTracer(
openTelemetrySdk.getTracer("default"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ java_library(
deps = [
"//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-api"),
artifact("io.opentelemetry:opentelemetry-sdk"),
artifact("io.opentelemetry:opentelemetry-sdk-common"),
artifact("io.opentelemetry:opentelemetry-sdk-trace"),
artifact("io.opentelemetry:opentelemetry-context"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,28 @@

package org.openqa.selenium.remote.tracing;

import io.opentelemetry.api.DefaultOpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer;

public class DefaultTestTracer {

public static Tracer createTracer() {
ContextPropagators propagators = ContextPropagators.create(
TextMapPropagator.composite(W3CTraceContextPropagator.getInstance()));
ContextPropagators propagators = ContextPropagators.noop();
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.build();

OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setPropagators(propagators)
.build();

Runtime.getRuntime()
.addShutdownHook(new Thread(sdkTracerProvider::close));

return new OpenTelemetryTracer(
DefaultOpenTelemetry.builder().build().getTracer("default"),
openTelemetrySdk.getTracer("test"),
propagators.getTextMapPropagator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.openqa.selenium.grid.web.CombinedHandler;
Expand Down Expand Up @@ -70,6 +72,16 @@
@Category(UnitTests.class)
public class TracerTest {

@Before
public void before() {
GlobalOpenTelemetry.resetForTest();
}

@After
public void after() {
GlobalOpenTelemetry.resetForTest();
}

@Test
public void shouldBeAbleToCreateATracer() {
List<SpanData> allSpans = new ArrayList<>();
Expand Down Expand Up @@ -603,10 +615,10 @@ public CompletableResultCode shutdown() {
.buildAndRegisterGlobal();

Runtime.getRuntime()
.addShutdownHook(new Thread(() -> openTelemetrySdk.getTracerManagement().shutdown()));
.addShutdownHook(new Thread(sdkTracerProvider::close));

return new OpenTelemetryTracer(
GlobalOpenTelemetry.getTracer("test"),
openTelemetrySdk.getTracer("test"),
propagators.getTextMapPropagator());
}
}
4 changes: 2 additions & 2 deletions java/maven_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@rules_jvm_external//:specs.bzl", "maven")

def selenium_java_deps():
netty_version = "4.1.58.Final"
opentelemetry_version = "0.14.1"
opentelemetry_version = "0.16.0"

maven_install(
artifacts = [
Expand Down Expand Up @@ -41,7 +41,7 @@ def selenium_java_deps():
"io.opentelemetry:opentelemetry-api:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-context:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-exporter-logging:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-semconv:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-semconv:%s" % opentelemetry_version+"-alpha",
"io.opentelemetry:opentelemetry-sdk:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-sdk-common:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-sdk-testing:%s" % opentelemetry_version,
Expand Down
Loading

0 comments on commit d17ac96

Please sign in to comment.