options.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package config
  21. import (
  22. opentracing "github.com/opentracing/opentracing-go"
  23. "github.com/uber/jaeger-lib/metrics"
  24. "github.com/uber/jaeger-client-go"
  25. )
  26. // Option is a function that sets some option on the client.
  27. type Option func(c *Options)
  28. // Options control behavior of the client.
  29. type Options struct {
  30. metrics metrics.Factory
  31. logger jaeger.Logger
  32. reporter jaeger.Reporter
  33. contribObservers []jaeger.ContribObserver
  34. observers []jaeger.Observer
  35. zipkinSharedRPCSpan bool
  36. tags []opentracing.Tag
  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. // Observer can be registered with the Tracer to receive notifications about new Spans.
  60. func Observer(observer jaeger.Observer) Option {
  61. return func(c *Options) {
  62. c.observers = append(c.observers, observer)
  63. }
  64. }
  65. // ContribObserver can be registered with the Tracer to recieve notifications
  66. // about new spans.
  67. func ContribObserver(observer jaeger.ContribObserver) Option {
  68. return func(c *Options) {
  69. c.contribObservers = append(c.contribObservers, observer)
  70. }
  71. }
  72. // ZipkinSharedRPCSpan creates an option that enables sharing span ID between client
  73. // and server spans a la zipkin. If false, client and server spans will be assigned
  74. // different IDs.
  75. func ZipkinSharedRPCSpan(zipkinSharedRPCSpan bool) Option {
  76. return func(c *Options) {
  77. c.zipkinSharedRPCSpan = zipkinSharedRPCSpan
  78. }
  79. }
  80. // Tag creates an option that adds a tracer-level tag.
  81. func Tag(key string, value interface{}) Option {
  82. return func(c *Options) {
  83. c.tags = append(c.tags, opentracing.Tag{Key: key, Value: value})
  84. }
  85. }
  86. func applyOptions(options ...Option) Options {
  87. opts := Options{}
  88. for _, option := range options {
  89. option(&opts)
  90. }
  91. if opts.metrics == nil {
  92. opts.metrics = metrics.NullFactory
  93. }
  94. if opts.logger == nil {
  95. opts.logger = jaeger.NullLogger
  96. }
  97. return opts
  98. }