tracer_options.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package jaeger
  21. import (
  22. "time"
  23. "github.com/opentracing/opentracing-go"
  24. "github.com/uber/jaeger-client-go/internal/baggage"
  25. )
  26. // TracerOption is a function that sets some option on the tracer
  27. type TracerOption func(tracer *Tracer)
  28. // TracerOptions is a factory for all available TracerOption's
  29. var TracerOptions tracerOptions
  30. type tracerOptions struct{}
  31. // Metrics creates a TracerOption that initializes Metrics on the tracer,
  32. // which is used to emit statistics.
  33. func (tracerOptions) Metrics(m *Metrics) TracerOption {
  34. return func(tracer *Tracer) {
  35. tracer.metrics = *m
  36. }
  37. }
  38. // Logger creates a TracerOption that gives the tracer a Logger.
  39. func (tracerOptions) Logger(logger Logger) TracerOption {
  40. return func(tracer *Tracer) {
  41. tracer.logger = logger
  42. }
  43. }
  44. func (tracerOptions) CustomHeaderKeys(headerKeys *HeadersConfig) TracerOption {
  45. return func(tracer *Tracer) {
  46. if headerKeys == nil {
  47. return
  48. }
  49. textPropagator := newTextMapPropagator(headerKeys.applyDefaults(), tracer.metrics)
  50. tracer.addCodec(opentracing.TextMap, textPropagator, textPropagator)
  51. httpHeaderPropagator := newHTTPHeaderPropagator(headerKeys.applyDefaults(), tracer.metrics)
  52. tracer.addCodec(opentracing.HTTPHeaders, httpHeaderPropagator, httpHeaderPropagator)
  53. }
  54. }
  55. // TimeNow creates a TracerOption that gives the tracer a function
  56. // used to generate timestamps for spans.
  57. func (tracerOptions) TimeNow(timeNow func() time.Time) TracerOption {
  58. return func(tracer *Tracer) {
  59. tracer.timeNow = timeNow
  60. }
  61. }
  62. // RandomNumber creates a TracerOption that gives the tracer
  63. // a thread-safe random number generator function for generating trace IDs.
  64. func (tracerOptions) RandomNumber(randomNumber func() uint64) TracerOption {
  65. return func(tracer *Tracer) {
  66. tracer.randomNumber = randomNumber
  67. }
  68. }
  69. // PoolSpans creates a TracerOption that tells the tracer whether it should use
  70. // an object pool to minimize span allocations.
  71. // This should be used with care, only if the service is not running any async tasks
  72. // that can access parent spans after those spans have been finished.
  73. func (tracerOptions) PoolSpans(poolSpans bool) TracerOption {
  74. return func(tracer *Tracer) {
  75. tracer.options.poolSpans = poolSpans
  76. }
  77. }
  78. // Deprecated: HostIPv4 creates a TracerOption that identifies the current service/process.
  79. // If not set, the factory method will obtain the current IP address.
  80. // The TracerOption is deprecated; the tracer will attempt to automatically detect the IP.
  81. func (tracerOptions) HostIPv4(hostIPv4 uint32) TracerOption {
  82. return func(tracer *Tracer) {
  83. tracer.hostIPv4 = hostIPv4
  84. }
  85. }
  86. func (tracerOptions) Injector(format interface{}, injector Injector) TracerOption {
  87. return func(tracer *Tracer) {
  88. tracer.injectors[format] = injector
  89. }
  90. }
  91. func (tracerOptions) Extractor(format interface{}, extractor Extractor) TracerOption {
  92. return func(tracer *Tracer) {
  93. tracer.extractors[format] = extractor
  94. }
  95. }
  96. func (t tracerOptions) Observer(observer Observer) TracerOption {
  97. return t.ContribObserver(&oldObserver{obs: observer})
  98. }
  99. func (tracerOptions) ContribObserver(observer ContribObserver) TracerOption {
  100. return func(tracer *Tracer) {
  101. tracer.observer.append(observer)
  102. }
  103. }
  104. func (tracerOptions) ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) TracerOption {
  105. return func(tracer *Tracer) {
  106. tracer.options.zipkinSharedRPCSpan = zipkinSharedRPCSpan
  107. }
  108. }
  109. func (tracerOptions) Tag(key string, value interface{}) TracerOption {
  110. return func(tracer *Tracer) {
  111. tracer.tags = append(tracer.tags, Tag{key: key, value: value})
  112. }
  113. }
  114. func (tracerOptions) BaggageRestrictionManager(mgr baggage.RestrictionManager) TracerOption {
  115. return func(tracer *Tracer) {
  116. tracer.baggageRestrictionManager = mgr
  117. }
  118. }