Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 1 | // Copyright 2017 Michal Witkowski. All Rights Reserved. |
| 2 | // See LICENSE for licensing terms. |
| 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 | // FilterFunc allows users to provide a function that filters out certain methods from being traced. |
| 20 | // |
| 21 | // If it returns false, the given request will not be traced. |
| 22 | type FilterFunc func(ctx context.Context, fullMethodName string) bool |
| 23 | |
| 24 | type options struct { |
| 25 | filterOutFunc FilterFunc |
| 26 | tracer opentracing.Tracer |
| 27 | } |
| 28 | |
| 29 | func evaluateOptions(opts []Option) *options { |
| 30 | optCopy := &options{} |
| 31 | *optCopy = *defaultOptions |
| 32 | for _, o := range opts { |
| 33 | o(optCopy) |
| 34 | } |
| 35 | if optCopy.tracer == nil { |
| 36 | optCopy.tracer = opentracing.GlobalTracer() |
| 37 | } |
| 38 | return optCopy |
| 39 | } |
| 40 | |
| 41 | type Option func(*options) |
| 42 | |
| 43 | // WithFilterFunc customizes the function used for deciding whether a given call is traced or not. |
| 44 | func WithFilterFunc(f FilterFunc) Option { |
| 45 | return func(o *options) { |
| 46 | o.filterOutFunc = f |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // WithTracer sets a custom tracer to be used for this middleware, otherwise the opentracing.GlobalTracer is used. |
| 51 | func WithTracer(tracer opentracing.Tracer) Option { |
| 52 | return func(o *options) { |
| 53 | o.tracer = tracer |
| 54 | } |
| 55 | } |