options.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2018 The Jaeger Authors.
  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 remote
  15. import (
  16. "time"
  17. "github.com/uber/jaeger-client-go"
  18. )
  19. const (
  20. defaultHostPort = "localhost:5778"
  21. defaultRefreshInterval = time.Second * 5
  22. )
  23. // Option is a function that sets some option on the Throttler
  24. type Option func(options *options)
  25. // Options is a factory for all available options
  26. var Options options
  27. type options struct {
  28. metrics *jaeger.Metrics
  29. logger jaeger.Logger
  30. hostPort string
  31. refreshInterval time.Duration
  32. synchronousInitialization bool
  33. }
  34. // Metrics creates an Option that initializes Metrics on the Throttler, which is used to emit statistics.
  35. func (options) Metrics(m *jaeger.Metrics) Option {
  36. return func(o *options) {
  37. o.metrics = m
  38. }
  39. }
  40. // Logger creates an Option that sets the logger used by the Throttler.
  41. func (options) Logger(logger jaeger.Logger) Option {
  42. return func(o *options) {
  43. o.logger = logger
  44. }
  45. }
  46. // HostPort creates an Option that sets the hostPort of the local agent that keeps track of credits.
  47. func (options) HostPort(hostPort string) Option {
  48. return func(o *options) {
  49. o.hostPort = hostPort
  50. }
  51. }
  52. // RefreshInterval creates an Option that sets how often the Throttler will poll local agent for
  53. // credits.
  54. func (options) RefreshInterval(refreshInterval time.Duration) Option {
  55. return func(o *options) {
  56. o.refreshInterval = refreshInterval
  57. }
  58. }
  59. // SynchronousInitialization creates an Option that determines whether the throttler should synchronously
  60. // fetch credits from the agent when an operation is seen for the first time. This should be set to true
  61. // if the client will be used by a short lived service that needs to ensure that credits are fetched upfront
  62. // such that sampling or throttling occurs.
  63. func (options) SynchronousInitialization(b bool) Option {
  64. return func(o *options) {
  65. o.synchronousInitialization = b
  66. }
  67. }
  68. func applyOptions(o ...Option) options {
  69. opts := options{}
  70. for _, option := range o {
  71. option(&opts)
  72. }
  73. if opts.metrics == nil {
  74. opts.metrics = jaeger.NewNullMetrics()
  75. }
  76. if opts.logger == nil {
  77. opts.logger = jaeger.NullLogger
  78. }
  79. if opts.hostPort == "" {
  80. opts.hostPort = defaultHostPort
  81. }
  82. if opts.refreshInterval == 0 {
  83. opts.refreshInterval = defaultRefreshInterval
  84. }
  85. return opts
  86. }