-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
endpoint.go
124 lines (100 loc) · 3.37 KB
/
endpoint.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
package opentracing
import (
"context"
"strconv"
"github.com/opentracing/opentracing-go"
otext "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/sd/lb"
)
// TraceEndpoint returns a Middleware that wraps the `next` Endpoint in an
// OpenTracing Span called `operationName`.
//
// If `ctx` already has a Span, child span is created from it.
// If `ctx` doesn't yet have a Span, the new one is created.
func TraceEndpoint(tracer opentracing.Tracer, operationName string, opts ...EndpointOption) endpoint.Middleware {
cfg := &EndpointOptions{
Tags: make(opentracing.Tags),
}
for _, opt := range opts {
opt(cfg)
}
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
if cfg.GetOperationName != nil {
if newOperationName := cfg.GetOperationName(ctx, operationName); newOperationName != "" {
operationName = newOperationName
}
}
var span opentracing.Span
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
span = tracer.StartSpan(
operationName,
opentracing.ChildOf(parentSpan.Context()),
)
} else {
span = tracer.StartSpan(operationName)
}
defer span.Finish()
applyTags(span, cfg.Tags)
if cfg.GetTags != nil {
extraTags := cfg.GetTags(ctx)
applyTags(span, extraTags)
}
ctx = opentracing.ContextWithSpan(ctx, span)
defer func() {
if err != nil {
if lbErr, ok := err.(lb.RetryError); ok {
// handle errors originating from lb.Retry
fields := make([]otlog.Field, 0, len(lbErr.RawErrors))
for idx, rawErr := range lbErr.RawErrors {
fields = append(fields, otlog.String(
"gokit.retry.error."+strconv.Itoa(idx+1), rawErr.Error(),
))
}
otext.LogError(span, lbErr, fields...)
return
}
// generic error
otext.LogError(span, err)
return
}
// test for business error
if res, ok := response.(endpoint.Failer); ok && res.Failed() != nil {
span.LogFields(
otlog.String("gokit.business.error", res.Failed().Error()),
)
if cfg.IgnoreBusinessError {
return
}
// treating business error as real error in span.
otext.LogError(span, res.Failed())
return
}
}()
return next(ctx, request)
}
}
}
// TraceServer returns a Middleware that wraps the `next` Endpoint in an
// OpenTracing Span called `operationName` with server span.kind tag..
func TraceServer(tracer opentracing.Tracer, operationName string, opts ...EndpointOption) endpoint.Middleware {
opts = append(opts, WithTags(map[string]interface{}{
otext.SpanKindRPCServer.Key: otext.SpanKindRPCServer.Value,
}))
return TraceEndpoint(tracer, operationName, opts...)
}
// TraceClient returns a Middleware that wraps the `next` Endpoint in an
// OpenTracing Span called `operationName` with client span.kind tag.
func TraceClient(tracer opentracing.Tracer, operationName string, opts ...EndpointOption) endpoint.Middleware {
opts = append(opts, WithTags(map[string]interface{}{
otext.SpanKindRPCClient.Key: otext.SpanKindRPCClient.Value,
}))
return TraceEndpoint(tracer, operationName, opts...)
}
func applyTags(span opentracing.Span, tags opentracing.Tags) {
for key, value := range tags {
span.SetTag(key, value)
}
}