forked from st3v/go-plugins
/
opencensus.go
165 lines (135 loc) · 4.06 KB
/
opencensus.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
// Package opencensus provides wrappers for OpenCensus tracing.
package opencensus
import (
"context"
"encoding/base64"
"fmt"
"github.com/micro/go-log"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
)
const (
// TracePropagationField is the key for the tracing context
// that will be injected in go-micro's metadata.
TracePropagationField = "X-Trace-Context"
)
// clientWrapper wraps an RPC client and adds tracing.
type clientWrapper struct {
client.Client
}
func injectTraceIntoCtx(ctx context.Context, span *trace.Span) context.Context {
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(map[string]string)
}
spanCtx := propagation.Binary(span.SpanContext())
md[TracePropagationField] = base64.RawStdEncoding.EncodeToString(spanCtx)
return metadata.NewContext(ctx, md)
}
// Call implements client.Client.Call.
func (w *clientWrapper) Call(
ctx context.Context,
req client.Request,
rsp interface{},
opts ...client.CallOption) (err error) {
t := newRequestTracker(req, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Call(ctx, req, rsp, opts...)
return
}
// Publish implements client.Client.Publish.
func (w *clientWrapper) Publish(ctx context.Context, p client.Publication, opts ...client.PublishOption) (err error) {
t := newPublicationTracker(p, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Publish(ctx, p, opts...)
return
}
// NewClientWrapper returns a client.Wrapper
// that adds monitoring to outgoing requests.
func NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
return &clientWrapper{c}
}
}
func getTraceFromCtx(ctx context.Context) *trace.SpanContext {
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(map[string]string)
}
encodedTraceCtx, ok := md[TracePropagationField]
if !ok {
return nil
}
traceCtxBytes, err := base64.RawStdEncoding.DecodeString(encodedTraceCtx)
if err != nil {
log.Logf("Could not decode trace context: %s", err.Error())
return nil
}
spanCtx, ok := propagation.FromBinary(traceCtxBytes)
if !ok {
log.Log("Could not decode trace context from binary")
return nil
}
return &spanCtx
}
// NewHandlerWrapper returns a server.HandlerWrapper
// that adds tracing to incoming requests.
func NewHandlerWrapper() server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
t := newRequestTracker(req, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
t.span = trace.NewSpanWithRemoteParent(
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Method()),
*spanCtx,
trace.StartOptions{},
)
ctx = trace.WithSpan(ctx, t.span)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Method()),
)
}
err = fn(ctx, req, rsp)
return
}
}
}
// NewSubscriberWrapper returns a server.SubscriberWrapper
// that adds tracing to subscription requests.
func NewSubscriberWrapper() server.SubscriberWrapper {
return func(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, p server.Publication) (err error) {
t := newPublicationTracker(p, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
t.span = trace.NewSpanWithRemoteParent(
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
*spanCtx,
trace.StartOptions{},
)
ctx = trace.WithSpan(ctx, t.span)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
)
}
err = fn(ctx, p)
return
}
}
}