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

Commit

Permalink
Opentracing 0.32.0 support
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Chimento <dchimento@gmail.com>
  • Loading branch information
dougEfresh committed Mar 28, 2019
1 parent bb0fa21 commit 108a26e
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 24 deletions.
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 @@ -74,6 +75,10 @@ public long getStart() {
return startTimeMicroseconds;
}

public boolean isFinished() {
return finished;
}

public long getDuration() {
synchronized (this) {
return durationMicroseconds;
Expand Down Expand Up @@ -206,6 +211,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 traceIdTextMapCodec;
private final String spanIdTextMapCodec;

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.traceIdTextMapCodec = convertTraceId();
this.spanIdTextMapCodec = 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.traceIdTextMapCodec;
}

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

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

@Override
public String toSpanId() {
return spanIdTextMapCodec;
}
}
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,24 @@ public JaegerTracer.SpanBuilder withTag(String key, Number value) {
return this;
}

@Override
public <T> Tracer.SpanBuilder withTag(Tag<T> tag, T value) {
if (value instanceof Number) {
return this.withTag(tag.getKey(), (Number) value);
}

if (value instanceof String) {
return this.withTag(tag.getKey(), (String) value);
}

if (value instanceof Boolean) {
return this.withTag(tag.getKey(), (Boolean) value);
}

this.tags.put(tag.getKey(), value);
return this;
}

@Override
public JaegerTracer.SpanBuilder withStartTimestamp(long microseconds) {
this.startTimeMicroseconds = microseconds;
Expand Down Expand Up @@ -428,8 +446,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 +504,7 @@ public JaegerSpan startManual() {
private JaegerObjectFactory getObjectFactory() {
return JaegerTracer.this.objectFactory;
}

}

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

@Override
public Scope activateSpan(Span span) {
return scopeManager().activate(span);
}
}
17 changes: 13 additions & 4 deletions jaeger-core/src/test/java/io/jaegertracing/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,15 @@ public void testCodecWithPropagationB3() {
@SuppressWarnings("unchecked")
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));
Map<String, String> injectMap = new HashMap<>();
TextMap textMap = new TestTextMap(injectMap);
tracer.inject(contextToInject, format, (C) textMap);
assertEquals(injectMapIsEmpty, injectMap.isEmpty());
if (injectMapIsEmpty) {
return;
}

JaegerSpanContext extractedContext = tracer.extract(format, (C) new TextMapExtractAdapter(injectMap));
JaegerSpanContext extractedContext = tracer.extract(format, (C) textMap);
assertEquals(contextToInject.getTraceId(), extractedContext.getTraceId());
assertEquals(contextToInject.getSpanId(), extractedContext.getSpanId());
}
Expand All @@ -502,7 +503,15 @@ public void testMetricsFactoryFromServiceLoader() {

static class TestTextMap implements TextMap {

private Map<String,String> values = new HashMap<>();
private final Map<String,String> values;

public TestTextMap() {
this(new HashMap<>());
}

public TestTextMap(Map<String, String> values) {
this.values = values;
}

@Override
public Iterator<Entry<String, String>> iterator() {
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 @@ -30,6 +30,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.junit.Before;
Expand All @@ -50,7 +51,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 TestTextMap(headers);

JaegerSpanContext inboundSpanContext = tracer.extract(Format.Builtin.TEXT_MAP, carrier);
assertNotNull(inboundSpanContext);
Expand All @@ -74,7 +75,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 TestTextMap(headers));
assertEquals(1, headers.size());

traceWithAdhocBaggage(headers);
Expand All @@ -83,11 +84,34 @@ 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 TestTextMap(headers));
JaegerSpan span = tracer.buildSpan("test").asChildOf(parent).start();

assertTrue(span.context().isSampled());
assertEquals("must have baggage", "v1", span.getBaggageItem("k1"));
assertEquals("must have baggage", "v2", span.getBaggageItem("k2"));
}

private static class TestTextMap implements TextMap {

private final Map<String,String> values;

public TestTextMap(Map<String, String> values) {
this.values = values;
}

@Override
public Iterator<Map.Entry<String, String>> iterator() {
return values.entrySet().iterator();
}

@Override
public void put(String key, String value) {
values.put(key, value);
}

public String get(String key) {
return values.get(key);
}
}
}
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();
}

}
Loading

0 comments on commit 108a26e

Please sign in to comment.