Skip to content

Commit

Permalink
Bump OpenTelemetry to 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 23, 2020
1 parent c3ccaac commit c5ad184
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
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.Tracer;

import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.tracing.EventAttributeValue;
import org.openqa.selenium.remote.tracing.Span;
import org.openqa.selenium.remote.tracing.Status;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -91,26 +90,26 @@ public Span addEvent(String name) {
public Span addEvent(String name, Map<String, EventAttributeValue> attributeMap) {
Require.nonNull("Name", name);
Require.nonNull("Event Attribute Map", attributeMap);
Map<String, AttributeValue> openTelAttributeMap = new HashMap<>();
Attributes.Builder otAttributes = new Attributes.Builder();

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

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

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

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

default:
Expand All @@ -119,7 +118,7 @@ public Span addEvent(String name, Map<String, EventAttributeValue> attributeMap)
}
);

span.addEvent(name, openTelAttributeMap);
span.addEvent(name, otAttributes.build());
return this;
}

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

import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.TracerSdkProvider;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.SpanData.TimedEvent;
import io.opentelemetry.sdk.trace.export.SimpleSpansProcessor;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.junit.Test;
import org.openqa.selenium.grid.web.CombinedHandler;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void shouldBeAbleToCreateATracer() {
}

Set<SpanData> values = allSpans.stream()
.filter(data -> data.getAttributes().containsKey("cheese"))
.filter(data -> data.getAttributes().get("cheese") != null)
.collect(Collectors.toSet());

assertThat(values).hasSize(1);
Expand All @@ -86,14 +86,13 @@ public void shouldBeAbleToCreateATracer() {
public void shouldBeAbleToCreateASpanWithEvents() {
List<SpanData> allSpans = new ArrayList<>();
Tracer tracer = createTracer(allSpans);
Map<String, AttributeValue> openTelAttributeValueMap = new HashMap<>();
openTelAttributeValueMap
.put("testString", AttributeValue.stringAttributeValue("attributeValue"));
openTelAttributeValueMap.put("testBoolean", AttributeValue.booleanAttributeValue(true));
openTelAttributeValueMap.put("testFloat", AttributeValue.doubleAttributeValue(5.5f));
openTelAttributeValueMap.put("testDouble", AttributeValue.doubleAttributeValue(5.55555));
openTelAttributeValueMap.put("testInt", AttributeValue.longAttributeValue(10));
openTelAttributeValueMap.put("testLong", AttributeValue.longAttributeValue(100L));
Attributes.Builder atAttributes = new Attributes.Builder();
atAttributes.setAttribute("testString", AttributeValue.stringAttributeValue("attributeValue"));
atAttributes.setAttribute("testBoolean", AttributeValue.booleanAttributeValue(true));
atAttributes.setAttribute("testFloat", AttributeValue.doubleAttributeValue(5.5f));
atAttributes.setAttribute("testDouble", AttributeValue.doubleAttributeValue(5.55555));
atAttributes.setAttribute("testInt", AttributeValue.longAttributeValue(10));
atAttributes.setAttribute("testLong", AttributeValue.longAttributeValue(100L));

try (Span span = tracer.getCurrentContext().createSpan("parent")) {
span.addEvent("Test event start");
Expand All @@ -112,18 +111,19 @@ public void shouldBeAbleToCreateASpanWithEvents() {
assertThat(allSpans).hasSize(1);

SpanData spanData = allSpans.get(0);
assertThat(spanData.getTimedEvents()).hasSize(2);
assertThat(spanData.getEvents()).hasSize(2);

List<TimedEvent> timedEvents = spanData.getTimedEvents();
assertThat(timedEvents).element(0).extracting(TimedEvent::getName)
List<SpanData.Event> timedEvents = spanData.getEvents();
assertThat(timedEvents).element(0).extracting(SpanData.Event::getName)
.isEqualTo("Test event start");
assertThat(timedEvents).element(0).extracting(TimedEvent::getTotalAttributeCount).isEqualTo(0);
assertThat(timedEvents).element(1).extracting(TimedEvent::getName).isEqualTo("Test event end");
assertThat(timedEvents).element(1).extracting(TimedEvent::getTotalAttributeCount).isEqualTo(6);
assertThat(timedEvents.get(1).getAttributes())
.containsKeys("testString", "testBoolean", "testFloat", "testDouble", "testInt",
"testLong");
assertThat(timedEvents.get(1).getAttributes()).isEqualTo(openTelAttributeValueMap);
assertThat(timedEvents).element(0).extracting(SpanData.Event::getTotalAttributeCount).isEqualTo(0);
assertThat(timedEvents).element(1).extracting(SpanData.Event::getName).isEqualTo("Test event end");
assertThat(timedEvents).element(1).extracting(SpanData.Event::getTotalAttributeCount).isEqualTo(6);

List<String> allKeys = new ArrayList<>();
timedEvents.get(1).getAttributes().forEach((key, value) -> allKeys.add(key));
assertThat(allKeys).contains("testString", "testBoolean", "testFloat", "testDouble", "testInt", "testLong");
assertThat(timedEvents.get(1).getAttributes()).isEqualTo(atAttributes.build());
}

@Test
Expand Down Expand Up @@ -258,7 +258,7 @@ public void cleverShenanigansRepresentingWhatWeSeeInTheRouter() {

private Tracer createTracer(List<SpanData> exportTo) {
TracerSdkProvider provider = OpenTelemetrySdk.getTracerProvider();
provider.addSpanProcessor(SimpleSpansProcessor.create(new SpanExporter() {
provider.addSpanProcessor(SimpleSpanProcessor.newBuilder(new SpanExporter() {
@Override
public ResultCode export(Collection<SpanData> spans) {
exportTo.addAll(spans);
Expand All @@ -272,7 +272,7 @@ public ResultCode export(Collection<SpanData> spans) {
@Override
public void shutdown() {
}
}));
}).build());

io.opentelemetry.trace.Tracer otTracer = provider.get("get");
return new OpenTelemetryTracer(
Expand Down
2 changes: 1 addition & 1 deletion java/maven_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@rules_jvm_external//:specs.bzl", "maven")
def selenium_java_deps():
jetty_version = "9.4.28.v20200408"
netty_version = "4.1.49.Final"
opentelemetry_version = "0.4.0"
opentelemetry_version = "0.6.0"

maven_install(
artifacts = [
Expand Down
Loading

0 comments on commit c5ad184

Please sign in to comment.