sampler_remote_options.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 jaeger
  15. import (
  16. "time"
  17. "github.com/uber/jaeger-client-go/log"
  18. )
  19. // SamplerOption is a function that sets some option on the sampler
  20. type SamplerOption func(options *samplerOptions)
  21. // SamplerOptions is a factory for all available SamplerOption's.
  22. var SamplerOptions SamplerOptionsFactory
  23. // SamplerOptionsFactory is a factory for all available SamplerOption's.
  24. // The type acts as a namespace for factory functions. It is public to
  25. // make the functions discoverable via godoc. Recommended to be used
  26. // via global SamplerOptions variable.
  27. type SamplerOptionsFactory struct{}
  28. type samplerOptions struct {
  29. metrics *Metrics
  30. sampler SamplerV2
  31. logger log.DebugLogger
  32. samplingServerURL string
  33. samplingRefreshInterval time.Duration
  34. samplingFetcher SamplingStrategyFetcher
  35. samplingParser SamplingStrategyParser
  36. updaters []SamplerUpdater
  37. posParams PerOperationSamplerParams
  38. }
  39. // Metrics creates a SamplerOption that initializes Metrics on the sampler,
  40. // which is used to emit statistics.
  41. func (SamplerOptionsFactory) Metrics(m *Metrics) SamplerOption {
  42. return func(o *samplerOptions) {
  43. o.metrics = m
  44. }
  45. }
  46. // MaxOperations creates a SamplerOption that sets the maximum number of
  47. // operations the sampler will keep track of.
  48. func (SamplerOptionsFactory) MaxOperations(maxOperations int) SamplerOption {
  49. return func(o *samplerOptions) {
  50. o.posParams.MaxOperations = maxOperations
  51. }
  52. }
  53. // OperationNameLateBinding creates a SamplerOption that sets the respective
  54. // field in the PerOperationSamplerParams.
  55. func (SamplerOptionsFactory) OperationNameLateBinding(enable bool) SamplerOption {
  56. return func(o *samplerOptions) {
  57. o.posParams.OperationNameLateBinding = enable
  58. }
  59. }
  60. // InitialSampler creates a SamplerOption that sets the initial sampler
  61. // to use before a remote sampler is created and used.
  62. func (SamplerOptionsFactory) InitialSampler(sampler Sampler) SamplerOption {
  63. return func(o *samplerOptions) {
  64. o.sampler = samplerV1toV2(sampler)
  65. }
  66. }
  67. // Logger creates a SamplerOption that sets the logger used by the sampler.
  68. func (SamplerOptionsFactory) Logger(logger Logger) SamplerOption {
  69. return func(o *samplerOptions) {
  70. o.logger = log.DebugLogAdapter(logger)
  71. }
  72. }
  73. // SamplingServerURL creates a SamplerOption that sets the sampling server url
  74. // of the local agent that contains the sampling strategies.
  75. func (SamplerOptionsFactory) SamplingServerURL(samplingServerURL string) SamplerOption {
  76. return func(o *samplerOptions) {
  77. o.samplingServerURL = samplingServerURL
  78. }
  79. }
  80. // SamplingRefreshInterval creates a SamplerOption that sets how often the
  81. // sampler will poll local agent for the appropriate sampling strategy.
  82. func (SamplerOptionsFactory) SamplingRefreshInterval(samplingRefreshInterval time.Duration) SamplerOption {
  83. return func(o *samplerOptions) {
  84. o.samplingRefreshInterval = samplingRefreshInterval
  85. }
  86. }
  87. // SamplingStrategyFetcher creates a SamplerOption that initializes sampling strategy fetcher.
  88. func (SamplerOptionsFactory) SamplingStrategyFetcher(fetcher SamplingStrategyFetcher) SamplerOption {
  89. return func(o *samplerOptions) {
  90. o.samplingFetcher = fetcher
  91. }
  92. }
  93. // SamplingStrategyParser creates a SamplerOption that initializes sampling strategy parser.
  94. func (SamplerOptionsFactory) SamplingStrategyParser(parser SamplingStrategyParser) SamplerOption {
  95. return func(o *samplerOptions) {
  96. o.samplingParser = parser
  97. }
  98. }
  99. // Updaters creates a SamplerOption that initializes sampler updaters.
  100. func (SamplerOptionsFactory) Updaters(updaters ...SamplerUpdater) SamplerOption {
  101. return func(o *samplerOptions) {
  102. o.updaters = updaters
  103. }
  104. }
  105. func (o *samplerOptions) applyOptionsAndDefaults(opts ...SamplerOption) *samplerOptions {
  106. for _, option := range opts {
  107. option(o)
  108. }
  109. if o.sampler == nil {
  110. o.sampler = newProbabilisticSampler(0.001)
  111. }
  112. if o.logger == nil {
  113. o.logger = log.NullLogger
  114. }
  115. if o.samplingServerURL == "" {
  116. o.samplingServerURL = DefaultSamplingServerURL
  117. }
  118. if o.metrics == nil {
  119. o.metrics = NewNullMetrics()
  120. }
  121. if o.samplingRefreshInterval <= 0 {
  122. o.samplingRefreshInterval = defaultSamplingRefreshInterval
  123. }
  124. if o.samplingFetcher == nil {
  125. o.samplingFetcher = newHTTPSamplingStrategyFetcher(o.samplingServerURL, o.logger)
  126. }
  127. if o.samplingParser == nil {
  128. o.samplingParser = new(samplingStrategyParser)
  129. }
  130. if o.updaters == nil {
  131. o.updaters = []SamplerUpdater{
  132. &AdaptiveSamplerUpdater{
  133. MaxOperations: o.posParams.MaxOperations,
  134. OperationNameLateBinding: o.posParams.OperationNameLateBinding,
  135. },
  136. new(ProbabilisticSamplerUpdater),
  137. new(RateLimitingSamplerUpdater),
  138. }
  139. }
  140. return o
  141. }