tracer_options.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. )
  21. // TracerOption is a function that sets some option on the tracer
  22. type TracerOption func(tracer *Tracer)
  23. // TracerOptions is a factory for all available TracerOption's
  24. var TracerOptions tracerOptions
  25. type tracerOptions struct{}
  26. // Metrics creates a TracerOption that initializes Metrics on the tracer,
  27. // which is used to emit statistics.
  28. func (tracerOptions) Metrics(m *Metrics) TracerOption {
  29. return func(tracer *Tracer) {
  30. tracer.metrics = *m
  31. }
  32. }
  33. // Logger creates a TracerOption that gives the tracer a Logger.
  34. func (tracerOptions) Logger(logger Logger) TracerOption {
  35. return func(tracer *Tracer) {
  36. tracer.logger = logger
  37. }
  38. }
  39. func (tracerOptions) CustomHeaderKeys(headerKeys *HeadersConfig) TracerOption {
  40. return func(tracer *Tracer) {
  41. if headerKeys == nil {
  42. return
  43. }
  44. textPropagator := newTextMapPropagator(headerKeys.applyDefaults(), tracer.metrics)
  45. tracer.addCodec(opentracing.TextMap, textPropagator, textPropagator)
  46. httpHeaderPropagator := newHTTPHeaderPropagator(headerKeys.applyDefaults(), tracer.metrics)
  47. tracer.addCodec(opentracing.HTTPHeaders, httpHeaderPropagator, httpHeaderPropagator)
  48. }
  49. }
  50. // TimeNow creates a TracerOption that gives the tracer a function
  51. // used to generate timestamps for spans.
  52. func (tracerOptions) TimeNow(timeNow func() time.Time) TracerOption {
  53. return func(tracer *Tracer) {
  54. tracer.timeNow = timeNow
  55. }
  56. }
  57. // RandomNumber creates a TracerOption that gives the tracer
  58. // a thread-safe random number generator function for generating trace IDs.
  59. func (tracerOptions) RandomNumber(randomNumber func() uint64) TracerOption {
  60. return func(tracer *Tracer) {
  61. tracer.randomNumber = randomNumber
  62. }
  63. }
  64. // PoolSpans creates a TracerOption that tells the tracer whether it should use
  65. // an object pool to minimize span allocations.
  66. // This should be used with care, only if the service is not running any async tasks
  67. // that can access parent spans after those spans have been finished.
  68. func (tracerOptions) PoolSpans(poolSpans bool) TracerOption {
  69. return func(tracer *Tracer) {
  70. tracer.options.poolSpans = poolSpans
  71. }
  72. }
  73. // Deprecated: HostIPv4 creates a TracerOption that identifies the current service/process.
  74. // If not set, the factory method will obtain the current IP address.
  75. // The TracerOption is deprecated; the tracer will attempt to automatically detect the IP.
  76. func (tracerOptions) HostIPv4(hostIPv4 uint32) TracerOption {
  77. return func(tracer *Tracer) {
  78. tracer.hostIPv4 = hostIPv4
  79. }
  80. }
  81. func (tracerOptions) Injector(format interface{}, injector Injector) TracerOption {
  82. return func(tracer *Tracer) {
  83. tracer.injectors[format] = injector
  84. }
  85. }
  86. func (tracerOptions) Extractor(format interface{}, extractor Extractor) TracerOption {
  87. return func(tracer *Tracer) {
  88. tracer.extractors[format] = extractor
  89. }
  90. }
  91. func (t tracerOptions) Observer(observer Observer) TracerOption {
  92. return t.ContribObserver(&oldObserver{obs: observer})
  93. }
  94. func (tracerOptions) ContribObserver(observer ContribObserver) TracerOption {
  95. return func(tracer *Tracer) {
  96. tracer.observer.append(observer)
  97. }
  98. }
  99. func (tracerOptions) Gen128Bit(gen128Bit bool) TracerOption {
  100. return func(tracer *Tracer) {
  101. tracer.options.gen128Bit = gen128Bit
  102. }
  103. }
  104. func (tracerOptions) HighTraceIDGenerator(highTraceIDGenerator func() uint64) TracerOption {
  105. return func(tracer *Tracer) {
  106. tracer.options.highTraceIDGenerator = highTraceIDGenerator
  107. }
  108. }
  109. func (tracerOptions) MaxTagValueLength(maxTagValueLength int) TracerOption {
  110. return func(tracer *Tracer) {
  111. tracer.options.maxTagValueLength = maxTagValueLength
  112. }
  113. }
  114. func (tracerOptions) ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) TracerOption {
  115. return func(tracer *Tracer) {
  116. tracer.options.zipkinSharedRPCSpan = zipkinSharedRPCSpan
  117. }
  118. }
  119. func (tracerOptions) Tag(key string, value interface{}) TracerOption {
  120. return func(tracer *Tracer) {
  121. tracer.tags = append(tracer.tags, Tag{key: key, value: value})
  122. }
  123. }
  124. func (tracerOptions) BaggageRestrictionManager(mgr baggage.RestrictionManager) TracerOption {
  125. return func(tracer *Tracer) {
  126. tracer.baggageRestrictionManager = mgr
  127. }
  128. }
  129. func (tracerOptions) DebugThrottler(throttler throttler.Throttler) TracerOption {
  130. return func(tracer *Tracer) {
  131. tracer.debugThrottler = throttler
  132. }
  133. }