-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
326 lines (300 loc) · 8.84 KB
/
tracing.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package tracing
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strings"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
jaegerConfig "github.com/uber/jaeger-client-go/config"
jaegerZap "github.com/uber/jaeger-client-go/log/zap"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
"github.com/kiga-hub/arc/logging"
microConf "github.com/kiga-hub/arc/micro/conf"
)
const (
skipSpanType = "opentracing.noopSpan"
)
// SetupGlobalTracer Set this overall tracer
//
//goland:noinspection GoUnusedExportedFunction
func SetupGlobalTracer(basic microConf.BasicConfig, trace TraceConfig, zlog *zap.Logger) (opentracing.Tracer, io.Closer, error) {
tracer, closer, err := CreateTracer(basic, trace, zlog)
if err == nil {
opentracing.SetGlobalTracer(tracer)
}
return tracer, closer, err
}
// CreateTracer create tracer
func CreateTracer(basic microConf.BasicConfig, trace TraceConfig, zlog *zap.Logger) (opentracing.Tracer, io.Closer, error) {
// tracing
traceCfg := jaegerConfig.Configuration{
ServiceName: basic.Service,
Sampler: &jaegerConfig.SamplerConfig{
Type: "const",
Param: 1,
},
Reporter: &jaegerConfig.ReporterConfig{
CollectorEndpoint: fmt.Sprintf("%s/api/traces", trace.JaegerCollectorAddr),
LogSpans: true,
BufferFlushInterval: 1 * time.Second,
},
}
opts := []jaegerConfig.Option{
jaegerConfig.Tag("zone", basic.Zone),
jaegerConfig.Tag("node", basic.Node),
jaegerConfig.Tag("machine", basic.Machine),
jaegerConfig.Tag("instance", basic.Instance),
jaegerConfig.Tag("service", basic.Service),
jaegerConfig.Tag("appName", basic.AppName),
jaegerConfig.Tag("appVersion", basic.AppVersion),
}
if zlog != nil {
opts = append(opts, jaegerConfig.Logger(jaegerZap.NewLogger(zlog)))
}
return traceCfg.NewTracer(opts...)
}
// MarshalSpanToJSON parse span to json
//
//goland:noinspection GoUnusedExportedFunction
func MarshalSpanToJSON(span opentracing.Span) (string, error) {
if span == nil {
return "", errors.New("nil span")
}
tc := opentracing.TextMapCarrier{}
err := span.Tracer().Inject(span.Context(), opentracing.TextMap, tc)
if err != nil {
return "", err
}
b, err := json.Marshal(tc)
if err != nil {
return "", err
}
return string(b[:]), nil
}
// UnmarshalJSONToCarrier unmarshal a JSON string to an opentracing.TextMapCarrier object
func UnmarshalJSONToCarrier(marshaled string) (opentracing.TextMapCarrier, error) {
tc := opentracing.TextMapCarrier{}
err := json.Unmarshal([]byte(marshaled), &tc)
return tc, err
}
// StartChildSpanFromJSON Start a subtask Span from json
//
//goland:noinspection GoUnusedExportedFunction
func StartChildSpanFromJSON(tracer opentracing.Tracer, operatorName, marshaled string) (opentracing.Span, error) {
if tracer == nil {
return nil, errors.New("tracer not set")
}
tc, err := UnmarshalJSONToCarrier(marshaled)
if err != nil {
return nil, err
}
ctx, err := tracer.Extract(opentracing.TextMap, tc)
if err != nil {
return nil, err
}
span := tracer.StartSpan(
operatorName,
opentracing.ChildOf(ctx),
)
return span, nil
}
// ContinueSpanFromJSON ContinueSpanFromJsons
//
//goland:noinspection GoUnusedExportedFunction
func ContinueSpanFromJSON(tracer opentracing.Tracer, operateName, marshaled string) (opentracing.Span, error) {
if tracer == nil {
return nil, errors.New("tracer not set")
}
tc, err := UnmarshalJSONToCarrier(marshaled)
if err != nil {
return nil, err
}
ctx, err := tracer.Extract(opentracing.TextMap, tc)
if err != nil {
return nil, err
}
if ctx == nil {
return nil, errors.New("no ctx found")
}
span := tracer.StartSpan(
operateName,
opentracing.FollowsFrom(ctx),
)
return span, nil
}
// GetTraceIDFromSpan get traceID from a Span
func GetTraceIDFromSpan(span opentracing.Span) string {
if span == nil {
return ""
}
strSlice := strings.Split(fmt.Sprintf("%+v", span), ":")
return strSlice[0]
}
// GetTraceIDFromJSON get traceID from a JSON string
//
//goland:noinspection GoUnusedExportedFunction
func GetTraceIDFromJSON(marshaled string) string {
tc, err := UnmarshalJSONToCarrier(marshaled)
if err != nil {
return ""
}
span, ok := tc["uber-trace-id"]
if !ok {
return ""
}
strSlice := strings.Split(fmt.Sprintf("%+v", span), ":")
return strSlice[0]
}
// ErrorToSpan ErrorTo Span
//
//goland:noinspection GoUnusedExportedFunction
func ErrorToSpan(span opentracing.Span, err error) {
if span == nil || err == nil {
return
}
ext.Error.Set(span, true)
span.LogKV("level", "error", "msg", err.Error())
}
var (
emptyFinishSpan = func() {}
)
// StartChildSpan Start ChildSpan
func StartChildSpan(
ctx context.Context,
logger logging.ILogger,
tracer opentracing.Tracer,
operateName string,
tags ...opentracing.StartSpanOption,
) (context.Context, func(), logging.ILogger) { // ctx, finish(), logger
if tracer == nil {
return ctx, emptyFinishSpan, logger
}
p := opentracing.SpanFromContext(ctx)
if p == nil {
return ctx, emptyFinishSpan, logger
}
// machinery will bring a noopSpan in the context if no other span set
pt := reflect.TypeOf(p)
if pt.String() == skipSpanType {
return ctx, emptyFinishSpan, logger
}
span, ctx := opentracing.StartSpanFromContextWithTracer(ctx, tracer, operateName, tags...)
return ctx, span.Finish, &LoggerWithSpan{
Span: span,
OriginalLogger: logger,
}
}
var (
//SpanKind "span.kind"
SpanKind = "span.kind"
//TraceKeyComponent "component"
TraceKeyComponent = "component"
//grpcTag
grpcTag = opentracing.Tag{Key: TraceKeyComponent, Value: "gRPC"}
//SpanKindClient client
_ = opentracing.Tag{Key: SpanKind, Value: "client"}
//SpanKindServer server
_ = opentracing.Tag{Key: SpanKind, Value: "server"}
//SpanKindPortal port
_ = opentracing.Tag{Key: SpanKind, Value: "portal"}
//SpanKindProducer producer
_ = opentracing.Tag{Key: SpanKind, Value: "producer"}
//SpanKindConsumer consumer
_ = opentracing.Tag{Key: SpanKind, Value: "consumer"}
//SpanKindWorker worker
_ = opentracing.Tag{Key: SpanKind, Value: "worker"}
//SpanKindComputation computation
_ = opentracing.Tag{Key: SpanKind, Value: "computation"}
//SpanKindDB database
_ = opentracing.Tag{Key: SpanKind, Value: "database"}
)
type clientSpanTagKey struct{}
// GetGRPCClientSpan get grpc client span
//
//goland:noinspection GoUnusedExportedFunction
func GetGRPCClientSpan(
ctx context.Context,
logger logging.ILogger,
tracer opentracing.Tracer,
operationName string,
mustFindParent bool,
) (context.Context, func(), logging.ILogger) { // ctx, finish(), logger
if tracer == nil {
return ctx, emptyFinishSpan, logger
}
parent := opentracing.SpanFromContext(ctx)
// machinery will bring a noopSpan in the context if no other span set
pt := reflect.TypeOf(parent)
if pt.String() == skipSpanType {
parent = nil
}
//fmt.Println(pt.String())
if mustFindParent && parent == nil {
return ctx, emptyFinishSpan, logger
}
opts := []opentracing.StartSpanOption{
ext.SpanKindRPCClient,
grpcTag,
}
if parent != nil {
opts = append(opts, opentracing.ChildOf(parent.Context()))
}
if tagX := ctx.Value(clientSpanTagKey{}); tagX != nil {
if opt, ok := tagX.(opentracing.StartSpanOption); ok {
opts = append(opts, opt)
}
}
span := tracer.StartSpan(operationName, opts...)
// Make sure we add this to the metadata of the call, so it gets propagated:
md := metautils.ExtractOutgoing(ctx).Clone()
if err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, metadataTextMap(md)); err != nil {
logger.Error(err)
return ctx, emptyFinishSpan, logger
}
ctxWithMetadata := md.ToOutgoing(ctx)
return opentracing.ContextWithSpan(ctxWithMetadata, span), span.Finish, &LoggerWithSpan{
Span: span,
OriginalLogger: logger,
}
}
const (
binHdrSuffix = "-bin"
)
// metadataTextMap extends a metadata.MD to be an opentracing text-map
type metadataTextMap metadata.MD
// Set is an 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 mul-map, and previous values may exist, but for opentracing headers, we do not append
// we just override.
m[encodedKey] = []string{encodedVal}
}
// ForeachKey is an 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.
// note: copied pasted from private values of grpc.metadata
func encodeKeyValue(k, v string) (string, string) {
k = strings.ToLower(k)
if strings.HasSuffix(k, binHdrSuffix) {
v = base64.StdEncoding.EncodeToString([]byte(v))
}
return k, v
}