options.go 4.0 KB

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