-
Notifications
You must be signed in to change notification settings - Fork 1
/
propagator.go
85 lines (71 loc) · 1.68 KB
/
propagator.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
package trace
import (
"net/http"
"google.golang.org/grpc/metadata"
)
const (
HttpFormat = iota
GrpcFormat
)
var (
emptyHttpPropagator httpPropagator
emptyGrpcPropagator grpcPropagator
)
type (
Propagator interface {
Extract(carrier interface{}) (Carrier, error)
Inject(carrier interface{}) (Carrier, error)
}
httpPropagator struct{}
grpcPropagator struct{}
)
func (h httpPropagator) Extract(carrier interface{}) (Carrier, error) {
if c, ok := carrier.(http.Header); !ok {
return nil, ErrInvalidCarrier
} else {
return httpCarrier(c), nil
}
}
func (h httpPropagator) Inject(carrier interface{}) (Carrier, error) {
if c, ok := carrier.(http.Header); ok {
return httpCarrier(c), nil
} else {
return nil, ErrInvalidCarrier
}
}
func (g grpcPropagator) Extract(carrier interface{}) (Carrier, error) {
if c, ok := carrier.(metadata.MD); ok {
return grpcCarrier(c), nil
} else {
return nil, ErrInvalidCarrier
}
}
func (g grpcPropagator) Inject(carrier interface{}) (Carrier, error) {
if c, ok := carrier.(metadata.MD); ok {
return grpcCarrier(c), nil
} else {
return nil, ErrInvalidCarrier
}
}
func Extract(format, carrier interface{}) (Carrier, error) {
switch v := format.(type) {
case int:
if v == HttpFormat {
return emptyHttpPropagator.Extract(carrier)
} else if v == GrpcFormat {
return emptyGrpcPropagator.Extract(carrier)
}
}
return nil, ErrInvalidCarrier
}
func Inject(format, carrier interface{}) (Carrier, error) {
switch v := format.(type) {
case int:
if v == HttpFormat {
return emptyHttpPropagator.Inject(carrier)
} else if v == GrpcFormat {
return emptyGrpcPropagator.Inject(carrier)
}
}
return nil, ErrInvalidCarrier
}