options.go 4.2 KB

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