tracer_options.go 7.0 KB

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