diff --git a/build.gradle b/build.gradle index 7f932a142..6f75687b8 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ plugins { ext.developmentVersion = getProperty('developmentVersion','0.32.1-SNAPSHOT') -ext.opentracingVersion = getProperty('opentracingVersion','0.31.0') +ext.opentracingVersion = getProperty('opentracingVersion','0.32.0-RC1') ext.guavaVersion = getProperty('guavaVersion','18.0') ext.apacheThriftVersion = getProperty('apacheThriftVersion','0.11.0') ext.jerseyVersion = getProperty('jerseyVersion','2.22.2') diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java index 695a76e8e..f53d5a7ea 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java @@ -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; @@ -206,6 +207,11 @@ public synchronized JaegerSpan setTag(String key, Number value) { return setTagAsObject(key, value); } + @Override + public synchronized Span setTag(Tag 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(); diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java index 2cae01d86..185c102e5 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java @@ -170,4 +170,14 @@ boolean hasTrace() { String getDebugId() { return debugId; } + + @Override + public String toTraceId() { + return this.getTraceId(); + } + + @Override + public String toSpanId() { + return Long.toHexString(this.getSpanId()); + } } diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerTracer.java b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerTracer.java index 5cefe5ab0..26816af68 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerTracer.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerTracer.java @@ -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; @@ -293,6 +294,24 @@ public JaegerTracer.SpanBuilder withTag(String key, Number value) { return this; } + @Override + public Tracer.SpanBuilder withTag(Tag 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; @@ -478,6 +497,7 @@ public JaegerSpan startManual() { private JaegerObjectFactory getObjectFactory() { return JaegerTracer.this.objectFactory; } + } /** @@ -686,4 +706,9 @@ boolean isExpandExceptionLogs() { public boolean isUseTraceId128Bit() { return this.useTraceId128Bit; } + + @Override + public Scope activateSpan(Span span) { + return scopeManager().activate(span); + } } diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/ActiveSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/ActiveSpanTest.java index f57898dc3..319ba45bf 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/ActiveSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/ActiveSpanTest.java @@ -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)) { @@ -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))); } diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java index 843c0fc5e..acb4f9f8d 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -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; @@ -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("objectTag") { + @Override + public void set(Span span, Object tagValue) { + } + }, this); + + Map 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); diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerTracerTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerTracerTest.java index 9a58fffc1..c4ac91983 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerTracerTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerTracerTest.java @@ -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; @@ -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("objectTag") { + @Override + public void set(Span span, Object tagValue) { + } + }, this); + + Span span = spanBuilder.start(); + Map 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(); + } + }