| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [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 jaeger |
| 16 | |
| 17 | import ( |
| 18 | "time" |
| 19 | |
| 20 | "github.com/opentracing/opentracing-go" |
| 21 | |
| 22 | "github.com/uber/jaeger-client-go/internal/baggage" |
| 23 | "github.com/uber/jaeger-client-go/internal/throttler" |
| 24 | "github.com/uber/jaeger-client-go/log" |
| 25 | ) |
| 26 | |
| 27 | // TracerOption is a function that sets some option on the tracer |
| 28 | type TracerOption func(tracer *Tracer) |
| 29 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 30 | // TracerOptions is a factory for all available TracerOption's. |
| 31 | var TracerOptions TracerOptionsFactory |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 32 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 33 | // TracerOptionsFactory is a struct that defines functions for all available TracerOption's. |
| 34 | type TracerOptionsFactory struct{} |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 35 | |
| 36 | // Metrics creates a TracerOption that initializes Metrics on the tracer, |
| 37 | // which is used to emit statistics. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 38 | func (TracerOptionsFactory) Metrics(m *Metrics) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 39 | return func(tracer *Tracer) { |
| 40 | tracer.metrics = *m |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Logger creates a TracerOption that gives the tracer a Logger. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 45 | func (TracerOptionsFactory) Logger(logger Logger) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 46 | return func(tracer *Tracer) { |
| 47 | tracer.logger = log.DebugLogAdapter(logger) |
| 48 | } |
| 49 | } |
| 50 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 51 | // CustomHeaderKeys allows to override default HTTP header keys used to propagate |
| 52 | // tracing context. |
| 53 | func (TracerOptionsFactory) CustomHeaderKeys(headerKeys *HeadersConfig) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 54 | return func(tracer *Tracer) { |
| 55 | if headerKeys == nil { |
| 56 | return |
| 57 | } |
| 58 | textPropagator := NewTextMapPropagator(headerKeys.ApplyDefaults(), tracer.metrics) |
| 59 | tracer.addCodec(opentracing.TextMap, textPropagator, textPropagator) |
| 60 | |
| 61 | httpHeaderPropagator := NewHTTPHeaderPropagator(headerKeys.ApplyDefaults(), tracer.metrics) |
| 62 | tracer.addCodec(opentracing.HTTPHeaders, httpHeaderPropagator, httpHeaderPropagator) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // TimeNow creates a TracerOption that gives the tracer a function |
| 67 | // used to generate timestamps for spans. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 68 | func (TracerOptionsFactory) TimeNow(timeNow func() time.Time) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 69 | return func(tracer *Tracer) { |
| 70 | tracer.timeNow = timeNow |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // RandomNumber creates a TracerOption that gives the tracer |
| 75 | // a thread-safe random number generator function for generating trace IDs. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 76 | func (TracerOptionsFactory) RandomNumber(randomNumber func() uint64) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 77 | return func(tracer *Tracer) { |
| 78 | tracer.randomNumber = randomNumber |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // PoolSpans creates a TracerOption that tells the tracer whether it should use |
| 83 | // an object pool to minimize span allocations. |
| 84 | // This should be used with care, only if the service is not running any async tasks |
| 85 | // that can access parent spans after those spans have been finished. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 86 | func (TracerOptionsFactory) PoolSpans(poolSpans bool) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 87 | return func(tracer *Tracer) { |
| 88 | if poolSpans { |
| 89 | tracer.spanAllocator = newSyncPollSpanAllocator() |
| 90 | } else { |
| 91 | tracer.spanAllocator = simpleSpanAllocator{} |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 96 | // HostIPv4 creates a TracerOption that identifies the current service/process. |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 97 | // If not set, the factory method will obtain the current IP address. |
| 98 | // The TracerOption is deprecated; the tracer will attempt to automatically detect the IP. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 99 | // |
| 100 | // Deprecated. |
| 101 | func (TracerOptionsFactory) HostIPv4(hostIPv4 uint32) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 102 | return func(tracer *Tracer) { |
| 103 | tracer.hostIPv4 = hostIPv4 |
| 104 | } |
| 105 | } |
| 106 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 107 | // Injector registers a Injector for given format. |
| 108 | func (TracerOptionsFactory) Injector(format interface{}, injector Injector) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 109 | return func(tracer *Tracer) { |
| 110 | tracer.injectors[format] = injector |
| 111 | } |
| 112 | } |
| 113 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 114 | // Extractor registers an Extractor for given format. |
| 115 | func (TracerOptionsFactory) Extractor(format interface{}, extractor Extractor) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 116 | return func(tracer *Tracer) { |
| 117 | tracer.extractors[format] = extractor |
| 118 | } |
| 119 | } |
| 120 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 121 | // Observer registers an Observer. |
| 122 | func (t TracerOptionsFactory) Observer(observer Observer) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 123 | return t.ContribObserver(&oldObserver{obs: observer}) |
| 124 | } |
| 125 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 126 | // ContribObserver registers a ContribObserver. |
| 127 | func (TracerOptionsFactory) ContribObserver(observer ContribObserver) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 128 | return func(tracer *Tracer) { |
| 129 | tracer.observer.append(observer) |
| 130 | } |
| 131 | } |
| 132 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 133 | // Gen128Bit enables generation of 128bit trace IDs. |
| 134 | func (TracerOptionsFactory) Gen128Bit(gen128Bit bool) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 135 | return func(tracer *Tracer) { |
| 136 | tracer.options.gen128Bit = gen128Bit |
| 137 | } |
| 138 | } |
| 139 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 140 | // NoDebugFlagOnForcedSampling turns off setting the debug flag in the trace context |
| 141 | // when the trace is force-started via sampling=1 span tag. |
| 142 | func (TracerOptionsFactory) NoDebugFlagOnForcedSampling(noDebugFlagOnForcedSampling bool) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 143 | return func(tracer *Tracer) { |
| 144 | tracer.options.noDebugFlagOnForcedSampling = noDebugFlagOnForcedSampling |
| 145 | } |
| 146 | } |
| 147 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 148 | // HighTraceIDGenerator allows to override define ID generator. |
| 149 | func (TracerOptionsFactory) HighTraceIDGenerator(highTraceIDGenerator func() uint64) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 150 | return func(tracer *Tracer) { |
| 151 | tracer.options.highTraceIDGenerator = highTraceIDGenerator |
| 152 | } |
| 153 | } |
| 154 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 155 | // MaxTagValueLength sets the limit on the max length of tag values. |
| 156 | func (TracerOptionsFactory) MaxTagValueLength(maxTagValueLength int) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 157 | return func(tracer *Tracer) { |
| 158 | tracer.options.maxTagValueLength = maxTagValueLength |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // MaxLogsPerSpan limits the number of Logs in a span (if set to a nonzero |
| 163 | // value). If a span has more logs than this value, logs are dropped as |
| 164 | // necessary (and replaced with a log describing how many were dropped). |
| 165 | // |
| 166 | // About half of the MaxLogsPerSpan logs kept are the oldest logs, and about |
| 167 | // half are the newest logs. |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 168 | func (TracerOptionsFactory) MaxLogsPerSpan(maxLogsPerSpan int) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 169 | return func(tracer *Tracer) { |
| 170 | tracer.options.maxLogsPerSpan = maxLogsPerSpan |
| 171 | } |
| 172 | } |
| 173 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 174 | // ZipkinSharedRPCSpan enables a mode where server-side span shares the span ID |
| 175 | // from the client span from the incoming request, for compatibility with Zipkin's |
| 176 | // "one span per RPC" model. |
| 177 | func (TracerOptionsFactory) ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 178 | return func(tracer *Tracer) { |
| 179 | tracer.options.zipkinSharedRPCSpan = zipkinSharedRPCSpan |
| 180 | } |
| 181 | } |
| 182 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 183 | // Tag adds a tracer-level tag that will be added to all spans. |
| 184 | func (TracerOptionsFactory) Tag(key string, value interface{}) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 185 | return func(tracer *Tracer) { |
| 186 | tracer.tags = append(tracer.tags, Tag{key: key, value: value}) |
| 187 | } |
| 188 | } |
| 189 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 190 | // BaggageRestrictionManager registers BaggageRestrictionManager. |
| 191 | func (TracerOptionsFactory) BaggageRestrictionManager(mgr baggage.RestrictionManager) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 192 | return func(tracer *Tracer) { |
| 193 | tracer.baggageRestrictionManager = mgr |
| 194 | } |
| 195 | } |
| 196 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 197 | // DebugThrottler registers a Throttler for debug spans. |
| 198 | func (TracerOptionsFactory) DebugThrottler(throttler throttler.Throttler) TracerOption { |
| khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 199 | return func(tracer *Tracer) { |
| 200 | tracer.debugThrottler = throttler |
| 201 | } |
| 202 | } |