Skip to content
Permalink
Browse files
adding parent span id support
  • Loading branch information
rmannibucau committed May 21, 2018
1 parent c12c359 commit 4566c59cd749791f7f602d677635e2589770ed76
Showing 6 changed files with 44 additions and 34 deletions.
@@ -23,6 +23,7 @@ NOTE: if you are using Microprofile Config it is used, otherwise it uses system
|geronimo.opentracing.client.filter.request.skip|Should client instrumentation be ignored|false
|geronimo.opentracing.client.filter.request.skipDefaultTags|Should `HTTP_METHOD`, `HTTP_URL` not be added to tags|false
|geronimo.opentracing.client.filter.request.skipPeerTags|Should `PEER_HOSTNAME`, `PEER_PORT` not be added to tags|false
|geronimo.opentracing.propagation.headers.parentSpanId|Name of the header used to host the parent spanId value|`X-B3-ParentSpanId`
|geronimo.opentracing.propagation.headers.spanId|Name of the header used to host the spanId value|`X-B3-SpanId`
|geronimo.opentracing.propagation.headers.traceId|Name of the header used to host the traceId value|`X-B3-TraceId`
|geronimo.opentracing.propagation.headers.baggagePrefix|Prefix of headers used to host the baggage values|`baggage-`
@@ -32,6 +32,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MultivaluedMap;

import org.apache.geronimo.microprofile.opentracing.config.GeronimoOpenTracingConfig;

import io.opentracing.Scope;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
@@ -40,8 +42,6 @@
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;

import org.apache.geronimo.microprofile.opentracing.config.GeronimoOpenTracingConfig;

