-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagation.go
216 lines (190 loc) · 6.69 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2021 The Houyi Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package houyi
import (
"bytes"
"encoding/binary"
"github.com/opentracing/opentracing-go"
"io"
"net/url"
"strings"
"sync"
)
// Injector and Extractor are APIs defined in opentracing-go,
// see in [https://github.com/opentracing/opentracing-go/blob/master/mocktracer/propagation.go].
// Injector is responsible for injecting SpanContext instances in a manner suitable
// for propagation via a format-specific "carrier" object. Typically the
// injection will take place across an RPC boundary, but message queues and
// other IPC mechanisms are also reasonable places to use an Injector.
type Injector interface {
// Inject takes `SpanContext` and injects it into `carrier`. The actual type
// of `carrier` depends on the `format` passed to `Tracer.Inject()`.
//
// Implementations may return opentracing.ErrInvalidCarrier or any other
// implementation-specific error if injection fails.
Inject(ctx SpanContext, carrier interface{}) error
}
// Extractor is responsible for extracting SpanContext instances from a
// format-specific "carrier" object. Typically the extraction will take place
// on the server side of an RPC boundary, but message queues and other IPC
// mechanisms are also reasonable places to use an Extractor.
type Extractor interface {
// Extract decodes a SpanContext instance from the given `carrier`,
// or (nil, opentracing.ErrSpanContextNotFound) if no context could
// be found in the `carrier`.
Extract(carrier interface{}) (SpanContext, error)
}
type HttpHeadersPropagator struct{}
func (p *HttpHeadersPropagator) Inject(ctx SpanContext, carrier interface{}) error {
writer, ok := carrier.(opentracing.TextMapWriter)
if !ok {
return opentracing.ErrInvalidCarrier
}
writer.Set(TraceContextHeaderName, ctx.String())
for baggageKey, baggageVal := range ctx.baggage {
safeVal := url.QueryEscape(baggageVal)
writer.Set(TraceBaggageHeaderPrefix+baggageKey, safeVal)
}
return nil
}
func (p *HttpHeadersPropagator) Extract(carrier interface{}) (SpanContext, error) {
reader, ok := carrier.(opentracing.TextMapReader)
if !ok {
return emptyContext, opentracing.ErrInvalidCarrier
}
var retCtx SpanContext
err := reader.ForeachKey(func(key, val string) error {
lowerKer := strings.ToLower(key)
switch {
case lowerKer == TraceContextHeaderName:
var err error
if err = ContextFromString(&retCtx, val); err != nil {
return err
}
case strings.HasPrefix(lowerKer, TraceBaggageHeaderPrefix):
if retCtx.baggage == nil {
retCtx.baggage = make(map[string]string)
}
if safeVal, err := url.QueryUnescape(val); err != nil {
return err
} else {
retCtx.baggage[p.removePrefix(lowerKer)] = safeVal
}
}
return nil
})
if err != nil {
return SpanContext{}, err
}
if retCtx.traceID.Low == 0 || retCtx.traceID.High == 0 || retCtx.spanID == 0 {
return emptyContext, opentracing.ErrSpanContextNotFound
}
return retCtx, nil
}
func (p *HttpHeadersPropagator) removePrefix(key string) string {
return key[len(TraceBaggageHeaderPrefix):]
}
type BinaryPropagator struct {
buffers sync.Pool
}
func (p *BinaryPropagator) Inject(ctx SpanContext, carrier interface{}) error {
writer, ok := carrier.(io.Writer)
if !ok {
return opentracing.ErrInvalidCarrier
}
// Handle the houyiTracer context
if err := binary.Write(writer, binary.BigEndian, ctx.traceID.High); err != nil {
return err
}
if err := binary.Write(writer, binary.BigEndian, ctx.traceID.Low); err != nil {
return err
}
if err := binary.Write(writer, binary.BigEndian, ctx.spanID); err != nil {
return err
}
if err := binary.Write(writer, binary.BigEndian, ctx.parentID); err != nil {
return err
}
if err := binary.Write(writer, binary.BigEndian, ctx.flags); err != nil {
return err
}
// Handle the baggage items
if err := binary.Write(writer, binary.BigEndian, int32(len(ctx.baggage))); err != nil {
return err
}
for k, v := range ctx.baggage {
if err := binary.Write(writer, binary.BigEndian, int32(len(k))); err != nil {
return err
}
io.WriteString(writer, k)
if err := binary.Write(writer, binary.BigEndian, int32(len(v))); err != nil {
return err
}
io.WriteString(writer, v)
}
return nil
}
func (p *BinaryPropagator) Extract(carrier interface{}) (SpanContext, error) {
reader, ok := carrier.(io.Reader)
if !ok {
return emptyContext, opentracing.ErrInvalidCarrier
}
var ctx SpanContext
if err := binary.Read(reader, binary.BigEndian, &ctx.traceID.High); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if err := binary.Read(reader, binary.BigEndian, &ctx.traceID.Low); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if err := binary.Read(reader, binary.BigEndian, &ctx.spanID); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if err := binary.Read(reader, binary.BigEndian, &ctx.parentID); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if err := binary.Read(reader, binary.BigEndian, &ctx.flags); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
var nBaggage int32
if err := binary.Read(reader, binary.BigEndian, &nBaggage); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
if inBaggage := int(nBaggage); inBaggage > 0 {
ctx.baggage = make(map[string]string, inBaggage)
buf := p.buffers.Get().(*bytes.Buffer)
defer p.buffers.Put(buf)
var keyLen, valLen int32
for i := 0; i < inBaggage; i++ {
if err := binary.Read(reader, binary.BigEndian, &keyLen); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
buf.Reset()
buf.Grow(int(keyLen))
if n, err := io.CopyN(buf, reader, int64(keyLen)); err != nil || int32(n) != keyLen {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
key := buf.String()
if err := binary.Read(reader, binary.BigEndian, &valLen); err != nil {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
buf.Reset()
buf.Grow(int(valLen))
if n, err := io.CopyN(buf, reader, int64(valLen)); err != nil || int32(n) != valLen {
return emptyContext, opentracing.ErrSpanContextCorrupted
}
ctx.baggage[key] = buf.String()
}
}
return ctx, nil
}