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
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
30// TracerOptions is a factory for all available TracerOption's
31var TracerOptions tracerOptions
32
33type tracerOptions struct{}
34
35// Metrics creates a TracerOption that initializes Metrics on the tracer,
36// which is used to emit statistics.
37func (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.
44func (tracerOptions) Logger(logger Logger) TracerOption {
45	return func(tracer *Tracer) {
46		tracer.logger = log.DebugLogAdapter(logger)
47	}
48}
49
50func (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.
65func (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.
73func (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.
83func (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.
96func (tracerOptions) HostIPv4(hostIPv4 uint32) TracerOption {
97	return func(tracer *Tracer) {
98		tracer.hostIPv4 = hostIPv4
99	}
100}
101
102func (tracerOptions) Injector(format interface{}, injector Injector) TracerOption {
103	return func(tracer *Tracer) {
104		tracer.injectors[format] = injector
105	}
106}
107
108func (tracerOptions) Extractor(format interface{}, extractor Extractor) TracerOption {
109	return func(tracer *Tracer) {
110		tracer.extractors[format] = extractor
111	}
112}
113
114func (t tracerOptions) Observer(observer Observer) TracerOption {
115	return t.ContribObserver(&oldObserver{obs: observer})
116}
117
118func (tracerOptions) ContribObserver(observer ContribObserver) TracerOption {
119	return func(tracer *Tracer) {
120		tracer.observer.append(observer)
121	}
122}
123
124func (tracerOptions) Gen128Bit(gen128Bit bool) TracerOption {
125	return func(tracer *Tracer) {
126		tracer.options.gen128Bit = gen128Bit
127	}
128}
129
130func (tracerOptions) NoDebugFlagOnForcedSampling(noDebugFlagOnForcedSampling bool) TracerOption {
131	return func(tracer *Tracer) {
132		tracer.options.noDebugFlagOnForcedSampling = noDebugFlagOnForcedSampling
133	}
134}
135
136func (tracerOptions) HighTraceIDGenerator(highTraceIDGenerator func() uint64) TracerOption {
137	return func(tracer *Tracer) {
138		tracer.options.highTraceIDGenerator = highTraceIDGenerator
139	}
140}
141
142func (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.
154func (tracerOptions) MaxLogsPerSpan(maxLogsPerSpan int) TracerOption {
155	return func(tracer *Tracer) {
156		tracer.options.maxLogsPerSpan = maxLogsPerSpan
157	}
158}
159
160func (tracerOptions) ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) TracerOption {
161	return func(tracer *Tracer) {
162		tracer.options.zipkinSharedRPCSpan = zipkinSharedRPCSpan
163	}
164}
165
166func (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
172func (tracerOptions) BaggageRestrictionManager(mgr baggage.RestrictionManager) TracerOption {
173	return func(tracer *Tracer) {
174		tracer.baggageRestrictionManager = mgr
175	}
176}
177
178func (tracerOptions) DebugThrottler(throttler throttler.Throttler) TracerOption {
179	return func(tracer *Tracer) {
180		tracer.debugThrottler = throttler
181	}
182}
183