@ApplicationScoped
public class GeronimoTracer implements Tracer {

@@ -57,12 +57,14 @@ public class GeronimoTracer implements Tracer {
@Inject
private GeronimoOpenTracingConfig config;

private String parentSpanIdHeader;
private String spanIdHeader;
private String traceIdHeader;
private String baggageHeaderPrefix;

@PostConstruct
private void init() {
parentSpanIdHeader = config.read("propagation.headers.parentSpanId", "X-B3-ParentSpanId");
spanIdHeader = config.read("propagation.headers.spanId", "X-B3-SpanId");
traceIdHeader = config.read("propagation.headers.traceId", "X-B3-TraceId");
baggageHeaderPrefix = config.read("propagation.headers.baggagePrefix", "baggage-");
@@ -81,7 +83,7 @@ public Span activeSpan() {
@Override
public SpanBuilder buildSpan(final String operationName) {
return new SpanBuilderImpl(
this, (traceId, baggages) -> newContext(traceId, idGenerator.next(), baggages),
this,
span -> finishedSpanEvent.fire(new FinishedSpan(processNewSpan(span))), operationName, idGenerator);
}

@@ -103,8 +105,9 @@ public <C> SpanContext extract(final Format<C> format, final C carrier) {
final MultivaluedMap<String, ?> map = JaxRsHeaderTextMap.class.cast(carrier).getMap();
final String traceid = (String) map.getFirst(traceIdHeader);
final String spanid = (String) map.getFirst(spanIdHeader);
final String parentspanid = (String) map.getFirst(parentSpanIdHeader);
if (traceid != null && spanid != null) {
return newContext(traceid, spanid, map.keySet().stream().filter(it -> it.startsWith(baggageHeaderPrefix))
return newContext(traceid, parentspanid, spanid, map.keySet().stream().filter(it -> it.startsWith(baggageHeaderPrefix))
.collect(toMap(identity(), k -> String.valueOf(map.getFirst(k)))));
}
return null;
@@ -113,8 +116,10 @@ public <C> SpanContext extract(final Format<C> format, final C carrier) {
final HttpServletRequest req = ServletHeaderTextMap.class.cast(carrier).getRequest();
final String traceid = req.getHeader(traceIdHeader);
final String spanid = req.getHeader(spanIdHeader);
final String parentspanid = req.getHeader(parentSpanIdHeader);
if (traceid != null && spanid != null) {
return newContext(traceid, spanid, list(req.getHeaderNames()).stream().filter(it -> it.startsWith(baggageHeaderPrefix))
return newContext(traceid, parentspanid, spanid, list(req.getHeaderNames()).stream()
.filter(it -> it.startsWith(baggageHeaderPrefix))
.collect(toMap(identity(), k -> String.valueOf(req.getHeader(k)))));
}
return null;
@@ -125,6 +130,7 @@ public <C> SpanContext extract(final Format<C> format, final C carrier) {
final Iterator<Map.Entry<String, String>> textMap = TextMap.class.cast(carrier).iterator();
String traceId = null;
String spanId = null;
String parentSpanId = null;
final Map<String, String> baggages = new HashMap<>();
while (textMap.hasNext()) {
final Map.Entry<String, String> next = textMap.next();
@@ -134,10 +140,12 @@ public <C> SpanContext extract(final Format<C> format, final C carrier) {
spanId = next.getValue();
} else if (traceIdHeader.equals(next.getKey())) {
traceId = next.getValue();
} else if (parentSpanIdHeader.equals(next.getKey())) {
parentSpanId = next.getValue();
}
}
if (traceId != null && spanId != null) {
return newContext(traceId, spanId, baggages);
return newContext(traceId, parentSpanId, spanId, baggages);
}
return null;
}
@@ -146,7 +154,8 @@ protected Span processNewSpan(final SpanImpl span) {
return span;
}

protected SpanContextImpl newContext(final Object traceId, final Object spanId, final Map<String, String> baggages) {
return new SpanContextImpl(traceId, spanId, baggages);
protected SpanContextImpl newContext(final Object traceId, final Object parentSpanId,
final Object spanId, final Map<String, String> baggages) {
return new SpanContextImpl(traceId, parentSpanId, spanId, baggages);
}
}
@@ -25,7 +25,6 @@
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;

@@ -37,7 +36,7 @@

public class SpanBuilderImpl implements Tracer.SpanBuilder {

private final Tracer tracer;
private final GeronimoTracer tracer;

private final Consumer<SpanImpl> onFinish;

@@ -49,18 +48,14 @@ public class SpanBuilderImpl implements Tracer.SpanBuilder {

private final IdGenerator idGenerator;

private final BiFunction<Object, Map<String, String>, SpanContextImpl> contextFactory;

private boolean ignoreActiveSpan;

private long timestamp = -1;

public SpanBuilderImpl(final Tracer tracer,
final BiFunction<Object, Map<String, String>, SpanContextImpl> contextFactory,
public SpanBuilderImpl(final GeronimoTracer tracer,
final Consumer<SpanImpl> onFinish, final String operationName,
final IdGenerator idGenerator) {
this.tracer = tracer;
this.contextFactory = contextFactory;
this.onFinish = onFinish;
this.operationName = operationName;
this.idGenerator = idGenerator;
@@ -139,14 +134,9 @@ public Span startManual() {
false))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
final SpanContextImpl context = parent == null ?
newSpanContext(baggages, idGenerator.next()) :
newSpanContext(baggages, parent.getValue().getTraceId());
final Object parentId = parent == null ? null : parent.getValue().getSpanId();
return new SpanImpl(operationName, timestamp, references, tags, onFinish, context, parentId);
}

private SpanContextImpl newSpanContext(final Map<String, String> baggages, final Object parent) {
return contextFactory.apply(parent, baggages);
tracer.newContext(idGenerator.next(), null, idGenerator.next(), baggages) :
tracer.newContext(parent.getValue().getTraceId(), parent.getValue().getSpanId(), idGenerator.next(), baggages);
return new SpanImpl(operationName, timestamp, references, tags, onFinish, context);
}

@Override
@@ -24,12 +24,15 @@ public class SpanContextImpl implements SpanContext {

private final Object traceId;

private final Object parentSpanId;

private final Object spanId;

private final Map<String, String> baggageItems;

public SpanContextImpl(final Object traceId, final Object spanId, final Map<String, String> baggageItems) {
public SpanContextImpl(final Object traceId, final Object parentSpanId, final Object spanId, final Map<String, String> baggageItems) {
this.traceId = traceId;
this.parentSpanId = parentSpanId;
this.spanId = spanId;
this.baggageItems = baggageItems;
}
@@ -42,6 +45,10 @@ public Object getTraceId() {
return traceId;
}

public Object getParentSpanId() {
return parentSpanId;
}

public Object getSpanId() {
return spanId;
}
@@ -55,6 +62,7 @@ public Iterable<Map.Entry<String, String>> baggageItems() {
public String toString() {
return "SpanContextImpl{" +
"traceId=" + traceId +
", parentSpanId=" + parentSpanId +
", spanId=" + spanId +
", baggageItems=" + baggageItems +
'}';
@@ -40,22 +40,19 @@ public class SpanImpl implements Span {

private final long startTimestamp;

private final Object parentId;

private String operationName;

private long finishTimestamp;

private final Collection<Log> logs = new ArrayList<>();

public SpanImpl(final String operationName, final long startTimestamp, final Collection<ReferenceImpl> references,
final Map<String, Object> tags, final Consumer<SpanImpl> onFinish, final SpanContextImpl context, final Object parentId) {
final Map<String, Object> tags, final Consumer<SpanImpl> onFinish, final SpanContextImpl context) {
this.operationName = operationName;
this.startTimestamp = startTimestamp;
this.references = references;
this.tags = tags;
this.context = context;
this.parentId = parentId;
this.onFinish = onFinish;
}

@@ -141,7 +138,6 @@ public Span setOperationName(final String operationName) {
public String toString() {
return "SpanImpl{" +
" id=" + context.getSpanId() +
", parentId=" + parentId +
", operationName='" + operationName + '\'' +
", references=" + references +
", tags=" + tags +
@@ -160,7 +156,7 @@ public Object getTraceId() {
}

public Object getParentId() {
return parentId;
return context.getParentSpanId();
}

public String getName() {
@@ -54,8 +54,9 @@ public synchronized void reset() {
}

@Override
protected SpanContextImpl newContext(final Object traceId, final Object spanId, final Map<String, String> baggages) {
return new TckSpanContext(traceId, spanId, baggages);
protected SpanContextImpl newContext(final Object traceId, final Object parentSpanId,
final Object spanId, final Map<String, String> baggages) {
return new TckSpanContext(traceId, parentSpanId, spanId, baggages);
}

@Override
@@ -173,15 +174,20 @@ public TckLog(final long timestampMicros, final Map<String, ?> fields) {
}

public static class TckSpanContext extends SpanContextImpl {
private TckSpanContext(final Object traceId, final Object spanId, final Map<String, String> baggages) {
super(traceId, spanId, baggages);
private TckSpanContext(final Object traceId, final Object parentSpanId, final Object spanId, final Map<String, String> baggages) {
super(traceId, parentSpanId, spanId, baggages);
}

public Object traceId() {
final Object traceId = getTraceId();
return traceId == null ? 0L : Long.parseLong(traceId.toString());
}

public Object parentSpanId() {
final Object spanId = getParentSpanId();
return spanId == null ? 0L : Long.parseLong(spanId.toString());
}

public Object spanId() {
final Object spanId = getSpanId();
return spanId == null ? 0L : Long.parseLong(spanId.toString());

0 comments on commit 4566c59

Please sign in to comment.