blob: e75102b2504511d990ad1204396e4efdccbe0be0 [file] [log] [blame]
Girish Kumardef46fc2020-08-05 18:20:11 +00001// Copyright 2017 Michal Witkowski. All Rights Reserved.
2// See LICENSE for licensing terms.
3
4package grpc_opentracing
5
6import (
7 "context"
8
9 "github.com/opentracing/opentracing-go"
10)
11
12var (
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.
22type FilterFunc func(ctx context.Context, fullMethodName string) bool
23
24type options struct {
25 filterOutFunc FilterFunc
26 tracer opentracing.Tracer
27}
28
29func 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
41type Option func(*options)
42
43// WithFilterFunc customizes the function used for deciding whether a given call is traced or not.
44func 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.
51func WithTracer(tracer opentracing.Tracer) Option {
52 return func(o *options) {
53 o.tracer = tracer
54 }
55}