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

Change return types from Jaeger Span/Tracer/Context to Jaeger types #469

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public JaegerTracer.Builder getTracerBuilder() {
return builder;
}

public synchronized Tracer getTracer() {
public synchronized JaegerTracer getTracer() {
if (tracer != null) {
return tracer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static JaegerSpanContext contextFromString(String value)
}

/*
TODO(oibe) because java doesn't like to convert large hex strings to longs
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the TODO marker here because it doesn't sounds like it's a TODO...

oibe: because java doesn't like to convert large hex strings to longs
we should write this manually instead of using BigInteger.
*/
return new JaegerSpanContext(
Expand Down
38 changes: 23 additions & 15 deletions jaeger-core/src/main/java/io/jaegertracing/JaegerTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,23 @@ public ScopeManager scopeManager() {
return scopeManager;
}

/**
* Retrieves the currently active span from the {@link ScopeManager}. It cannot be guaranteed that this span
* will be a {@link JaegerSpan}, as other libraries might have set this active span there. Consumers expecting
* this to return a {@link JaegerSpan} should always check the type of the return and act accordingly.
*
* @return the currently active span from the {@link ScopeManager}
*/
@Override
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();
}

@Override
public Tracer.SpanBuilder buildSpan(String operationName) {
public JaegerTracer.SpanBuilder buildSpan(String operationName) {
return new SpanBuilder(operationName);
}

Expand Down Expand Up @@ -205,10 +214,9 @@ public void close() {
sampler.close();
}

//Visible for testing
class SpanBuilder implements Tracer.SpanBuilder {
public class SpanBuilder implements Tracer.SpanBuilder {

private String operationName = null;
private String operationName;
private long startTimeMicroseconds;
/**
* In 99% situations there is only one parent (childOf), so we do not want to allocate
Expand All @@ -223,19 +231,19 @@ class SpanBuilder implements Tracer.SpanBuilder {
}

@Override
public Tracer.SpanBuilder asChildOf(SpanContext parent) {
public JaegerTracer.SpanBuilder asChildOf(SpanContext parent) {
return addReference(References.CHILD_OF, parent);
}

@Override
public Tracer.SpanBuilder asChildOf(Span parent) {
public JaegerTracer.SpanBuilder asChildOf(Span parent) {
return addReference(References.CHILD_OF, parent != null ? parent.context() : null);
}

@Override
public Tracer.SpanBuilder addReference(String referenceType, SpanContext reference) {

public JaegerTracer.SpanBuilder addReference(String referenceType, SpanContext reference) {
if (!(reference instanceof JaegerSpanContext)) {
log.warn("Expected to have a JaegerSpanContext but got " + referenceType.getClass().getName());
return this;
}

Expand All @@ -261,25 +269,25 @@ public Tracer.SpanBuilder addReference(String referenceType, SpanContext referen
}

@Override
public Tracer.SpanBuilder withTag(String key, String value) {
public JaegerTracer.SpanBuilder withTag(String key, String value) {
tags.put(key, value);
return this;
}

@Override
public Tracer.SpanBuilder withTag(String key, boolean value) {
public JaegerTracer.SpanBuilder withTag(String key, boolean value) {
tags.put(key, value);
return this;
}

@Override
public Tracer.SpanBuilder withTag(String key, Number value) {
public JaegerTracer.SpanBuilder withTag(String key, Number value) {
tags.put(key, value);
return this;
}

@Override
public Tracer.SpanBuilder withStartTimestamp(long microseconds) {
public JaegerTracer.SpanBuilder withStartTimestamp(long microseconds) {
this.startTimeMicroseconds = microseconds;
return this;
}
Expand Down Expand Up @@ -441,20 +449,20 @@ public Scope startActive(boolean finishSpanOnClose) {
}

@Override
public Tracer.SpanBuilder ignoreActiveSpan() {
public JaegerTracer.SpanBuilder ignoreActiveSpan() {
ignoreActiveSpan = true;
return this;
}

@Override
@Deprecated
public Span startManual() {
public JaegerSpan startManual() {
return start();
}
}

/**
* Builds Jaeger JaegerTracer with options.
* Builds a {@link JaegerTracer} with options.
*/
public static final class Builder {
private Sampler sampler;
Expand Down
42 changes: 19 additions & 23 deletions jaeger-core/src/test/java/io/jaegertracing/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import io.jaegertracing.samplers.ProbabilisticSampler;
import io.jaegertracing.samplers.RateLimitingSampler;
import io.jaegertracing.samplers.Sampler;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.noop.NoopTracerFactory;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.Format.Builtin;
Expand Down Expand Up @@ -139,7 +137,7 @@ public void testReporterConfigurationInvalidLogSpans() {
public void testTracerTagslist() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");
System.setProperty(Configuration.JAEGER_TAGS, "testTag1=testValue1, testTag2 = testValue2");
JaegerTracer tracer = (JaegerTracer) Configuration.fromEnv().getTracer();
JaegerTracer tracer = Configuration.fromEnv().getTracer();
assertEquals("testValue1", tracer.tags().get("testTag1"));
assertEquals("testValue2", tracer.tags().get("testTag2"));
}
Expand All @@ -148,15 +146,15 @@ public void testTracerTagslist() {
public void testTracerTagslistFormatError() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");
System.setProperty(Configuration.JAEGER_TAGS, "testTag1, testTag2 = testValue2");
JaegerTracer tracer = (JaegerTracer) Configuration.fromEnv().getTracer();
JaegerTracer tracer = Configuration.fromEnv().getTracer();
assertEquals("testValue2", tracer.tags().get("testTag2"));
}

@Test
public void testTracerTagsSubstitutionDefault() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");
System.setProperty(Configuration.JAEGER_TAGS, "testTag1=${" + TEST_PROPERTY + ":hello}");
JaegerTracer tracer = (JaegerTracer) Configuration.fromEnv().getTracer();
JaegerTracer tracer = Configuration.fromEnv().getTracer();
assertEquals("hello", tracer.tags().get("testTag1"));
}

Expand All @@ -165,7 +163,7 @@ public void testTracerTagsSubstitutionSpecified() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");
System.setProperty(TEST_PROPERTY, "goodbye");
System.setProperty(Configuration.JAEGER_TAGS, "testTag1=${" + TEST_PROPERTY + ":hello}");
JaegerTracer tracer = (JaegerTracer) Configuration.fromEnv().getTracer();
JaegerTracer tracer = Configuration.fromEnv().getTracer();
assertEquals("goodbye", tracer.tags().get("testTag1"));
}

Expand All @@ -188,16 +186,16 @@ public void testPropagationB3Only() {
long spanId = 5678;

TestTextMap textMap = new TestTextMap();
SpanContext spanContext = new JaegerSpanContext(traceId, spanId, 0, (byte)0);
JaegerSpanContext spanContext = new JaegerSpanContext(traceId, spanId, 0, (byte)0);

Tracer tracer = Configuration.fromEnv().getTracer();
JaegerTracer tracer = Configuration.fromEnv().getTracer();
tracer.inject(spanContext, Format.Builtin.TEXT_MAP, textMap);

assertNotNull(textMap.get("X-B3-TraceId"));
assertNotNull(textMap.get("X-B3-SpanId"));
assertNull(textMap.get("uber-trace-id"));

JaegerSpanContext extractedContext = (JaegerSpanContext)tracer.extract(Format.Builtin.TEXT_MAP, textMap);
JaegerSpanContext extractedContext = tracer.extract(Format.Builtin.TEXT_MAP, textMap);
assertEquals(traceId, extractedContext.getTraceId());
assertEquals(spanId, extractedContext.getSpanId());
}
Expand All @@ -211,16 +209,16 @@ public void testPropagationJaegerAndB3() {
long spanId = 5678;

TestTextMap textMap = new TestTextMap();
SpanContext spanContext = new JaegerSpanContext(traceId, spanId, 0, (byte)0);
JaegerSpanContext spanContext = new JaegerSpanContext(traceId, spanId, 0, (byte)0);

Tracer tracer = Configuration.fromEnv().getTracer();
JaegerTracer tracer = Configuration.fromEnv().getTracer();
tracer.inject(spanContext, Format.Builtin.TEXT_MAP, textMap);

assertNotNull(textMap.get("uber-trace-id"));
assertNotNull(textMap.get("X-B3-TraceId"));
assertNotNull(textMap.get("X-B3-SpanId"));

JaegerSpanContext extractedContext = (JaegerSpanContext)tracer.extract(Format.Builtin.TEXT_MAP, textMap);
JaegerSpanContext extractedContext = tracer.extract(Format.Builtin.TEXT_MAP, textMap);
assertEquals(traceId, extractedContext.getTraceId());
assertEquals(spanId, extractedContext.getSpanId());
}
Expand All @@ -230,7 +228,7 @@ public void testPropagationDefault() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");

TestTextMap textMap = new TestTextMap();
SpanContext spanContext = new JaegerSpanContext(1234, 5678, 0, (byte)0);
JaegerSpanContext spanContext = new JaegerSpanContext(1234, 5678, 0, (byte)0);

Configuration.fromEnv().getTracer().inject(spanContext, Format.Builtin.TEXT_MAP, textMap);

Expand All @@ -245,7 +243,7 @@ public void testPropagationValidFormat() {
System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test");

TestTextMap textMap = new TestTextMap();
SpanContext spanContext = new JaegerSpanContext(1234, 5678, 0, (byte)0);
JaegerSpanContext spanContext = new JaegerSpanContext(1234, 5678, 0, (byte)0);

Configuration.fromEnv().getTracer().inject(spanContext, Format.Builtin.TEXT_MAP, textMap);

Expand Down Expand Up @@ -356,7 +354,7 @@ public void inject(JaegerSpanContext spanContext, TextMap carrier) {

Configuration configuration = new Configuration("foo")
.withCodec(codecConfiguration);
SpanContext spanContext = new JaegerSpanContext(2L, 11L, 22L, (byte) 0);
JaegerSpanContext spanContext = new JaegerSpanContext(2L, 11L, 22L, (byte) 0);
assertInjectExtract(configuration.getTracer(), Builtin.TEXT_MAP, spanContext, false);
// added codecs above overrides the default implementation
assertInjectExtract(configuration.getTracer(), Builtin.HTTP_HEADERS, spanContext, true);
Expand All @@ -365,26 +363,24 @@ public void inject(JaegerSpanContext spanContext, TextMap carrier) {
@Test
public void testDefaultCodecs() {
Configuration configuration = new Configuration("foo");
SpanContext spanContext = new JaegerSpanContext(2L, 11L, 22L, (byte) 0);
JaegerSpanContext spanContext = new JaegerSpanContext(2L, 11L, 22L, (byte) 0);
assertInjectExtract(configuration.getTracer(), Builtin.TEXT_MAP, spanContext, false);
assertInjectExtract(configuration.getTracer(), Builtin.HTTP_HEADERS, spanContext, false);
}

@SuppressWarnings("unchecked")
private <C> void assertInjectExtract(Tracer tracer, Format<C> format, SpanContext contextToInject,
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 TextMapInjectAdapter(injectMap));
assertEquals(injectMapIsEmpty, injectMap.isEmpty());
if (injectMapIsEmpty) {
return;
}
JaegerSpanContext extractedContext = (JaegerSpanContext) tracer.extract(format,
(C)new TextMapExtractAdapter(injectMap));

JaegerSpanContext spanContextToInject = (JaegerSpanContext) contextToInject;
assertEquals(spanContextToInject.getTraceId(), extractedContext.getTraceId());
assertEquals(spanContextToInject.getSpanId(), extractedContext.getSpanId());
JaegerSpanContext extractedContext = tracer.extract(format, (C) new TextMapExtractAdapter(injectMap));
assertEquals(contextToInject.getTraceId(), extractedContext.getTraceId());
assertEquals(contextToInject.getSpanId(), extractedContext.getSpanId());
}

@Test
Expand Down
Loading