/
opentracing.go
175 lines (157 loc) · 5.35 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
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
// Package opentracing provides wrappers for OpenTracing
package opentracing
import (
"fmt"
"context"
"strings"
"go-micro.dev/v4/client"
"go-micro.dev/v4/metadata"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/server"
opentracing "github.com/opentracing/opentracing-go"
opentracinglog "github.com/opentracing/opentracing-go/log"
)
type otWrapper struct {
ot opentracing.Tracer
client.Client
}
// StartSpanFromContext returns a new span with the given operation name and options. If a span
// is found in the context, it will be used as the parent of the resulting span.
func StartSpanFromContext(ctx context.Context, tracer opentracing.Tracer, name string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.Span, error) {
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(metadata.Metadata)
}
// Find parent span.
// First try to get span within current service boundary.
// If there doesn't exist, try to get it from go-micro metadata(which is cross boundary)
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
opts = append(opts, opentracing.ChildOf(parentSpan.Context()))
} else if spanCtx, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md)); err == nil {
opts = append(opts, opentracing.ChildOf(spanCtx))
}
// allocate new map with only one element
nmd := make(metadata.Metadata, 1)
sp := tracer.StartSpan(name, opts...)
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
return nil, nil, err
}
for k, v := range nmd {
md.Set(strings.Title(k), v)
}
ctx = opentracing.ContextWithSpan(ctx, sp)
ctx = metadata.NewContext(ctx, md)
return ctx, sp, nil
}
func (o *otWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
name := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
ctx, span, err := StartSpanFromContext(ctx, o.ot, name)
if err != nil {
return err
}
defer span.Finish()
if err = o.Client.Call(ctx, req, rsp, opts...); err != nil {
span.LogFields(opentracinglog.String("error", err.Error()))
span.SetTag("error", true)
}
return err
}
func (o *otWrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
name := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
ctx, span, err := StartSpanFromContext(ctx, o.ot, name)
if err != nil {
return nil, err
}
defer span.Finish()
stream, err := o.Client.Stream(ctx, req, opts...)
if err != nil {
span.LogFields(opentracinglog.String("error", err.Error()))
span.SetTag("error", true)
}
return stream, err
}
func (o *otWrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
name := fmt.Sprintf("Pub to %s", p.Topic())
ctx, span, err := StartSpanFromContext(ctx, o.ot, name)
if err != nil {
return err
}
defer span.Finish()
if err = o.Client.Publish(ctx, p, opts...); err != nil {
span.LogFields(opentracinglog.String("error", err.Error()))
span.SetTag("error", true)
}
return err
}
// NewClientWrapper accepts an open tracing Trace and returns a Client Wrapper
func NewClientWrapper(ot opentracing.Tracer) client.Wrapper {
return func(c client.Client) client.Client {
if ot == nil {
ot = opentracing.GlobalTracer()
}
return &otWrapper{ot, c}
}
}
// NewCallWrapper accepts an opentracing Tracer and returns a Call Wrapper
func NewCallWrapper(ot opentracing.Tracer) client.CallWrapper {
return func(cf client.CallFunc) client.CallFunc {
return func(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
if ot == nil {
ot = opentracing.GlobalTracer()
}
name := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
ctx, span, err := StartSpanFromContext(ctx, ot, name)
if err != nil {
return err
}
defer span.Finish()
if err = cf(ctx, node, req, rsp, opts); err != nil {
span.LogFields(opentracinglog.String("error", err.Error()))
span.SetTag("error", true)
}
return err
}
}
}
// NewHandlerWrapper accepts an opentracing Tracer and returns a Handler Wrapper
func NewHandlerWrapper(ot opentracing.Tracer) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
if ot == nil {
ot = opentracing.GlobalTracer()
}
name := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
ctx, span, err := StartSpanFromContext(ctx, ot, name)
if err != nil {
return err
}
defer span.Finish()
if err = h(ctx, req, rsp); err != nil {
span.LogFields(opentracinglog.String("error", err.Error()))
span.SetTag("error", true)
}
return err
}
}
}
// NewSubscriberWrapper accepts an opentracing Tracer and returns a Subscriber Wrapper
func NewSubscriberWrapper(ot opentracing.Tracer) server.SubscriberWrapper {
return func(next server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, msg server.Message) error {
name := "Sub from " + msg.Topic()
if ot == nil {
ot = opentracing.GlobalTracer()
}
ctx, span, err := StartSpanFromContext(ctx, ot, name)
if err != nil {
return err
}
defer span.Finish()
if err = next(ctx, msg); err != nil {
span.LogFields(opentracinglog.String("error", err.Error()))
span.SetTag("error", true)
}
return err
}
}
}