options.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 config
  15. import (
  16. opentracing "github.com/opentracing/opentracing-go"
  17. "github.com/uber/jaeger-lib/metrics"
  18. "github.com/uber/jaeger-client-go"
  19. )
  20. // Option is a function that sets some option on the client.
  21. type Option func(c *Options)
  22. // Options control behavior of the client.
  23. type Options struct {
  24. metrics metrics.Factory
  25. logger jaeger.Logger
  26. reporter jaeger.Reporter
  27. sampler jaeger.Sampler
  28. contribObservers []jaeger.ContribObserver
  29. observers []jaeger.Observer
  30. gen128Bit bool
  31. poolSpans bool
  32. zipkinSharedRPCSpan bool
  33. maxTagValueLength int
  34. noDebugFlagOnForcedSampling bool
  35. tags []opentracing.Tag
  36. injectors map[interface{}]jaeger.Injector
  37. extractors map[interface{}]jaeger.Extractor
  38. randomNumber func() uint64
  39. }
  40. // Metrics creates an Option that initializes Metrics in the tracer,
  41. // which is used to emit statistics about spans.
  42. func Metrics(factory metrics.Factory) Option {
  43. return func(c *Options) {
  44. c.metrics = factory
  45. }
  46. }
  47. // Logger can be provided to log Reporter errors, as well as to log spans
  48. // if Reporter.LogSpans is set to true.
  49. func Logger(logger jaeger.Logger) Option {
  50. return func(c *Options) {
  51. c.logger = logger
  52. }
  53. }
  54. // Reporter can be provided explicitly to override the configuration.
  55. // Useful for testing, e.g. by passing InMemoryReporter.
  56. func Reporter(reporter jaeger.Reporter) Option {
  57. return func(c *Options) {
  58. c.reporter = reporter
  59. }
  60. }
  61. // Sampler can be provided explicitly to override the configuration.
  62. func Sampler(sampler jaeger.Sampler) Option {
  63. return func(c *Options) {
  64. c.sampler = sampler
  65. }
  66. }
  67. // Observer can be registered with the Tracer to receive notifications about new Spans.
  68. func Observer(observer jaeger.Observer) Option {
  69. return func(c *Options) {
  70. c.observers = append(c.observers, observer)
  71. }
  72. }
  73. // ContribObserver can be registered with the Tracer to receive notifications
  74. // about new spans.
  75. func ContribObserver(observer jaeger.ContribObserver) Option {
  76. return func(c *Options) {
  77. c.contribObservers = append(c.contribObservers, observer)
  78. }
  79. }
  80. // Gen128Bit specifies whether to generate 128bit trace IDs.
  81. func Gen128Bit(gen128Bit bool) Option {
  82. return func(c *Options) {
  83. c.gen128Bit = gen128Bit
  84. }
  85. }
  86. // PoolSpans specifies whether to pool spans
  87. func PoolSpans(poolSpans bool) Option {
  88. return func(c *Options) {
  89. c.poolSpans = poolSpans
  90. }
  91. }
  92. // ZipkinSharedRPCSpan creates an option that enables sharing span ID between client
  93. // and server spans a la zipkin. If false, client and server spans will be assigned
  94. // different IDs.
  95. func ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) Option {
  96. return func(c *Options) {
  97. c.zipkinSharedRPCSpan = zipkinSharedRPCSpan
  98. }
  99. }
  100. // MaxTagValueLength can be provided to override the default max tag value length.
  101. func MaxTagValueLength(maxTagValueLength int) Option {
  102. return func(c *Options) {
  103. c.maxTagValueLength = maxTagValueLength
  104. }
  105. }
  106. // NoDebugFlagOnForcedSampling can be used to decide whether debug flag will be set or not
  107. // when calling span.setSamplingPriority to force sample a span.
  108. func NoDebugFlagOnForcedSampling(noDebugFlagOnForcedSampling bool) Option {
  109. return func(c *Options) {
  110. c.noDebugFlagOnForcedSampling = noDebugFlagOnForcedSampling
  111. }
  112. }
  113. // Tag creates an option that adds a tracer-level tag.
  114. func Tag(key string, value interface{}) Option {
  115. return func(c *Options) {
  116. c.tags = append(c.tags, opentracing.Tag{Key: key, Value: value})
  117. }
  118. }
  119. // Injector registers an Injector with the given format.
  120. func Injector(format interface{}, injector jaeger.Injector) Option {
  121. return func(c *Options) {
  122. c.injectors[format] = injector
  123. }
  124. }
  125. // Extractor registers an Extractor with the given format.
  126. func Extractor(format interface{}, extractor jaeger.Extractor) Option {
  127. return func(c *Options) {
  128. c.extractors[format] = extractor
  129. }
  130. }
  131. // WithRandomNumber supplies a random number generator function to the Tracer used to generate trace and span IDs.
  132. func WithRandomNumber(f func() uint64) Option {
  133. return func(c *Options) {
  134. c.randomNumber = f
  135. }
  136. }
  137. func applyOptions(options ...Option) Options {
  138. opts := Options{
  139. injectors: make(map[interface{}]jaeger.Injector),
  140. extractors: make(map[interface{}]jaeger.Extractor),
  141. }
  142. for _, option := range options {
  143. option(&opts)
  144. }
  145. if opts.metrics == nil {
  146. opts.metrics = metrics.NullFactory
  147. }
  148. if opts.logger == nil {
  149. opts.logger = jaeger.NullLogger
  150. }
  151. return opts
  152. }