-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtracing.go
More file actions
84 lines (74 loc) · 2.83 KB
/
Copy pathtracing.go
File metadata and controls
84 lines (74 loc) · 2.83 KB
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
// package tracing contains opentracing helper functions
package tracing
import (
"bytes"
"context"
"fmt"
"net/rpc"
"github.com/cloudflare/gokeyless/protocol"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
opentracing_log "github.com/opentracing/opentracing-go/log"
"github.com/uber/jaeger-client-go"
)
// SpanContextFromBinary builds span context based on binary-encoded bytes (e.g. for servers)
func SpanContextFromBinary(spanData []byte) (opentracing.SpanContext, error) {
if len(spanData) == 0 {
// If there is no span data to decode then `Extract` will return an error, but
// we really only want this function to return error if the span data is malformed, not empty.
return nil, nil
}
var scReader = bytes.NewReader(spanData)
sc, err := opentracing.GlobalTracer().Extract(opentracing.Binary, scReader)
if err != nil {
carrier := opentracing.TextMapCarrier{
jaeger.TraceContextHeaderName: string(spanData),
}
sc, err = opentracing.GlobalTracer().Extract(opentracing.TextMap, &carrier)
if err != nil {
return nil, err
}
}
return sc, nil
}
// SpanContextToBinary returns bytes representing the binary-encoded span context (e.g. for clients)
func SpanContextToBinary(sc opentracing.SpanContext) ([]byte, error) {
var b bytes.Buffer
err := opentracing.GlobalTracer().Inject(sc, opentracing.Binary, &b)
if err != nil {
return nil, err
}
return b.Bytes(), nil
}
// SetOperationSpanTags sets span tags with all of the operation's fields
func SetOperationSpanTags(span opentracing.Span, op *protocol.Operation) {
tags := map[string]interface{}{
"operation.opcode": op.Opcode.String(),
"operation.ski": op.SKI,
"operation.digest": fmt.Sprintf("%02x", op.Digest),
"operation.clientip": op.ClientIP,
"operation.serverip": op.ServerIP,
"operation.sni": op.SNI,
"operation.certid": op.CertID,
"operation.customfuncname": op.CustomFuncName,
"operation.forwardingsvc": fmt.Sprintf("%d", op.ForwardingSvc),
"operation.compliance_region": op.ComplianceRegion.String(),
}
for k, v := range tags {
span.SetTag(k, v)
}
}
// CallRPC wraps rpc.Call with a trace
func CallRPC(ctx context.Context, rpc *rpc.Client, serviceMethod string, args interface{}, reply interface{}) error {
span, ctx := opentracing.StartSpanFromContext(ctx, fmt.Sprintf("rpc: %s", serviceMethod))
defer span.Finish()
ext.SpanKind.Set(span, ext.SpanKindRPCClientEnum)
return rpc.Call(serviceMethod, args, reply)
}
// LogError marks that an error has occurred within the scope of a span.
func LogError(span opentracing.Span, err error) {
//set error tag to true, allows searching by `error=true`
ext.Error.Set(span, true)
//emit a log message, with the value containing the error message
span.LogFields(opentracing_log.Error(err))
}