Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit e9a91c9

Browse files
committed
Trace context support in propagation Inject/Extract
1 parent 0b0d4a4 commit e9a91c9

File tree

2 files changed

+50
-97
lines changed

2 files changed

+50
-97
lines changed

tracer/propagation_ot.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tracer
22

33
import (
44
"encoding/binary"
5+
"fmt"
56
"io"
67
"strconv"
78
"strings"
@@ -26,6 +27,9 @@ const (
2627
fieldNameTraceID = prefixTracerState + "traceid"
2728
fieldNameSpanID = prefixTracerState + "spanid"
2829
fieldNameSampled = prefixTracerState + "sampled"
30+
31+
traceParentKey = "traceparent"
32+
traceStateKey = "tracestate"
2933
)
3034

3135
func (p *textMapPropagator) Inject(
@@ -44,9 +48,28 @@ func (p *textMapPropagator) Inject(
4448
carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16))
4549
carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled))
4650

51+
tpSampled := "00"
52+
if sc.Sampled {
53+
tpSampled = "01"
54+
}
55+
traceParentValue := fmt.Sprintf("%v-%032x-%016x-%v",
56+
"00", // Version 0
57+
sc.TraceID, // 8bytes TraceId
58+
sc.SpanID, // 8bytes SpanId
59+
tpSampled, // 00 for not sampled, 01 for sampled
60+
)
61+
carrier.Set(traceParentKey, traceParentValue)
62+
63+
var traceStatePairs []string
64+
4765
for k, v := range sc.Baggage {
4866
carrier.Set(prefixBaggage+k, v)
67+
traceStatePairs = append(traceStatePairs, k+"="+v)
4968
}
69+
70+
traceStateValue := strings.Join(traceStatePairs, ",")
71+
carrier.Set(traceStateKey, traceStateValue)
72+
5073
return nil
5174
}
5275

@@ -79,6 +102,33 @@ func (p *textMapPropagator) Extract(
79102
if err != nil {
80103
return opentracing.ErrSpanContextCorrupted
81104
}
105+
case traceParentKey:
106+
if len(v) < 55 {
107+
return opentracing.ErrSpanContextCorrupted
108+
}
109+
traceParentArray := strings.Split(v, "-")
110+
if len(traceParentArray) < 4 || traceParentArray[0] != "00" || len(traceParentArray[1]) != 32 || len(traceParentArray[2]) != 16 {
111+
return opentracing.ErrSpanContextCorrupted
112+
}
113+
114+
traceID, err = strconv.ParseUint(traceParentArray[1][16:], 16, 64)
115+
if err != nil {
116+
return opentracing.ErrSpanContextCorrupted
117+
}
118+
spanID, err = strconv.ParseUint(traceParentArray[2], 16, 64)
119+
if err != nil {
120+
return opentracing.ErrSpanContextCorrupted
121+
}
122+
if traceParentArray[3] == "01" {
123+
sampled = true
124+
}
125+
case traceStateKey:
126+
traceStateArray := strings.Split(v, ",")
127+
for _, stItem := range traceStateArray {
128+
stItem = strings.TrimSpace(stItem)
129+
stItemArray := strings.Split(stItem, "=")
130+
decodedBaggage[stItemArray[0]] = stItemArray[1]
131+
}
82132
default:
83133
lowercaseK := strings.ToLower(k)
84134
if strings.HasPrefix(lowercaseK, prefixBaggage) {

tracer/propagation_trace_context.go

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)