Girish Gowdra | 5d7d644 | 2020-09-08 17:03:11 -0700 | [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 | |
| 30 | // TracerOptions is a factory for all available TracerOption's |
| 31 | var TracerOptions tracerOptions |
| 32 | |
| 33 | type tracerOptions struct{} |
| 34 | |
| 35 | // Metrics creates a TracerOption that initializes Metrics on the tracer, |
| 36 | // which is used to emit statistics. |
| 37 | func (tracerOptions) Metrics(m *Metrics) TracerOption { |
| 38 | return func(tracer *Tracer) { |
| 39 | tracer.metrics = *m |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Logger creates a TracerOption that gives the tracer a Logger. |
| 44 | func (tracerOptions) Logger(logger Logger) TracerOption { |
| 45 | return func(tracer *Tracer) { |
| 46 | tracer.logger = log.DebugLogAdapter(logger) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func (tracerOptions) CustomHeaderKeys(headerKeys *HeadersConfig) TracerOption { |
| 51 | return func(tracer *Tracer) { |
| 52 | if headerKeys == nil { |
| 53 | return |
| 54 | } |
| 55 | textPropagator := NewTextMapPropagator(headerKeys.ApplyDefaults(), tracer.metrics) |
| 56 | tracer.addCodec(opentracing.TextMap, textPropagator, textPropagator) |
| 57 | |
| 58 | httpHeaderPropagator := NewHTTPHeaderPropagator(headerKeys.ApplyDefaults(), tracer.metrics) |
| 59 | tracer.addCodec(opentracing.HTTPHeaders, httpHeaderPropagator, httpHeaderPropagator) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // TimeNow creates a TracerOption that gives the tracer a function |
| 64 | // used to generate timestamps for spans. |
| 65 | func (tracerOptions) TimeNow(timeNow func() time.Time) TracerOption { |
| 66 | return func(tracer *Tracer) { |
| 67 | tracer.timeNow = timeNow |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // RandomNumber creates a TracerOption that gives the tracer |
| 72 | // a thread-safe random number generator function for generating trace IDs. |
| 73 | func (tracerOptions) RandomNumber(randomNumber func() uint64) TracerOption { |
| 74 | return func(tracer *Tracer) { |
| 75 | tracer.randomNumber = randomNumber |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // PoolSpans creates a TracerOption that tells the tracer whether it should use |
| 80 | // an object pool to minimize span allocations. |
| 81 | // This should be used with care, only if the service is not running any async tasks |
| 82 | // that can access parent spans after those spans have been finished. |
| 83 | func (tracerOptions) PoolSpans(poolSpans bool) TracerOption { |
| 84 | return func(tracer *Tracer) { |
| 85 | if poolSpans { |
| 86 | tracer.spanAllocator = newSyncPollSpanAllocator() |
| 87 | } else { |
| 88 | tracer.spanAllocator = simpleSpanAllocator{} |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Deprecated: HostIPv4 creates a TracerOption that identifies the current service/process. |
| 94 | // If not set, the factory method will obtain the current IP address. |
| 95 | // The TracerOption is deprecated; the tracer will attempt to automatically detect the IP. |
| 96 | func (tracerOptions) HostIPv4(hostIPv4 uint32) TracerOption { |
| 97 | return func(tracer *Tracer) { |
| 98 | tracer.hostIPv4 = hostIPv4 |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func (tracerOptions) Injector(format interface{}, injector Injector) TracerOption { |
| 103 | return func(tracer *Tracer) { |
| 104 | tracer.injectors[format] = injector |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func (tracerOptions) Extractor(format interface{}, extractor Extractor) TracerOption { |
| 109 | return func(tracer *Tracer) { |
| 110 | tracer.extractors[format] = extractor |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func (t tracerOptions) Observer(observer Observer) TracerOption { |
| 115 | return t.ContribObserver(&oldObserver{obs: observer}) |
| 116 | } |
| 117 | |
| 118 | func (tracerOptions) ContribObserver(observer ContribObserver) TracerOption { |
| 119 | return func(tracer *Tracer) { |
| 120 | tracer.observer.append(observer) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func (tracerOptions) Gen128Bit(gen128Bit bool) TracerOption { |
| 125 | return func(tracer *Tracer) { |
| 126 | tracer.options.gen128Bit = gen128Bit |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func (tracerOptions) NoDebugFlagOnForcedSampling(noDebugFlagOnForcedSampling bool) TracerOption { |
| 131 | return func(tracer *Tracer) { |
| 132 | tracer.options.noDebugFlagOnForcedSampling = noDebugFlagOnForcedSampling |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func (tracerOptions) HighTraceIDGenerator(highTraceIDGenerator func() uint64) TracerOption { |
| 137 | return func(tracer *Tracer) { |
| 138 | tracer.options.highTraceIDGenerator = highTraceIDGenerator |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func (tracerOptions) MaxTagValueLength(maxTagValueLength int) TracerOption { |
| 143 | return func(tracer *Tracer) { |
| 144 | tracer.options.maxTagValueLength = maxTagValueLength |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // MaxLogsPerSpan limits the number of Logs in a span (if set to a nonzero |
| 149 | // value). If a span has more logs than this value, logs are dropped as |
| 150 | // necessary (and replaced with a log describing how many were dropped). |
| 151 | // |
| 152 | // About half of the MaxLogsPerSpan logs kept are the oldest logs, and about |
| 153 | // half are the newest logs. |
| 154 | func (tracerOptions) MaxLogsPerSpan(maxLogsPerSpan int) TracerOption { |
| 155 | return func(tracer *Tracer) { |
| 156 | tracer.options.maxLogsPerSpan = maxLogsPerSpan |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func (tracerOptions) ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) TracerOption { |
| 161 | return func(tracer *Tracer) { |
| 162 | tracer.options.zipkinSharedRPCSpan = zipkinSharedRPCSpan |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func (tracerOptions) Tag(key string, value interface{}) TracerOption { |
| 167 | return func(tracer *Tracer) { |
| 168 | tracer.tags = append(tracer.tags, Tag{key: key, value: value}) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func (tracerOptions) BaggageRestrictionManager(mgr baggage.RestrictionManager) TracerOption { |
| 173 | return func(tracer *Tracer) { |
| 174 | tracer.baggageRestrictionManager = mgr |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func (tracerOptions) DebugThrottler(throttler throttler.Throttler) TracerOption { |
| 179 | return func(tracer *Tracer) { |
| 180 | tracer.debugThrottler = throttler |
| 181 | } |
| 182 | } |