options.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 remote
  21. import (
  22. "time"
  23. "github.com/uber/jaeger-client-go"
  24. )
  25. const (
  26. defaultMaxValueLength = 2048
  27. defaultRefreshInterval = time.Minute
  28. defaultHostPort = "localhost:5778"
  29. )
  30. // Option is a function that sets some option on the RestrictionManager
  31. type Option func(options *options)
  32. // Options is a factory for all available options
  33. var Options options
  34. type options struct {
  35. denyBaggageOnInitializationFailure bool
  36. metrics *jaeger.Metrics
  37. logger jaeger.Logger
  38. hostPort string
  39. refreshInterval time.Duration
  40. }
  41. // DenyBaggageOnInitializationFailure creates an Option that determines the startup failure mode of RestrictionManager.
  42. // If DenyBaggageOnInitializationFailure is true, RestrictionManager will not allow any baggage to be written until baggage
  43. // restrictions have been retrieved from agent.
  44. // If DenyBaggageOnInitializationFailure is false, RestrictionManager will allow any baggage to be written until baggage
  45. // restrictions have been retrieved from agent.
  46. func (options) DenyBaggageOnInitializationFailure(b bool) Option {
  47. return func(o *options) {
  48. o.denyBaggageOnInitializationFailure = b
  49. }
  50. }
  51. // Metrics creates an Option that initializes Metrics on the RestrictionManager, which is used to emit statistics.
  52. func (options) Metrics(m *jaeger.Metrics) Option {
  53. return func(o *options) {
  54. o.metrics = m
  55. }
  56. }
  57. // Logger creates an Option that sets the logger used by the RestrictionManager.
  58. func (options) Logger(logger jaeger.Logger) Option {
  59. return func(o *options) {
  60. o.logger = logger
  61. }
  62. }
  63. // HostPort creates an Option that sets the hostPort of the local agent that contains the baggage restrictions.
  64. func (options) HostPort(hostPort string) Option {
  65. return func(o *options) {
  66. o.hostPort = hostPort
  67. }
  68. }
  69. // RefreshInterval creates an Option that sets how often the RestrictionManager will poll local agent for
  70. // the baggage restrictions.
  71. func (options) RefreshInterval(refreshInterval time.Duration) Option {
  72. return func(o *options) {
  73. o.refreshInterval = refreshInterval
  74. }
  75. }
  76. func applyOptions(o ...Option) options {
  77. opts := options{}
  78. for _, option := range o {
  79. option(&opts)
  80. }
  81. if opts.metrics == nil {
  82. opts.metrics = jaeger.NewNullMetrics()
  83. }
  84. if opts.logger == nil {
  85. opts.logger = jaeger.NullLogger
  86. }
  87. if opts.hostPort == "" {
  88. opts.hostPort = defaultHostPort
  89. }
  90. if opts.refreshInterval == 0 {
  91. opts.refreshInterval = defaultRefreshInterval
  92. }
  93. return opts
  94. }