-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracing.go
102 lines (84 loc) · 2.73 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
package gotracing
import (
"context"
"fmt"
"net/http"
"github.com/cresta/zapctx"
"github.com/gorilla/mux"
"google.golang.org/grpc"
)
type SpanConfig struct {
OperationName string
}
type Tracing interface {
WrapRoundTrip(rt http.RoundTripper) http.RoundTripper
AttachTag(ctx context.Context, key string, value interface{})
DynamicFields() []zapctx.DynamicFields
CreateRootMux() (*mux.Router, http.Handler)
GrpcServerInterceptors(serviceName string) ([]grpc.UnaryServerInterceptor, []grpc.StreamServerInterceptor)
GrpcClientInterceptors(serviceName string) ([]grpc.UnaryClientInterceptor, []grpc.StreamClientInterceptor)
StartSpanFromContext(ctx context.Context, cfg SpanConfig, callback func(ctx context.Context) error) error
}
type Constructor func(config Config) (Tracing, error)
type Registry struct {
Constructors map[string]Constructor
}
func (r *Registry) New(name string, config Config) (Tracing, error) {
if name == "" || r == nil {
config.Log.Info(context.Background(), "returning no-op tracer")
return Noop{}, nil
}
cons, exists := r.Constructors[name]
if !exists {
return nil, fmt.Errorf("unable to find tracer named: %s", name)
}
ret, err := cons(config)
if err != nil {
return nil, fmt.Errorf("unable to create registry %s: %w", name, err)
}
return ret, nil
}
type Config struct {
ServiceName string
Log *zapctx.Logger
Env []string
}
var _ Tracing = Noop{}
type Noop struct{}
func (n Noop) GrpcServerInterceptors(_ string) ([]grpc.UnaryServerInterceptor, []grpc.StreamServerInterceptor) {
return nil, nil
}
func (n Noop) GrpcClientInterceptors(_ string) ([]grpc.UnaryClientInterceptor, []grpc.StreamClientInterceptor) {
return nil, nil
}
func (n Noop) StartSpanFromContext(ctx context.Context, _ SpanConfig, callback func(ctx context.Context) error) error {
return callback(ctx)
}
func (n Noop) WrapRoundTrip(rt http.RoundTripper) http.RoundTripper {
return rt
}
func (n Noop) AttachTag(_ context.Context, _ string, _ interface{}) {
}
func (n Noop) DynamicFields() []zapctx.DynamicFields {
return nil
}
func (n Noop) CreateRootMux() (*mux.Router, http.Handler) {
ret := mux.NewRouter()
return ret, ret
}
// MuxTagging adds key/value tags to the context for a handler based upon the mux Vars
func MuxTagging(t Tracing) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
for k, v := range mux.Vars(request) {
t.AttachTag(request.Context(), fmt.Sprintf("mux.vars.%s", k), v)
}
if r := mux.CurrentRoute(request); r != nil {
if r.GetName() != "" {
t.AttachTag(request.Context(), "mux.name", r.GetName())
}
}
handler.ServeHTTP(writer, request)
})
}
}