...
1
2
3
4 package grpc_opentracing
5
6 import (
7 "context"
8
9 "github.com/opentracing/opentracing-go"
10 )
11
12 var (
13 defaultOptions = &options{
14 filterOutFunc: nil,
15 tracer: nil,
16 }
17 )
18
19
20
21
22 type FilterFunc func(ctx context.Context, fullMethodName string) bool
23
24
25 type UnaryRequestHandlerFunc func(span opentracing.Span, req interface{})
26
27
28 type OpNameFunc func(method string) string
29
30 type options struct {
31 filterOutFunc FilterFunc
32 tracer opentracing.Tracer
33 traceHeaderName string
34 unaryRequestHandlerFunc UnaryRequestHandlerFunc
35 opNameFunc OpNameFunc
36 }
37
38 func evaluateOptions(opts []Option) *options {
39 optCopy := &options{}
40 *optCopy = *defaultOptions
41 for _, o := range opts {
42 o(optCopy)
43 }
44 if optCopy.tracer == nil {
45 optCopy.tracer = opentracing.GlobalTracer()
46 }
47 if optCopy.traceHeaderName == "" {
48 optCopy.traceHeaderName = "uber-trace-id"
49 }
50 return optCopy
51 }
52
53 type Option func(*options)
54
55
56 func WithFilterFunc(f FilterFunc) Option {
57 return func(o *options) {
58 o.filterOutFunc = f
59 }
60 }
61
62
63
64 func WithTraceHeaderName(name string) Option {
65 return func(o *options) {
66 o.traceHeaderName = name
67 }
68 }
69
70
71 func WithTracer(tracer opentracing.Tracer) Option {
72 return func(o *options) {
73 o.tracer = tracer
74 }
75 }
76
77
78 func WithUnaryRequestHandlerFunc(f UnaryRequestHandlerFunc) Option {
79 return func(o *options) {
80 o.unaryRequestHandlerFunc = f
81 }
82 }
83
84
85 func WithOpName(f OpNameFunc) Option {
86 return func(o *options) {
87 o.opNameFunc = f
88 }
89 }
90
View as plain text