This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathopentracing.go
135 lines (109 loc) · 3.96 KB
/
opentracing.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
/*
Copyright 2019 The Vitess 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 trace
import (
"encoding/base64"
"encoding/json"
"context"
otgrpc "github.com/opentracing-contrib/go-grpc"
"github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
"vitess.io/vitess/go/vt/vterrors"
)
var _ Span = (*openTracingSpan)(nil)
type openTracingSpan struct {
otSpan opentracing.Span
}
// Finish will mark a span as finished
func (js openTracingSpan) Finish() {
js.otSpan.Finish()
}
// Annotate will add information to an existing span
func (js openTracingSpan) Annotate(key string, value interface{}) {
js.otSpan.SetTag(key, value)
}
var _ tracingService = (*openTracingService)(nil)
type tracer interface {
GetOpenTracingTracer() opentracing.Tracer
}
type openTracingService struct {
Tracer tracer
}
// AddGrpcServerOptions is part of an interface implementation
func (jf openTracingService) AddGrpcServerOptions(addInterceptors func(s grpc.StreamServerInterceptor, u grpc.UnaryServerInterceptor)) {
ot := jf.Tracer.GetOpenTracingTracer()
addInterceptors(otgrpc.OpenTracingStreamServerInterceptor(ot), otgrpc.OpenTracingServerInterceptor(ot))
}
// AddGrpcClientOptions is part of an interface implementation
func (jf openTracingService) AddGrpcClientOptions(addInterceptors func(s grpc.StreamClientInterceptor, u grpc.UnaryClientInterceptor)) {
ot := jf.Tracer.GetOpenTracingTracer()
addInterceptors(otgrpc.OpenTracingStreamClientInterceptor(ot), otgrpc.OpenTracingClientInterceptor(ot))
}
// NewClientSpan is part of an interface implementation
func (jf openTracingService) NewClientSpan(parent Span, serviceName, label string) Span {
span := jf.New(parent, label)
span.Annotate("peer.service", serviceName)
return span
}
// New is part of an interface implementation
func (jf openTracingService) New(parent Span, label string) Span {
var innerSpan opentracing.Span
if parent == nil {
innerSpan = jf.Tracer.GetOpenTracingTracer().StartSpan(label)
} else {
jaegerParent := parent.(openTracingSpan)
span := jaegerParent.otSpan
innerSpan = jf.Tracer.GetOpenTracingTracer().StartSpan(label, opentracing.ChildOf(span.Context()))
}
return openTracingSpan{otSpan: innerSpan}
}
func extractMapFromString(in string) (opentracing.TextMapCarrier, error) {
decodedBytes, err := base64.StdEncoding.DecodeString(in)
if err != nil {
return nil, err
}
var dat opentracing.TextMapCarrier
err = json.Unmarshal(decodedBytes, &dat)
if err != nil {
return nil, err
}
return dat, nil
}
func (jf openTracingService) NewFromString(parent, label string) (Span, error) {
carrier, err := extractMapFromString(parent)
if err != nil {
return nil, err
}
spanContext, err := jf.Tracer.GetOpenTracingTracer().Extract(opentracing.TextMap, carrier)
if err != nil {
return nil, vterrors.Wrap(err, "failed to deserialize span context")
}
innerSpan := jf.Tracer.GetOpenTracingTracer().StartSpan(label, opentracing.ChildOf(spanContext))
return openTracingSpan{otSpan: innerSpan}, nil
}
// FromContext is part of an interface implementation
func (jf openTracingService) FromContext(ctx context.Context) (Span, bool) {
innerSpan := opentracing.SpanFromContext(ctx)
if innerSpan == nil {
return nil, false
}
return openTracingSpan{otSpan: innerSpan}, true
}
// NewContext is part of an interface implementation
func (jf openTracingService) NewContext(parent context.Context, s Span) context.Context {
span, ok := s.(openTracingSpan)
if !ok {
return nil
}
return opentracing.ContextWithSpan(parent, span.otSpan)
}