-
Notifications
You must be signed in to change notification settings - Fork 5
Trace context support in propagation Inject/Extract #105
base: master
Are you sure you want to change the base?
Changes from all commits
c4b42ad
0b0d4a4
e9a91c9
87192ee
72e1070
8bb9f59
689d561
cdf1cdb
fb9de8a
8398f84
488b927
8ec9666
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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,8 @@ package tracer | |||||
|
||||||
import ( | ||||||
"encoding/binary" | ||||||
"fmt" | ||||||
"github.com/google/uuid" | ||||||
"io" | ||||||
"strconv" | ||||||
"strings" | ||||||
|
@@ -26,6 +28,8 @@ const ( | |||||
fieldNameTraceID = prefixTracerState + "traceid" | ||||||
fieldNameSpanID = prefixTracerState + "spanid" | ||||||
fieldNameSampled = prefixTracerState + "sampled" | ||||||
|
||||||
traceParentKey = "traceparent" | ||||||
) | ||||||
|
||||||
func (p *textMapPropagator) Inject( | ||||||
|
@@ -40,13 +44,29 @@ func (p *textMapPropagator) Inject( | |||||
if !ok { | ||||||
return opentracing.ErrInvalidCarrier | ||||||
} | ||||||
carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16)) | ||||||
|
||||||
traceId := strings.Replace(sc.TraceID.String(), "-", "", -1) | ||||||
|
||||||
carrier.Set(fieldNameTraceID, traceId) | ||||||
carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16)) | ||||||
carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled)) | ||||||
|
||||||
tpSampled := "00" | ||||||
if sc.Sampled { | ||||||
tpSampled = "01" | ||||||
} | ||||||
traceParentValue := fmt.Sprintf("%v-%v-%016x-%v", | ||||||
"00", // Version 0 | ||||||
traceId, // 16bytes TraceId | ||||||
sc.SpanID, // 8bytes SpanId | ||||||
tpSampled, // 00 for not sampled, 01 for sampled | ||||||
) | ||||||
carrier.Set(traceParentKey, traceParentValue) | ||||||
|
||||||
for k, v := range sc.Baggage { | ||||||
carrier.Set(prefixBaggage+k, v) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
|
@@ -58,14 +78,46 @@ func (p *textMapPropagator) Extract( | |||||
return nil, opentracing.ErrInvalidCarrier | ||||||
} | ||||||
requiredFieldCount := 0 | ||||||
var traceID, spanID uint64 | ||||||
var traceID uuid.UUID | ||||||
var spanID uint64 | ||||||
var sampled bool | ||||||
var err error | ||||||
decodedBaggage := make(map[string]string) | ||||||
|
||||||
err = carrier.ForeachKey(func(k, v string) error { | ||||||
switch strings.ToLower(k) { | ||||||
case traceParentKey: | ||||||
if len(v) < 55 { | ||||||
return opentracing.ErrSpanContextCorrupted | ||||||
} | ||||||
traceParentArray := strings.Split(v, "-") | ||||||
if len(traceParentArray) < 4 || traceParentArray[0] != "00" || len(traceParentArray[1]) != 32 || len(traceParentArray[2]) != 16 { | ||||||
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.
Suggested change
|
||||||
return opentracing.ErrSpanContextCorrupted | ||||||
} | ||||||
|
||||||
traceID, err = uuid.Parse(traceParentArray[1]) | ||||||
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. In the |
||||||
if err != nil { | ||||||
return opentracing.ErrSpanContextCorrupted | ||||||
} | ||||||
spanID, err = strconv.ParseUint(traceParentArray[2], 16, 64) | ||||||
if err != nil { | ||||||
return opentracing.ErrSpanContextCorrupted | ||||||
} | ||||||
if traceParentArray[3] == "01" { | ||||||
sampled = true | ||||||
} | ||||||
requiredFieldCount = requiredFieldCount + 3 | ||||||
default: | ||||||
// Balance off the requiredFieldCount++ just below... | ||||||
requiredFieldCount-- | ||||||
} | ||||||
requiredFieldCount++ | ||||||
return nil | ||||||
}) | ||||||
err = carrier.ForeachKey(func(k, v string) error { | ||||||
switch strings.ToLower(k) { | ||||||
case fieldNameTraceID: | ||||||
traceID, err = strconv.ParseUint(v, 16, 64) | ||||||
traceID, err = uuid.Parse(v) | ||||||
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 change implies all agents need to send UUID, isn't? |
||||||
if err != nil { | ||||||
return opentracing.ErrSpanContextCorrupted | ||||||
} | ||||||
|
@@ -93,6 +145,7 @@ func (p *textMapPropagator) Extract( | |||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
if requiredFieldCount < tracerStateFieldCount { | ||||||
if requiredFieldCount == 0 { | ||||||
return nil, opentracing.ErrSpanContextNotFound | ||||||
|
@@ -122,7 +175,9 @@ func (p *binaryPropagator) Inject( | |||||
} | ||||||
|
||||||
state := wire.TracerState{} | ||||||
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. Just curiosity, are we using the binary protocol? Is there any standard for that? |
||||||
state.TraceId = sc.TraceID | ||||||
bytes, _ := sc.TraceID.MarshalBinary() | ||||||
state.TraceIdHi = binary.BigEndian.Uint64(bytes[:8]) | ||||||
state.TraceIdLo = binary.BigEndian.Uint64(bytes[8:]) | ||||||
state.SpanId = sc.SpanID | ||||||
state.Sampled = sc.Sampled | ||||||
state.BaggageItems = sc.Baggage | ||||||
|
@@ -171,8 +226,12 @@ func (p *binaryPropagator) Extract( | |||||
return nil, opentracing.ErrSpanContextCorrupted | ||||||
} | ||||||
|
||||||
traceIdBytes := make([]byte, 16) | ||||||
binary.BigEndian.PutUint64(traceIdBytes[:8], ctx.TraceIdHi) | ||||||
binary.BigEndian.PutUint64(traceIdBytes[8:], ctx.TraceIdLo) | ||||||
traceID, _ := uuid.FromBytes(traceIdBytes) | ||||||
return SpanContext{ | ||||||
TraceID: ctx.TraceId, | ||||||
TraceID: traceID, | ||||||
SpanID: ctx.SpanId, | ||||||
Sampled: ctx.Sampled, | ||||||
Baggage: ctx.BaggageItems, | ||||||
|
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.
I cannot locate the
tracestate
header for the baggage in OTel format.