-
Notifications
You must be signed in to change notification settings - Fork 232
Add baggage to B3 codec #438
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
package io.jaegertracing.propagation; | ||
|
||
import io.jaegertracing.SpanContext; | ||
import io.jaegertracing.Tracer; | ||
import io.jaegertracing.baggage.BaggageRestrictionManager; | ||
import io.opentracing.propagation.TextMap; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
|
@@ -34,17 +37,34 @@ | |
* | ||
* <p> | ||
* See <a href="http://zipkin.io/pages/instrumenting.html">Instrumenting a Library</a> | ||
* | ||
* Note that this codec automatically propagates baggage | ||
* (with {@value io.jaegertracing.propagation.B3TextMapCodec#BAGGAGE_NAME} prefix). | ||
* Baggage whitelisting can be configured in {@link BaggageRestrictionManager} and then | ||
* passed to {@link Tracer.Builder#baggageRestrictionManager} | ||
*/ | ||
public class B3TextMapCodec implements Codec<TextMap> { | ||
protected static final String TRACE_ID_NAME = "X-B3-TraceId"; | ||
protected static final String SPAN_ID_NAME = "X-B3-SpanId"; | ||
protected static final String PARENT_SPAN_ID_NAME = "X-B3-ParentSpanId"; | ||
protected static final String SAMPLED_NAME = "X-B3-Sampled"; | ||
protected static final String FLAGS_NAME = "X-B3-Flags"; | ||
protected static final String BAGGAGE_NAME = "baggage-"; | ||
// NOTE: uber's flags aren't the same as B3/Finagle ones | ||
protected static final byte SAMPLED_FLAG = 1; | ||
protected static final byte DEBUG_FLAG = 2; | ||
|
||
private static final PrefixedKeys keys = new PrefixedKeys(); | ||
private final String baggageName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto call it prefix instead? |
||
|
||
public B3TextMapCodec() { | ||
this(new Builder()); | ||
} | ||
|
||
private B3TextMapCodec(Builder builder) { | ||
this.baggageName = builder.baggagePrefix; | ||
} | ||
|
||
@Override | ||
public void inject(SpanContext spanContext, TextMap carrier) { | ||
carrier.put(TRACE_ID_NAME, HexCodec.toLowerHex(spanContext.getTraceId())); | ||
|
@@ -56,6 +76,9 @@ public void inject(SpanContext spanContext, TextMap carrier) { | |
if (spanContext.isDebug()) { | ||
carrier.put(FLAGS_NAME, "1"); | ||
} | ||
for (Map.Entry<String, String> entry : spanContext.baggageItems()) { | ||
carrier.put(keys.prefixedKey(entry.getKey(), baggageName), entry.getValue()); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -64,6 +87,7 @@ public SpanContext extract(TextMap carrier) { | |
Long spanId = null; | ||
Long parentId = 0L; // Conventionally, parent id == 0 means the root span | ||
byte flags = 0; | ||
Map<String, String> baggage = null; | ||
for (Map.Entry<String, String> entry : carrier) { | ||
if (entry.getKey().equalsIgnoreCase(SAMPLED_NAME)) { | ||
String value = entry.getValue(); | ||
|
@@ -80,12 +104,37 @@ public SpanContext extract(TextMap carrier) { | |
if (entry.getValue().equals("1")) { | ||
flags |= DEBUG_FLAG; | ||
} | ||
} else if (entry.getKey().startsWith(BAGGAGE_NAME)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be |
||
if (baggage == null) { | ||
baggage = new HashMap<String, String>(); | ||
} | ||
baggage.put(keys.unprefixedKey(entry.getKey(), BAGGAGE_NAME), entry.getValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
} | ||
|
||
if (null != traceId && null != parentId && null != spanId) { | ||
return new SpanContext(traceId, spanId, parentId, flags); | ||
SpanContext spanContext = new SpanContext(traceId, spanId, parentId, flags); | ||
if (baggage != null) { | ||
spanContext = spanContext.withBaggage(baggage); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is an interesting use case, what if only baggage is propagated without the rest of the trace context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the moment it's an invalid span context - null |
||
} | ||
return spanContext; | ||
} | ||
return null; | ||
} | ||
|
||
public static class Builder { | ||
private String baggagePrefix = BAGGAGE_NAME; | ||
|
||
/** | ||
* Specify baggage prefix. The default is {@value B3TextMapCodec#BAGGAGE_NAME} | ||
*/ | ||
public Builder withBaggagePrefix(String baggagePrefix) { | ||
this.baggagePrefix = baggagePrefix; | ||
return this; | ||
} | ||
|
||
public B3TextMapCodec build() { | ||
return new B3TextMapCodec(this); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/NAME/PREFIX?