Skip to content

Commit

Permalink
Merge pull request #8 from opentracing/changes/split_text_fixes
Browse files Browse the repository at this point in the history
Make SplitTextCarrier work with superset maps
  • Loading branch information
bensigelman committed Mar 4, 2016
2 parents 5faedce + 0b70b0b commit 4e76772
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions propagation_ot.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ type goHTTPPropagator struct {
}

const (
fieldNameTraceID = "traceid"
fieldNameSpanID = "spanid"
fieldNameSampled = "sampled"
prefixTracerState = "ot-tracer-"
prefixBaggage = "ot-baggage-"

tracerStateFieldCount = 3
fieldNameTraceID = prefixTracerState + "traceid"
fieldNameSpanID = prefixTracerState + "spanid"
fieldNameSampled = prefixTracerState + "sampled"
)

func (p *splitTextPropagator) Inject(
Expand All @@ -42,18 +46,18 @@ func (p *splitTextPropagator) Inject(
return opentracing.ErrInvalidCarrier
}
if splitTextCarrier.TracerState == nil {
splitTextCarrier.TracerState = make(map[string]string, 3 /* see below */)
splitTextCarrier.TracerState = make(map[string]string, tracerStateFieldCount)
}
splitTextCarrier.TracerState[fieldNameTraceID] = strconv.FormatInt(sc.raw.TraceID, 10)
splitTextCarrier.TracerState[fieldNameSpanID] = strconv.FormatInt(sc.raw.SpanID, 10)
splitTextCarrier.TracerState[fieldNameTraceID] = strconv.FormatInt(sc.raw.TraceID, 16)
splitTextCarrier.TracerState[fieldNameSpanID] = strconv.FormatInt(sc.raw.SpanID, 16)
splitTextCarrier.TracerState[fieldNameSampled] = strconv.FormatBool(sc.raw.Sampled)

sc.Lock()
if l := len(sc.raw.Baggage); l > 0 && splitTextCarrier.Baggage == nil {
splitTextCarrier.Baggage = make(map[string]string, l)
}
for k, v := range sc.raw.Baggage {
splitTextCarrier.Baggage[k] = v
splitTextCarrier.Baggage[prefixBaggage+k] = v
}
sc.Unlock()
return nil
Expand All @@ -74,12 +78,12 @@ func (p *splitTextPropagator) Join(
for k, v := range splitTextCarrier.TracerState {
switch strings.ToLower(k) {
case fieldNameTraceID:
traceID, err = strconv.ParseInt(v, 10, 64)
traceID, err = strconv.ParseInt(v, 16, 64)
if err != nil {
return nil, opentracing.ErrTraceCorrupted
}
case fieldNameSpanID:
propagatedSpanID, err = strconv.ParseInt(v, 10, 64)
propagatedSpanID, err = strconv.ParseInt(v, 16, 64)
if err != nil {
return nil, opentracing.ErrTraceCorrupted
}
Expand All @@ -93,8 +97,17 @@ func (p *splitTextPropagator) Join(
}
requiredFieldCount++
}
const expFieldCount = 3
if requiredFieldCount < expFieldCount {
var decodedBaggage map[string]string
if splitTextCarrier.Baggage != nil {
decodedBaggage = make(map[string]string)
for k, v := range splitTextCarrier.Baggage {
lowercaseK := strings.ToLower(k)
if strings.HasPrefix(lowercaseK, prefixBaggage) {
decodedBaggage[strings.TrimPrefix(lowercaseK, prefixBaggage)] = v
}
}
}
if requiredFieldCount < tracerStateFieldCount {
if len(splitTextCarrier.TracerState) == 0 {
return nil, opentracing.ErrTraceNotFound
}
Expand All @@ -109,8 +122,8 @@ func (p *splitTextPropagator) Join(
ParentSpanID: propagatedSpanID,
Sampled: sampled,
},
Baggage: decodedBaggage,
}
sp.raw.Baggage = splitTextCarrier.Baggage

return p.tracer.startSpanInternal(
sp,
Expand Down

0 comments on commit 4e76772

Please sign in to comment.