Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Adding support for opentracing 0.32 #567

Merged
merged 5 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {

ext.developmentVersion = getProperty('developmentVersion','0.34.1-SNAPSHOT')

ext.opentracingVersion = getProperty('opentracingVersion','0.31.0')
ext.opentracingVersion = getProperty('opentracingVersion','0.32.0')
ext.guavaVersion = getProperty('guavaVersion','18.0')
ext.apacheThriftVersion = getProperty('apacheThriftVersion','0.12.0')
ext.jerseyVersion = getProperty('jerseyVersion','2.22.2')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.opentracing.Span;
import io.opentracing.log.Fields;
import io.opentracing.tag.Tag;
import io.opentracing.tag.Tags;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -206,6 +207,11 @@ public synchronized JaegerSpan setTag(String key, Number value) {
return setTagAsObject(key, value);
}

@Override
public synchronized <T> Span setTag(Tag<T> tag, T value) {
return setTagAsObject(tag.getKey(), value);
}

private JaegerSpan setTagAsObject(String key, Object value) {
if (key.equals(Tags.SAMPLING_PRIORITY.getKey()) && (value instanceof Number)) {
int priority = ((Number) value).intValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class JaegerSpanContext implements SpanContext {
private final Map<String, String> baggage;
private final String debugId;
private final JaegerObjectFactory objectFactory;
private final String traceIdAsString;
private final String spanIdAsString;

public JaegerSpanContext(long traceIdHigh, long traceIdLow, long spanId, long parentId, byte flags) {
this(
Expand Down Expand Up @@ -67,6 +69,8 @@ protected JaegerSpanContext(
this.baggage = baggage;
this.debugId = debugId;
this.objectFactory = objectFactory;
this.traceIdAsString = convertTraceId();
this.spanIdAsString = Long.toHexString(spanId);
}

@Override
Expand All @@ -82,7 +86,7 @@ Map<String, String> baggage() {
return this.baggage;
}

public String getTraceId() {
private String convertTraceId() {
if (traceIdHigh == 0L) {
return Long.toHexString(traceIdLow);
}
Expand All @@ -97,6 +101,10 @@ public String getTraceId() {
return hexStringHigh + hexStringLow;
}

public String getTraceId() {
return this.traceIdAsString;
}

public long getTraceIdLow() {
return traceIdLow;
}
Expand Down Expand Up @@ -170,4 +178,14 @@ boolean hasTrace() {
String getDebugId() {
return debugId;
}

@Override
public String toTraceId() {
return traceIdAsString;
}

@Override
public String toSpanId() {
return spanIdAsString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tag;
import io.opentracing.tag.Tags;
import io.opentracing.util.ThreadLocalScopeManager;
import java.io.Closeable;
Expand Down Expand Up @@ -189,8 +190,7 @@ public ScopeManager scopeManager() {
public Span activeSpan() {
// the active scope might have been added there through an API extension, similar to what the OT java-metrics
// library does -- therefore, we can't guarantee that we are returning a JaegerSpan here.
Scope scope = this.scopeManager.active();
return scope == null ? null : scope.span();
return this.scopeManager.activeSpan();
}

@Override
Expand Down Expand Up @@ -301,6 +301,14 @@ public JaegerTracer.SpanBuilder withTag(String key, Number value) {
return this;
}

@Override
public <T> Tracer.SpanBuilder withTag(Tag<T> tag, T value) {
if (tag != null && tag.getKey() != null) {
this.tags.put(tag.getKey(), value);
}
return this;
}

@Override
public JaegerTracer.SpanBuilder withStartTimestamp(long microseconds) {
this.startTimeMicroseconds = microseconds;
Expand Down Expand Up @@ -428,8 +436,8 @@ public JaegerSpan start() {
JaegerSpanContext context;

// Check if active span should be established as CHILD_OF relationship
if (references.isEmpty() && !ignoreActiveSpan && null != scopeManager.active()) {
asChildOf(scopeManager.active().span());
if (references.isEmpty() && !ignoreActiveSpan && null != scopeManager.activeSpan()) {
asChildOf(scopeManager.activeSpan());
}

if (references.isEmpty() || !references.get(0).getSpanContext().hasTrace()) {
Expand Down Expand Up @@ -486,6 +494,7 @@ public JaegerSpan startManual() {
private JaegerObjectFactory getObjectFactory() {
return JaegerTracer.this.objectFactory;
}

}

/**
Expand Down Expand Up @@ -700,4 +709,9 @@ boolean isExpandExceptionLogs() {
public boolean isUseTraceId128Bit() {
return this.useTraceId128Bit;
}

@Override
public Scope activateSpan(Span span) {
return scopeManager().activate(span);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
import io.opentracing.propagation.Format;
import io.opentracing.propagation.Format.Builtin;
import io.opentracing.propagation.TextMap;
import io.opentracing.propagation.TextMapExtractAdapter;
import io.opentracing.propagation.TextMapInjectAdapter;
import io.opentracing.propagation.TextMapAdapter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -480,13 +479,13 @@ public void testCodecWithPropagationB3() {
private <C> void assertInjectExtract(JaegerTracer tracer, Format<C> format, JaegerSpanContext contextToInject,
boolean injectMapIsEmpty) {
HashMap<String, String> injectMap = new HashMap<>();
tracer.inject(contextToInject, format, (C) new TextMapInjectAdapter(injectMap));
tracer.inject(contextToInject, format, (C) new TextMapAdapter(injectMap));
assertEquals(injectMapIsEmpty, injectMap.isEmpty());
if (injectMapIsEmpty) {
return;
}

JaegerSpanContext extractedContext = tracer.extract(format, (C) new TextMapExtractAdapter(injectMap));
JaegerSpanContext extractedContext = tracer.extract(format, (C) new TextMapAdapter(injectMap));
assertEquals(contextToInject.getTraceId(), extractedContext.getTraceId());
assertEquals(contextToInject.getSpanId(), extractedContext.getSpanId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public void testActiveSpan() {
assertEquals(mockSpan, tracer.activeSpan());
}

@Test
public void testActivateSpan() {
JaegerSpan mockSpan = Mockito.mock(JaegerSpan.class);
tracer.activateSpan(mockSpan);
assertEquals(mockSpan, tracer.activeSpan());
}

@Test
public void testActiveSpanPropagation() {
try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
Expand Down Expand Up @@ -158,12 +165,23 @@ public Scope activate(Span span, boolean finishSpanOnClose) {
return scope;
}

@Override
public Scope activate(Span span) {
return activate(span, false);
}

@Override
public Scope active() {
return scope;
}

@Override
public Span activeSpan() {
return null;
}
}).build();
assertEquals(scope, tracer.scopeManager().active());
assertEquals(scope, tracer.scopeManager().activate(mock(Span.class)));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import io.opentracing.Span;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
import io.opentracing.propagation.TextMapExtractAdapter;
import io.opentracing.propagation.TextMapInjectAdapter;
import io.opentracing.propagation.TextMapAdapter;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -50,7 +49,7 @@ public void setUp() {
@Test
public void testDebugCorrelationId() {
Map<String, String> headers = Collections.singletonMap(Constants.DEBUG_ID_HEADER_KEY, "Coraline");
TextMap carrier = new TextMapExtractAdapter(headers);
TextMap carrier = new TextMapAdapter(headers);

JaegerSpanContext inboundSpanContext = tracer.extract(Format.Builtin.TEXT_MAP, carrier);
assertNotNull(inboundSpanContext);
Expand All @@ -74,7 +73,7 @@ public void testStartTraceWithAdhocBaggage() {
public void testJoinTraceWithAdhocBaggage() {
Span span = tracer.buildSpan("test").start();
Map<String, String> headers = new HashMap<String, String>();
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMapInjectAdapter(headers));
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMapAdapter(headers));
assertEquals(1, headers.size());

traceWithAdhocBaggage(headers);
Expand All @@ -83,7 +82,7 @@ public void testJoinTraceWithAdhocBaggage() {
private void traceWithAdhocBaggage(Map<String, String> headers) {
headers.put("jaeger-baggage", "k1=v1, k2 = v2");

JaegerSpanContext parent = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapExtractAdapter(headers));
JaegerSpanContext parent = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(headers));
JaegerSpan span = tracer.buildSpan("test").asChildOf(parent).start();

assertTrue(span.context().isSampled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
import io.opentracing.SpanContext;
import io.opentracing.log.Fields;
import io.opentracing.noop.NoopSpan;
import io.opentracing.tag.AbstractTag;
import io.opentracing.tag.BooleanTag;
import io.opentracing.tag.IntTag;
import io.opentracing.tag.StringTag;
import io.opentracing.tag.Tags;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand Down Expand Up @@ -154,6 +158,35 @@ public void testSetNumberTag() {
assertEquals(expected, jaegerSpan.getTags().get(key));
}

@Test
public void testSetTag() {
jaegerSpan.setTag(new StringTag("stringTag"), "stringTagValue")
.setTag(new IntTag("numberTag"), 1)
.setTag(new BooleanTag("booleanTag"), true)
.setTag(new AbstractTag<Object>("objectTag") {
@Override
public void set(Span span, Object tagValue) {
}
}, this);

Map<String, Object> tags = jaegerSpan.getTags();
assertEquals("stringTagValue", tags.get("stringTag"));
assertEquals(1, tags.get("numberTag"));
assertEquals(true, tags.get("booleanTag"));
assertEquals(this, tags.get("objectTag"));
}

@Test
public void testToTraceId() {
assertEquals(jaegerSpan.context().getTraceId(), jaegerSpan.context().toTraceId());
}

@Test
public void testToSpanId() {
assertEquals(Long.toHexString(jaegerSpan.context().getSpanId()), jaegerSpan.context().toSpanId());
}


@Test
public void testWithTimestampAccurateClock() {
testWithTimestamp(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@
import io.opentracing.Span;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
import io.opentracing.tag.AbstractTag;
import io.opentracing.tag.BooleanTag;
import io.opentracing.tag.IntTag;
import io.opentracing.tag.StringTag;
import io.opentracing.tag.Tags;

import java.io.Closeable;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -184,4 +189,26 @@ public void testSpanContextNotSampled() {
assertEquals(1, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=y,state=started"));
assertEquals(0, metricsFactory.getCounter("jaeger_tracer_traces", "sampled=n,state=started"));
}

@Test
public void testWithTagObject() {
JaegerTracer.SpanBuilder spanBuilder = tracer.buildSpan("ndnd");
spanBuilder.withTag(new StringTag("stringTag"), "stringTagValue")
.withTag(new IntTag("numberTag"), 1)
.withTag(new BooleanTag("booleanTag"), true)
.withTag(new AbstractTag<Object>("objectTag") {
@Override
public void set(Span span, Object tagValue) {
}
}, this);

Span span = spanBuilder.start();
Map<String, Object> tags = ((JaegerSpan) span).getTags();
assertEquals("stringTagValue", tags.get("stringTag"));
assertEquals(1, tags.get("numberTag"));
assertEquals(true, tags.get("booleanTag"));
assertEquals(this, tags.get("objectTag"));
span.finish();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import io.jaegertracing.internal.exceptions.EmptyTracerStateStringException;
import io.jaegertracing.internal.exceptions.MalformedTracerStateStringException;
import io.jaegertracing.internal.exceptions.TraceIdOutOfBoundException;
import io.opentracing.propagation.TextMapExtractAdapter;
import io.opentracing.propagation.TextMapInjectAdapter;
import io.opentracing.propagation.TextMapAdapter;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -136,15 +135,15 @@ public void testAdhocBaggageWithTraceId() {
long traceIdLow = 42;
long spanId = 1;
long parentId = 0;
codec.inject(new JaegerSpanContext(0L, traceIdLow, spanId, parentId, (byte)1), new TextMapInjectAdapter(headers));
codec.inject(new JaegerSpanContext(0L, traceIdLow, spanId, parentId, (byte)1), new TextMapAdapter(headers));
headers.put("jaeger-baggage", "k1=v1, k2 = v2");
JaegerSpanContext context = codec.extract(new TextMapExtractAdapter(headers));
JaegerSpanContext context = codec.extract(new TextMapAdapter(headers));
assertEquals("must have trace ID", 42, context.getTraceIdLow());
assertEquals("must have trace ID", 0L, context.getTraceIdHigh());
assertEquals("must have bagggae", "v1", context.getBaggageItem("k1"));
assertEquals("must have bagggae", "v2", context.getBaggageItem("k2"));
}

/**
* Tests that the codec will return non-null SpanContext even if the only header
* present is "jaeger-baggage".
Expand All @@ -154,7 +153,7 @@ public void testAdhocBaggageWithoutTraceId() {
Map<String, String> headers = new HashMap<String, String>();
headers.put("jaeger-baggage", "k1=v1, k2 = v2, k3=v3=d3");
TextMapCodec codec = new TextMapCodec(false);
JaegerSpanContext context = codec.extract(new TextMapExtractAdapter(headers));
JaegerSpanContext context = codec.extract(new TextMapAdapter(headers));
assertEquals("v1", context.getBaggageItem("k1"));
assertEquals("v2", context.getBaggageItem("k2"));
assertEquals(null, context.getBaggageItem("k3"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
import io.jaegertracing.zipkin.internal.ThriftSpanConverter;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
import io.opentracing.propagation.TextMapExtractAdapter;
import io.opentracing.propagation.TextMapInjectAdapter;
import io.opentracing.propagation.TextMapAdapter;
import io.opentracing.tag.Tags;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -264,10 +263,9 @@ public void testRpcChildSpanHasTheSameId() {
.start();

Map<String, String> map = new HashMap<>();
TextMap carrier = new TextMapInjectAdapter(map);
TextMap carrier = new TextMapAdapter(map);
tracer.inject(client.context(), Format.Builtin.TEXT_MAP, carrier);

carrier = new TextMapExtractAdapter(map);
JaegerSpanContext ctx = tracer.extract(Format.Builtin.TEXT_MAP, carrier);
JaegerSpanContext clientCtx = client.context();
assertEquals(clientCtx.getSpanId(), ctx.getSpanId());
Expand Down
Loading