forked from qustavo/dd-go-opentracing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
propagation.go
76 lines (64 loc) · 1.83 KB
/
propagation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ddtracer
import (
"strconv"
"strings"
"github.com/DataDog/dd-trace-go/tracer"
opentracing "github.com/opentracing/opentracing-go"
)
const (
tracePrefix = "dd-trace-"
fieldSpanID = tracePrefix + "spanid"
fieldTraceID = tracePrefix + "traceid"
fieldParentID = tracePrefix + "parentid"
//fieldSampled = tracePrefix + "sampled"
)
type textMapPropagator struct {
t *Tracer
}
// Inject takes a span and a supplied carrier and injects the span into the carrier
func (p *textMapPropagator) Inject(span *tracer.Span, carrier interface{}) error {
tm, ok := carrier.(opentracing.TextMapWriter)
if !ok {
return opentracing.ErrInvalidCarrier
}
tm.Set(fieldSpanID, strconv.FormatUint(span.SpanID, 16))
tm.Set(fieldTraceID, strconv.FormatUint(span.TraceID, 16))
if span.ParentID > 0 {
tm.Set(fieldParentID, strconv.FormatUint(span.ParentID, 16))
}
return nil
}
// Extract a full span object from a supplied carrier if one exists
func (p *textMapPropagator) Extract(carrier interface{}) (opentracing.SpanContext, error) {
tm, ok := carrier.(opentracing.TextMapReader)
if !ok {
return nil, opentracing.ErrInvalidCarrier
}
var err error
var spanID, traceID, parentID uint64
err = tm.ForeachKey(func(k, v string) error {
switch strings.ToLower(k) {
case fieldSpanID:
spanID, err = strconv.ParseUint(v, 16, 64)
if err != nil {
return opentracing.ErrSpanContextCorrupted
}
case fieldTraceID:
traceID, err = strconv.ParseUint(v, 16, 64)
if err != nil {
return opentracing.ErrSpanContextCorrupted
}
case fieldParentID:
parentID, err = strconv.ParseUint(v, 16, 64)
if err != nil {
return opentracing.ErrSpanContextCorrupted
}
}
return nil
})
span := &Span{
tracer.NewSpan("", DefaultService, DefaultResource, spanID, traceID, parentID, p.t.Tracer),
p.t,
}
return span.Context(), err
}