Girish Kumar | e48ec8a | 2020-08-18 12:28:51 +0000 | [diff] [blame] | 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
| 18 | opentracing "github.com/opentracing/opentracing-go" |
| 19 | "github.com/uber/jaeger-lib/metrics" |
| 20 | |
| 21 | "github.com/uber/jaeger-client-go" |
| 22 | ) |
| 23 | |
| 24 | // Option is a function that sets some option on the client. |
| 25 | type Option func(c *Options) |
| 26 | |
| 27 | // Options control behavior of the client. |
| 28 | type Options struct { |
| 29 | metrics metrics.Factory |
| 30 | logger jaeger.Logger |
| 31 | reporter jaeger.Reporter |
| 32 | sampler jaeger.Sampler |
| 33 | contribObservers []jaeger.ContribObserver |
| 34 | observers []jaeger.Observer |
| 35 | gen128Bit bool |
| 36 | poolSpans bool |
| 37 | zipkinSharedRPCSpan bool |
| 38 | maxTagValueLength int |
| 39 | noDebugFlagOnForcedSampling bool |
| 40 | tags []opentracing.Tag |
| 41 | injectors map[interface{}]jaeger.Injector |
| 42 | extractors map[interface{}]jaeger.Extractor |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 43 | randomNumber func() uint64 |
Girish Kumar | e48ec8a | 2020-08-18 12:28:51 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Metrics creates an Option that initializes Metrics in the tracer, |
| 47 | // which is used to emit statistics about spans. |
| 48 | func Metrics(factory metrics.Factory) Option { |
| 49 | return func(c *Options) { |
| 50 | c.metrics = factory |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Logger can be provided to log Reporter errors, as well as to log spans |
| 55 | // if Reporter.LogSpans is set to true. |
| 56 | func Logger(logger jaeger.Logger) Option { |
| 57 | return func(c *Options) { |
| 58 | c.logger = logger |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Reporter can be provided explicitly to override the configuration. |
| 63 | // Useful for testing, e.g. by passing InMemoryReporter. |
| 64 | func Reporter(reporter jaeger.Reporter) Option { |
| 65 | return func(c *Options) { |
| 66 | c.reporter = reporter |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Sampler can be provided explicitly to override the configuration. |
| 71 | func Sampler(sampler jaeger.Sampler) Option { |
| 72 | return func(c *Options) { |
| 73 | c.sampler = sampler |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Observer can be registered with the Tracer to receive notifications about new Spans. |
| 78 | func Observer(observer jaeger.Observer) Option { |
| 79 | return func(c *Options) { |
| 80 | c.observers = append(c.observers, observer) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // ContribObserver can be registered with the Tracer to receive notifications |
| 85 | // about new spans. |
| 86 | func ContribObserver(observer jaeger.ContribObserver) Option { |
| 87 | return func(c *Options) { |
| 88 | c.contribObservers = append(c.contribObservers, observer) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Gen128Bit specifies whether to generate 128bit trace IDs. |
| 93 | func Gen128Bit(gen128Bit bool) Option { |
| 94 | return func(c *Options) { |
| 95 | c.gen128Bit = gen128Bit |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // PoolSpans specifies whether to pool spans |
| 100 | func PoolSpans(poolSpans bool) Option { |
| 101 | return func(c *Options) { |
| 102 | c.poolSpans = poolSpans |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // ZipkinSharedRPCSpan creates an option that enables sharing span ID between client |
| 107 | // and server spans a la zipkin. If false, client and server spans will be assigned |
| 108 | // different IDs. |
| 109 | func ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) Option { |
| 110 | return func(c *Options) { |
| 111 | c.zipkinSharedRPCSpan = zipkinSharedRPCSpan |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // MaxTagValueLength can be provided to override the default max tag value length. |
| 116 | func MaxTagValueLength(maxTagValueLength int) Option { |
| 117 | return func(c *Options) { |
| 118 | c.maxTagValueLength = maxTagValueLength |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // NoDebugFlagOnForcedSampling can be used to decide whether debug flag will be set or not |
| 123 | // when calling span.setSamplingPriority to force sample a span. |
| 124 | func NoDebugFlagOnForcedSampling(noDebugFlagOnForcedSampling bool) Option { |
| 125 | return func(c *Options) { |
| 126 | c.noDebugFlagOnForcedSampling = noDebugFlagOnForcedSampling |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Tag creates an option that adds a tracer-level tag. |
| 131 | func Tag(key string, value interface{}) Option { |
| 132 | return func(c *Options) { |
| 133 | c.tags = append(c.tags, opentracing.Tag{Key: key, Value: value}) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Injector registers an Injector with the given format. |
| 138 | func Injector(format interface{}, injector jaeger.Injector) Option { |
| 139 | return func(c *Options) { |
| 140 | c.injectors[format] = injector |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Extractor registers an Extractor with the given format. |
| 145 | func Extractor(format interface{}, extractor jaeger.Extractor) Option { |
| 146 | return func(c *Options) { |
| 147 | c.extractors[format] = extractor |
| 148 | } |
| 149 | } |
| 150 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 151 | // WithRandomNumber supplies a random number generator function to the Tracer used to generate trace and span IDs. |
| 152 | func WithRandomNumber(f func() uint64) Option { |
| 153 | return func(c *Options) { |
| 154 | c.randomNumber = f |
| 155 | } |
| 156 | } |
| 157 | |
Girish Kumar | e48ec8a | 2020-08-18 12:28:51 +0000 | [diff] [blame] | 158 | func applyOptions(options ...Option) Options { |
| 159 | opts := Options{ |
| 160 | injectors: make(map[interface{}]jaeger.Injector), |
| 161 | extractors: make(map[interface{}]jaeger.Extractor), |
| 162 | } |
| 163 | for _, option := range options { |
| 164 | option(&opts) |
| 165 | } |
| 166 | if opts.metrics == nil { |
| 167 | opts.metrics = metrics.NullFactory |
| 168 | } |
| 169 | if opts.logger == nil { |
| 170 | opts.logger = jaeger.NullLogger |
| 171 | } |
| 172 | return opts |
| 173 | } |