restriction_manager.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "fmt"
  17. "net/url"
  18. "sync"
  19. "time"
  20. "github.com/uber/jaeger-client-go/internal/baggage"
  21. thrift "github.com/uber/jaeger-client-go/thrift-gen/baggage"
  22. "github.com/uber/jaeger-client-go/utils"
  23. )
  24. type httpBaggageRestrictionManagerProxy struct {
  25. url string
  26. }
  27. func newHTTPBaggageRestrictionManagerProxy(hostPort, serviceName string) *httpBaggageRestrictionManagerProxy {
  28. v := url.Values{}
  29. v.Set("service", serviceName)
  30. return &httpBaggageRestrictionManagerProxy{
  31. url: fmt.Sprintf("http://%s/baggageRestrictions?%s", hostPort, v.Encode()),
  32. }
  33. }
  34. func (s *httpBaggageRestrictionManagerProxy) GetBaggageRestrictions(serviceName string) ([]*thrift.BaggageRestriction, error) {
  35. var out []*thrift.BaggageRestriction
  36. if err := utils.GetJSON(s.url, &out); err != nil {
  37. return nil, err
  38. }
  39. return out, nil
  40. }
  41. // RestrictionManager manages baggage restrictions by retrieving baggage restrictions from agent
  42. type RestrictionManager struct {
  43. options
  44. mux sync.RWMutex
  45. serviceName string
  46. restrictions map[string]*baggage.Restriction
  47. thriftProxy thrift.BaggageRestrictionManager
  48. pollStopped sync.WaitGroup
  49. stopPoll chan struct{}
  50. invalidRestriction *baggage.Restriction
  51. validRestriction *baggage.Restriction
  52. // Determines if the manager has successfully retrieved baggage restrictions from agent
  53. initialized bool
  54. }
  55. // NewRestrictionManager returns a BaggageRestrictionManager that polls the agent for the latest
  56. // baggage restrictions.
  57. func NewRestrictionManager(serviceName string, options ...Option) *RestrictionManager {
  58. // TODO there is a developing use case where a single tracer can generate traces on behalf of many services.
  59. // restrictionsMap will need to exist per service
  60. opts := applyOptions(options...)
  61. m := &RestrictionManager{
  62. serviceName: serviceName,
  63. options: opts,
  64. restrictions: make(map[string]*baggage.Restriction),
  65. thriftProxy: newHTTPBaggageRestrictionManagerProxy(opts.hostPort, serviceName),
  66. stopPoll: make(chan struct{}),
  67. invalidRestriction: baggage.NewRestriction(false, 0),
  68. validRestriction: baggage.NewRestriction(true, defaultMaxValueLength),
  69. }
  70. m.pollStopped.Add(1)
  71. go m.pollManager()
  72. return m
  73. }
  74. // isReady returns true if the manager has retrieved baggage restrictions from the remote source.
  75. func (m *RestrictionManager) isReady() bool {
  76. m.mux.RLock()
  77. defer m.mux.RUnlock()
  78. return m.initialized
  79. }
  80. // GetRestriction implements RestrictionManager#GetRestriction.
  81. func (m *RestrictionManager) GetRestriction(service, key string) *baggage.Restriction {
  82. m.mux.RLock()
  83. defer m.mux.RUnlock()
  84. if !m.initialized {
  85. if m.denyBaggageOnInitializationFailure {
  86. return m.invalidRestriction
  87. }
  88. return m.validRestriction
  89. }
  90. if restriction, ok := m.restrictions[key]; ok {
  91. return restriction
  92. }
  93. return m.invalidRestriction
  94. }
  95. // Close stops remote polling and closes the RemoteRestrictionManager.
  96. func (m *RestrictionManager) Close() error {
  97. close(m.stopPoll)
  98. m.pollStopped.Wait()
  99. return nil
  100. }
  101. func (m *RestrictionManager) pollManager() {
  102. defer m.pollStopped.Done()
  103. // attempt to initialize baggage restrictions
  104. if err := m.updateRestrictions(); err != nil {
  105. m.logger.Error(fmt.Sprintf("Failed to initialize baggage restrictions: %s", err.Error()))
  106. }
  107. ticker := time.NewTicker(m.refreshInterval)
  108. defer ticker.Stop()
  109. for {
  110. select {
  111. case <-ticker.C:
  112. if err := m.updateRestrictions(); err != nil {
  113. m.logger.Error(fmt.Sprintf("Failed to update baggage restrictions: %s", err.Error()))
  114. }
  115. case <-m.stopPoll:
  116. return
  117. }
  118. }
  119. }
  120. func (m *RestrictionManager) updateRestrictions() error {
  121. restrictions, err := m.thriftProxy.GetBaggageRestrictions(m.serviceName)
  122. if err != nil {
  123. m.metrics.BaggageRestrictionsUpdateFailure.Inc(1)
  124. return err
  125. }
  126. newRestrictions := m.parseRestrictions(restrictions)
  127. m.metrics.BaggageRestrictionsUpdateSuccess.Inc(1)
  128. m.mux.Lock()
  129. defer m.mux.Unlock()
  130. m.initialized = true
  131. m.restrictions = newRestrictions
  132. return nil
  133. }
  134. func (m *RestrictionManager) parseRestrictions(restrictions []*thrift.BaggageRestriction) map[string]*baggage.Restriction {
  135. setters := make(map[string]*baggage.Restriction, len(restrictions))
  136. for _, restriction := range restrictions {
  137. setters[restriction.BaggageKey] = baggage.NewRestriction(true, int(restriction.MaxValueLength))
  138. }
  139. return setters
  140. }