-
Notifications
You must be signed in to change notification settings - Fork 53
/
propagation_binary.go
118 lines (105 loc) · 2.79 KB
/
propagation_binary.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package lightstep
import (
"encoding/base64"
"io"
"io/ioutil"
"github.com/gogo/protobuf/proto"
lightstep "github.com/lightstep/lightstep-tracer-common/golang/gogo/lightsteppb"
"github.com/opentracing/opentracing-go"
)
// BinaryCarrier is used as the format parameter in inject/extract for lighstep binary propagation.
const BinaryCarrier = opentracing.Binary
// BinaryPropagator propagates context in binary format
var BinaryPropagator binaryPropagator
type binaryPropagator struct{}
func (binaryPropagator) Inject(
spanContext opentracing.SpanContext,
opaqueCarrier interface{},
) error {
sc, ok := spanContext.(SpanContext)
if !ok {
return opentracing.ErrInvalidSpanContext
}
data, err := proto.Marshal(&lightstep.BinaryCarrier{
BasicCtx: &lightstep.BasicTracerCarrier{
TraceId: sc.TraceID,
SpanId: sc.SpanID,
Sampled: sc.Sampled == "" || sc.Sampled == "true" || sc.Sampled == "1",
BaggageItems: sc.Baggage,
},
})
if err != nil {
return err
}
switch carrier := opaqueCarrier.(type) {
case io.Writer:
buf := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(buf, data)
_, err = carrier.Write(buf)
return err
case *string:
*carrier = base64.StdEncoding.EncodeToString(data)
case *[]byte:
*carrier = make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(*carrier, data)
default:
return opentracing.ErrInvalidCarrier
}
return nil
}
func (binaryPropagator) Extract(
opaqueCarrier interface{},
) (opentracing.SpanContext, error) {
var data []byte
var err error
// Decode from string, *string, *[]byte, or []byte
switch carrier := opaqueCarrier.(type) {
case io.Reader:
buf, err := ioutil.ReadAll(carrier)
if err != nil {
return nil, err
}
data, err = decodeBase64Bytes(buf)
case *string:
if carrier != nil {
data, err = base64.StdEncoding.DecodeString(*carrier)
}
case string:
data, err = base64.StdEncoding.DecodeString(carrier)
case *[]byte:
if carrier != nil {
data, err = decodeBase64Bytes(*carrier)
}
case []byte:
data, err = decodeBase64Bytes(carrier)
default:
return nil, opentracing.ErrInvalidCarrier
}
if err != nil {
return nil, err
}
pb := &lightstep.BinaryCarrier{}
if err := proto.Unmarshal(data, pb); err != nil {
return nil, err
}
if pb.BasicCtx == nil {
return nil, opentracing.ErrInvalidCarrier
}
spanContext := SpanContext{
TraceID: pb.BasicCtx.TraceId,
SpanID: pb.BasicCtx.SpanId,
Baggage: pb.BasicCtx.BaggageItems,
}
if !pb.BasicCtx.Sampled {
spanContext.Sampled = "false"
}
return spanContext, nil
}
func decodeBase64Bytes(in []byte) ([]byte, error) {
data := make([]byte, base64.StdEncoding.DecodedLen(len(in)))
n, err := base64.StdEncoding.Decode(data, in)
if err != nil {
return nil, err
}
return data[:n], nil
}