options.go 3.2 KB

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