tracer_options.go 4.5 KB

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