options.go 3.1 KB

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