restriction_manager.go 4.9 KB

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