blob: 430fe56a130b7f7b65ae3c9913ac81305245adab [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
khenaidood948f772021-08-11 17:49:24 -040024// UnaryRequestHandlerFunc is a custom request handler
25type UnaryRequestHandlerFunc func(span opentracing.Span, req interface{})
26
27// OpNameFunc is a func that allows custom operation names instead of the gRPC method.
28type OpNameFunc func(method string) string
29
Girish Kumardef46fc2020-08-05 18:20:11 +000030type options struct {
khenaidood948f772021-08-11 17:49:24 -040031 filterOutFunc FilterFunc
32 tracer opentracing.Tracer
33 traceHeaderName string
34 unaryRequestHandlerFunc UnaryRequestHandlerFunc
35 opNameFunc OpNameFunc
Girish Kumardef46fc2020-08-05 18:20:11 +000036}
37
38func 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 }
khenaidood948f772021-08-11 17:49:24 -040047 if optCopy.traceHeaderName == "" {
48 optCopy.traceHeaderName = "uber-trace-id"
49 }
Girish Kumardef46fc2020-08-05 18:20:11 +000050 return optCopy
51}
52
53type Option func(*options)
54
55// WithFilterFunc customizes the function used for deciding whether a given call is traced or not.
56func WithFilterFunc(f FilterFunc) Option {
57 return func(o *options) {
58 o.filterOutFunc = f
59 }
60}
61
khenaidood948f772021-08-11 17:49:24 -040062// WithTraceHeaderName customizes the trace header name where trace metadata passed with requests.
63// Default one is `uber-trace-id`
64func WithTraceHeaderName(name string) Option {
65 return func(o *options) {
66 o.traceHeaderName = name
67 }
68}
69
Girish Kumardef46fc2020-08-05 18:20:11 +000070// WithTracer sets a custom tracer to be used for this middleware, otherwise the opentracing.GlobalTracer is used.
71func WithTracer(tracer opentracing.Tracer) Option {
72 return func(o *options) {
73 o.tracer = tracer
74 }
75}
khenaidood948f772021-08-11 17:49:24 -040076
77// WithUnaryRequestHandlerFunc sets a custom handler for the request
78func WithUnaryRequestHandlerFunc(f UnaryRequestHandlerFunc) Option {
79 return func(o *options) {
80 o.unaryRequestHandlerFunc = f
81 }
82}
83
84// WithOpName customizes the trace Operation name
85func WithOpName(f OpNameFunc) Option {
86 return func(o *options) {
87 o.opNameFunc = f
88 }
89}