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
|
const (
binHdrSuffix = "-bin"
)
var grpcTag = opentracing.Tag{Key: string(ext.Component), Value: "gRPC"}
// Interceptor for wrap gRPC Client Calls
func ClientInterceptor(tracer opentracing.Tracer) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
newCtx, clientSpan := newClientSpanFromContext(ctx, tracer, method)
// add custom tags to client span
clientSpan.SetTag("grpc.target", cc.Target())
clientSpan.SetTag("grpc.method", method)
// invoke grpc call
err := invoker(newCtx, method, req, reply, cc, opts...)
// finish client span
finishClientSpan(clientSpan, err)
return err
}
}
// metadataTextMap extends a metadata.MD to be an opentracing textmap
type metadataTextMap metadata.MD
// Set is a opentracing.TextMapReader interface that extracts values.
func (m metadataTextMap) Set(key, val string) {
// gRPC allows for complex binary values to be written.
encodedKey, encodedVal := encodeKeyValue(key, val)
// The metadata object is a multiMap, and previous values may exist, but for opentracing headers, we do not append
// we just override.
m[encodedKey] = []string{encodedVal}
}
// ForeachKey is a opentracing.TextMapReader interface that extracts values.
func (m metadataTextMap) ForeachKey(callback func(key, val string) error) error {
for k, vv := range m {
for _, v := range vv {
if err := callback(k, v); err != nil {
return err
}
}
}
return nil
}
// encodeKeyValue encodes key and value qualified for transmission via gRPC.
func encodeKeyValue(k, v string) (string, string) {
k = strings.ToLower(k)
if strings.HasSuffix(k, binHdrSuffix) {
val := base64.StdEncoding.EncodeToString([]byte(v))
v = val
}
return k, v
}
type clientSpanTagKey struct{}
func newClientSpanFromContext(ctx context.Context, tracer opentracing.Tracer, operateName string) (context.Context, opentracing.Span) {
var parentSpanCtx opentracing.SpanContext
// fetch possible existed parent context which withValue of span from ctx
if parent := opentracing.SpanFromContext(ctx); parent != nil {
parentSpanCtx = parent.Context()
}
opts := []opentracing.StartSpanOption{
opentracing.ChildOf(parentSpanCtx),
ext.SpanKindRPCClient,
grpcTag,
}
// check ctx, if contains StartSpanOption
if tag := ctx.Value(clientSpanTagKey{}); tag != nil {
if opt, ok := tag.(opentracing.StartSpanOption); ok {
opts = append(opts, opt)
}
}
clientSpan := tracer.StartSpan(operateName, opts...)
// create new textMap carrier
md := metadataTextMap{}
// inject data from current span to textMap carrier
if err := tracer.Inject(clientSpan.Context(), opentracing.HTTPHeaders, md); err != nil {
// record inject error, can't panic, will break the span
clientSpan.LogFields(log.String("inject-error", err.Error()))
}
// set metadata to ctx
ctxWithMetadata := metadata.NewOutgoingContext(ctx, metadata.MD(md))
return opentracing.ContextWithSpan(ctxWithMetadata, clientSpan), clientSpan
}
func finishClientSpan(clientSpan opentracing.Span, err error) {
if err != nil && err != io.EOF {
ext.Error.Set(clientSpan, true)
clientSpan.LogFields(log.String("invoke-error", err.Error()))
}
clientSpan.Finish()
}
|