options.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. tags []opentracing.Tag
  35. injectors map[interface{}]jaeger.Injector
  36. extractors map[interface{}]jaeger.Extractor
  37. }
  38. // Metrics creates an Option that initializes Metrics in the tracer,
  39. // which is used to emit statistics about spans.
  40. func Metrics(factory metrics.Factory) Option {
  41. return func(c *Options) {
  42. c.metrics = factory
  43. }
  44. }
  45. // Logger can be provided to log Reporter errors, as well as to log spans
  46. // if Reporter.LogSpans is set to true.
  47. func Logger(logger jaeger.Logger) Option {
  48. return func(c *Options) {
  49. c.logger = logger
  50. }
  51. }
  52. // Reporter can be provided explicitly to override the configuration.
  53. // Useful for testing, e.g. by passing InMemoryReporter.
  54. func Reporter(reporter jaeger.Reporter) Option {
  55. return func(c *Options) {
  56. c.reporter = reporter
  57. }
  58. }
  59. // Sampler can be provided explicitly to override the configuration.
  60. func Sampler(sampler jaeger.Sampler) Option {
  61. return func(c *Options) {
  62. c.sampler = sampler
  63. }
  64. }
  65. // Observer can be registered with the Tracer to receive notifications about new Spans.
  66. func Observer(observer jaeger.Observer) Option {
  67. return func(c *Options) {
  68. c.observers = append(c.observers, observer)
  69. }
  70. }
  71. // ContribObserver can be registered with the Tracer to receive notifications
  72. // about new spans.
  73. func ContribObserver(observer jaeger.ContribObserver) Option {
  74. return func(c *Options) {
  75. c.contribObservers = append(c.contribObservers, observer)
  76. }
  77. }
  78. // Gen128Bit specifies whether to generate 128bit trace IDs.
  79. func Gen128Bit(gen128Bit bool) Option {
  80. return func(c *Options) {
  81. c.gen128Bit = gen128Bit
  82. }
  83. }
  84. // PoolSpans specifies whether to pool spans
  85. func PoolSpans(poolSpans bool) Option {
  86. return func(c *Options) {
  87. c.poolSpans = poolSpans
  88. }
  89. }
  90. // ZipkinSharedRPCSpan creates an option that enables sharing span ID between client
  91. // and server spans a la zipkin. If false, client and server spans will be assigned
  92. // different IDs.
  93. func ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) Option {
  94. return func(c *Options) {
  95. c.zipkinSharedRPCSpan = zipkinSharedRPCSpan
  96. }
  97. }
  98. // MaxTagValueLength can be provided to override the default max tag value length.
  99. func MaxTagValueLength(maxTagValueLength int) Option {
  100. return func(c *Options) {
  101. c.maxTagValueLength = maxTagValueLength
  102. }
  103. }
  104. // Tag creates an option that adds a tracer-level tag.
  105. func Tag(key string, value interface{}) Option {
  106. return func(c *Options) {
  107. c.tags = append(c.tags, opentracing.Tag{Key: key, Value: value})
  108. }
  109. }
  110. // Injector registers an Injector with the given format.
  111. func Injector(format interface{}, injector jaeger.Injector) Option {
  112. return func(c *Options) {
  113. c.injectors[format] = injector
  114. }
  115. }
  116. // Extractor registers an Extractor with the given format.
  117. func Extractor(format interface{}, extractor jaeger.Extractor) Option {
  118. return func(c *Options) {
  119. c.extractors[format] = extractor
  120. }
  121. }
  122. func applyOptions(options ...Option) Options {
  123. opts := Options{
  124. injectors: make(map[interface{}]jaeger.Injector),
  125. extractors: make(map[interface{}]jaeger.Extractor),
  126. }
  127. for _, option := range options {
  128. option(&opts)
  129. }
  130. if opts.metrics == nil {
  131. opts.metrics = metrics.NullFactory
  132. }
  133. if opts.logger == nil {
  134. opts.logger = jaeger.NullLogger
  135. }
  136. return opts
  137. }