blob: 16b460656421f5e83e4f89424d1b84fbacf29429 [file] [log] [blame]
khenaidooc6c7bda2020-06-17 17:20:18 -04001// 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
15package jaeger
16
17import (
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
28type TracerOption func(tracer *Tracer)
29
Abhay Kumara2ae5992025-11-10 14:02:24 +000030// TracerOptions is a factory for all available TracerOption's.
31var TracerOptions TracerOptionsFactory
khenaidooc6c7bda2020-06-17 17:20:18 -040032
Abhay Kumara2ae5992025-11-10 14:02:24 +000033// TracerOptionsFactory is a struct that defines functions for all available TracerOption's.
34type TracerOptionsFactory struct{}
khenaidooc6c7bda2020-06-17 17:20:18 -040035
36// Metrics creates a TracerOption that initializes Metrics on the tracer,
37// which is used to emit statistics.
Abhay Kumara2ae5992025-11-10 14:02:24 +000038func (TracerOptionsFactory) Metrics(m *Metrics) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -040039 return func(tracer *Tracer) {
40 tracer.metrics = *m
41 }
42}
43
44// Logger creates a TracerOption that gives the tracer a Logger.
Abhay Kumara2ae5992025-11-10 14:02:24 +000045func (TracerOptionsFactory) Logger(logger Logger) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -040046 return func(tracer *Tracer) {
47 tracer.logger = log.DebugLogAdapter(logger)
48 }
49}
50
Abhay Kumara2ae5992025-11-10 14:02:24 +000051// CustomHeaderKeys allows to override default HTTP header keys used to propagate
52// tracing context.
53func (TracerOptionsFactory) CustomHeaderKeys(headerKeys *HeadersConfig) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -040054 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 Kumara2ae5992025-11-10 14:02:24 +000068func (TracerOptionsFactory) TimeNow(timeNow func() time.Time) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -040069 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 Kumara2ae5992025-11-10 14:02:24 +000076func (TracerOptionsFactory) RandomNumber(randomNumber func() uint64) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -040077 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 Kumara2ae5992025-11-10 14:02:24 +000086func (TracerOptionsFactory) PoolSpans(poolSpans bool) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -040087 return func(tracer *Tracer) {
88 if poolSpans {
89 tracer.spanAllocator = newSyncPollSpanAllocator()
90 } else {
91 tracer.spanAllocator = simpleSpanAllocator{}
92 }
93 }
94}
95
Abhay Kumara2ae5992025-11-10 14:02:24 +000096// HostIPv4 creates a TracerOption that identifies the current service/process.
khenaidooc6c7bda2020-06-17 17:20:18 -040097// 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 Kumara2ae5992025-11-10 14:02:24 +000099//
100// Deprecated.
101func (TracerOptionsFactory) HostIPv4(hostIPv4 uint32) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400102 return func(tracer *Tracer) {
103 tracer.hostIPv4 = hostIPv4
104 }
105}
106
Abhay Kumara2ae5992025-11-10 14:02:24 +0000107// Injector registers a Injector for given format.
108func (TracerOptionsFactory) Injector(format interface{}, injector Injector) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400109 return func(tracer *Tracer) {
110 tracer.injectors[format] = injector
111 }
112}
113
Abhay Kumara2ae5992025-11-10 14:02:24 +0000114// Extractor registers an Extractor for given format.
115func (TracerOptionsFactory) Extractor(format interface{}, extractor Extractor) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400116 return func(tracer *Tracer) {
117 tracer.extractors[format] = extractor
118 }
119}
120
Abhay Kumara2ae5992025-11-10 14:02:24 +0000121// Observer registers an Observer.
122func (t TracerOptionsFactory) Observer(observer Observer) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400123 return t.ContribObserver(&oldObserver{obs: observer})
124}
125
Abhay Kumara2ae5992025-11-10 14:02:24 +0000126// ContribObserver registers a ContribObserver.
127func (TracerOptionsFactory) ContribObserver(observer ContribObserver) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400128 return func(tracer *Tracer) {
129 tracer.observer.append(observer)
130 }
131}
132
Abhay Kumara2ae5992025-11-10 14:02:24 +0000133// Gen128Bit enables generation of 128bit trace IDs.
134func (TracerOptionsFactory) Gen128Bit(gen128Bit bool) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400135 return func(tracer *Tracer) {
136 tracer.options.gen128Bit = gen128Bit
137 }
138}
139
Abhay Kumara2ae5992025-11-10 14:02:24 +0000140// NoDebugFlagOnForcedSampling turns off setting the debug flag in the trace context
141// when the trace is force-started via sampling=1 span tag.
142func (TracerOptionsFactory) NoDebugFlagOnForcedSampling(noDebugFlagOnForcedSampling bool) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400143 return func(tracer *Tracer) {
144 tracer.options.noDebugFlagOnForcedSampling = noDebugFlagOnForcedSampling
145 }
146}
147
Abhay Kumara2ae5992025-11-10 14:02:24 +0000148// HighTraceIDGenerator allows to override define ID generator.
149func (TracerOptionsFactory) HighTraceIDGenerator(highTraceIDGenerator func() uint64) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400150 return func(tracer *Tracer) {
151 tracer.options.highTraceIDGenerator = highTraceIDGenerator
152 }
153}
154
Abhay Kumara2ae5992025-11-10 14:02:24 +0000155// MaxTagValueLength sets the limit on the max length of tag values.
156func (TracerOptionsFactory) MaxTagValueLength(maxTagValueLength int) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400157 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 Kumara2ae5992025-11-10 14:02:24 +0000168func (TracerOptionsFactory) MaxLogsPerSpan(maxLogsPerSpan int) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400169 return func(tracer *Tracer) {
170 tracer.options.maxLogsPerSpan = maxLogsPerSpan
171 }
172}
173
Abhay Kumara2ae5992025-11-10 14:02:24 +0000174// 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.
177func (TracerOptionsFactory) ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400178 return func(tracer *Tracer) {
179 tracer.options.zipkinSharedRPCSpan = zipkinSharedRPCSpan
180 }
181}
182
Abhay Kumara2ae5992025-11-10 14:02:24 +0000183// Tag adds a tracer-level tag that will be added to all spans.
184func (TracerOptionsFactory) Tag(key string, value interface{}) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400185 return func(tracer *Tracer) {
186 tracer.tags = append(tracer.tags, Tag{key: key, value: value})
187 }
188}
189
Abhay Kumara2ae5992025-11-10 14:02:24 +0000190// BaggageRestrictionManager registers BaggageRestrictionManager.
191func (TracerOptionsFactory) BaggageRestrictionManager(mgr baggage.RestrictionManager) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400192 return func(tracer *Tracer) {
193 tracer.baggageRestrictionManager = mgr
194 }
195}
196
Abhay Kumara2ae5992025-11-10 14:02:24 +0000197// DebugThrottler registers a Throttler for debug spans.
198func (TracerOptionsFactory) DebugThrottler(throttler throttler.Throttler) TracerOption {
khenaidooc6c7bda2020-06-17 17:20:18 -0400199 return func(tracer *Tracer) {
200 tracer.debugThrottler = throttler
201 }
202}