-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathpropagation.go
157 lines (142 loc) · 4.33 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
// Copyright 2022 The OpenZipkin 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 zipkintracer
import (
"net/http"
"strings"
opentracing "github.com/opentracing/opentracing-go"
"github.com/openzipkin/zipkin-go/model"
"github.com/openzipkin/zipkin-go/propagation"
"github.com/openzipkin/zipkin-go/propagation/b3"
)
// DelegatingCarrier is a flexible carrier interface which can be implemented
// by types which have a means of storing the trace metadata and already know
// how to serialize themselves
type DelegatingCarrier interface {
State() (model.SpanContext, error)
SetState(model.SpanContext) error
}
type textMapPropagator struct {
tracer *tracerImpl
}
func (p *textMapPropagator) Inject(
spanContext opentracing.SpanContext,
opaqueCarrier interface{},
) error {
sc, ok := spanContext.(SpanContext)
if !ok {
return opentracing.ErrInvalidSpanContext
}
// native zipkin-go injector
if injector, ok := opaqueCarrier.(propagation.Injector); ok {
return injector(model.SpanContext(sc))
}
// fallback to support native opentracing http carrier
if httpCarrier, ok := opaqueCarrier.(opentracing.HTTPHeadersCarrier); ok {
req := &http.Request{Header: http.Header(httpCarrier)}
switch p.tracer.opts.b3InjectOpt {
case B3InjectSingle:
return b3.InjectHTTP(req, b3.WithSingleHeaderOnly())(model.SpanContext(sc))
case B3InjectBoth:
return b3.InjectHTTP(req, b3.WithSingleAndMultiHeader())(model.SpanContext(sc))
default:
return b3.InjectHTTP(req)(model.SpanContext(sc))
}
}
// fallback to support native opentracing textmap writer
if carrier, ok := opaqueCarrier.(opentracing.TextMapWriter); ok {
var (
err error
m = make(b3.Map)
)
switch p.tracer.opts.b3InjectOpt {
case B3InjectSingle:
err = m.Inject(b3.WithSingleHeaderOnly())(model.SpanContext(sc))
case B3InjectBoth:
err = m.Inject(b3.WithSingleAndMultiHeader())(model.SpanContext(sc))
default:
err = m.Inject()(model.SpanContext(sc))
}
if err != nil {
return err
}
for k, v := range m {
carrier.Set(k, v)
}
return nil
}
return opentracing.ErrInvalidCarrier
}
func (p *textMapPropagator) Extract(
opaqueCarrier interface{},
) (opentracing.SpanContext, error) {
if extractor, ok := opaqueCarrier.(propagation.Extractor); ok {
sc, err := extractor()
if sc != nil {
return SpanContext(*sc), err
}
return SpanContext{}, err
}
if httpCarrier, ok := opaqueCarrier.(opentracing.HTTPHeadersCarrier); ok {
req := &http.Request{Header: http.Header(httpCarrier)}
sc, err := b3.ExtractHTTP(req)()
if sc != nil {
return SpanContext(*sc), err
}
return SpanContext{}, err
}
if carrier, ok := opaqueCarrier.(opentracing.TextMapReader); ok {
m := make(b3.Map)
carrier.ForeachKey(func(key string, val string) error {
// no matter the format of the B3 headers, they will be retrieved
// using the standard lowercase format e.g. x-b3-traceid. See
// https://github.com/openzipkin/zipkin-go/blob/master/propagation/b3/shared.go
m[strings.ToLower(key)] = val
return nil
})
sc, err := m.Extract()
if sc != nil {
return SpanContext(*sc), err
}
return SpanContext{}, err
}
return nil, opentracing.ErrUnsupportedFormat
}
type accessorPropagator struct {
tracer *tracerImpl
}
func (p *accessorPropagator) Inject(
spanContext opentracing.SpanContext,
opaqueCarrier interface{},
) error {
dc, ok := opaqueCarrier.(DelegatingCarrier)
if !ok || dc == nil {
return opentracing.ErrInvalidCarrier
}
sc, ok := spanContext.(SpanContext)
if !ok {
return opentracing.ErrInvalidSpanContext
}
return dc.SetState(model.SpanContext(sc))
}
func (p *accessorPropagator) Extract(
opaqueCarrier interface{},
) (opentracing.SpanContext, error) {
dc, ok := opaqueCarrier.(DelegatingCarrier)
if !ok || dc == nil {
return nil, opentracing.ErrInvalidCarrier
}
sc, err := dc.State()
return SpanContext(sc), err
}