This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
Upgrade all samplers to v2 #450
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ import ( | |
"sync/atomic" | ||
"time" | ||
|
||
"github.com/uber/jaeger-client-go/log" | ||
"github.com/uber/jaeger-client-go/thrift-gen/sampling" | ||
"github.com/uber/jaeger-client-go/utils" | ||
) | ||
|
@@ -30,8 +29,6 @@ const ( | |
defaultSamplingRefreshInterval = time.Minute | ||
) | ||
|
||
// ----------------------- | ||
|
||
// RemotelyControlledSampler is a delegating sampler that polls a remote server | ||
// for the appropriate sampling strategy, constructs a corresponding sampler and | ||
// delegates to it for sampling decisions. | ||
|
@@ -48,29 +45,15 @@ type RemotelyControlledSampler struct { | |
doneChan chan *sync.WaitGroup | ||
} | ||
|
||
type httpSamplingManager struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to bottom of the file |
||
serverURL string | ||
} | ||
|
||
func (s *httpSamplingManager) GetSamplingStrategy(serviceName string) (*sampling.SamplingStrategyResponse, error) { | ||
var out sampling.SamplingStrategyResponse | ||
v := url.Values{} | ||
v.Set("service", serviceName) | ||
if err := utils.GetJSON(s.serverURL+"?"+v.Encode(), &out); err != nil { | ||
return nil, err | ||
} | ||
return &out, nil | ||
} | ||
|
||
// NewRemotelyControlledSampler creates a sampler that periodically pulls | ||
// the sampling strategy from an HTTP sampling server (e.g. jaeger-agent). | ||
func NewRemotelyControlledSampler( | ||
serviceName string, | ||
opts ...SamplerOption, | ||
) *RemotelyControlledSampler { | ||
options := applySamplerOptions(opts...) | ||
options := new(samplerOptions).applyOptionsAndDefaults(opts...) | ||
sampler := &RemotelyControlledSampler{ | ||
samplerOptions: options, | ||
samplerOptions: *options, | ||
serviceName: serviceName, | ||
manager: &httpSamplingManager{serverURL: options.samplingServerURL}, | ||
doneChan: make(chan *sync.WaitGroup), | ||
|
@@ -79,37 +62,26 @@ func NewRemotelyControlledSampler( | |
return sampler | ||
} | ||
|
||
func applySamplerOptions(opts ...SamplerOption) samplerOptions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to sampler_remote_options |
||
options := samplerOptions{} | ||
for _, option := range opts { | ||
option(&options) | ||
} | ||
if options.sampler == nil { | ||
options.sampler = newProbabilisticSampler(0.001) | ||
} | ||
if options.logger == nil { | ||
options.logger = log.NullLogger | ||
} | ||
if options.maxOperations <= 0 { | ||
options.maxOperations = defaultMaxOperations | ||
} | ||
if options.samplingServerURL == "" { | ||
options.samplingServerURL = DefaultSamplingServerURL | ||
} | ||
if options.metrics == nil { | ||
options.metrics = NewNullMetrics() | ||
} | ||
if options.samplingRefreshInterval <= 0 { | ||
options.samplingRefreshInterval = defaultSamplingRefreshInterval | ||
} | ||
return options | ||
} | ||
|
||
// IsSampled implements IsSampled() of Sampler. | ||
// TODO (breaking change) remove when Sampler V1 is removed | ||
func (s *RemotelyControlledSampler) IsSampled(id TraceID, operation string) (bool, []Tag) { | ||
s.RLock() | ||
defer s.RUnlock() | ||
return s.sampler.IsSampled(id, operation) | ||
return false, nil | ||
} | ||
|
||
func (s *RemotelyControlledSampler) OnCreateSpan(span *Span) SamplingDecision { | ||
return s.sampler.OnCreateSpan(span) | ||
} | ||
|
||
func (s *RemotelyControlledSampler) OnSetOperationName(span *Span, operationName string) SamplingDecision { | ||
return s.sampler.OnSetOperationName(span, operationName) | ||
} | ||
|
||
func (s *RemotelyControlledSampler) OnSetTag(span *Span, key string, value interface{}) SamplingDecision { | ||
return s.sampler.OnSetTag(span, key, value) | ||
} | ||
|
||
func (s *RemotelyControlledSampler) OnFinishSpan(span *Span) SamplingDecision { | ||
return s.sampler.OnFinishSpan(span) | ||
} | ||
|
||
// Close implements Close() of Sampler. | ||
|
@@ -134,7 +106,7 @@ func (s *RemotelyControlledSampler) Equal(other Sampler) bool { | |
o.RLock() | ||
defer s.RUnlock() | ||
defer o.RUnlock() | ||
return s.sampler.Equal(o.sampler) | ||
return s.sampler.(Sampler).Equal(o.sampler.(Sampler)) | ||
} | ||
return false | ||
} | ||
|
@@ -157,13 +129,13 @@ func (s *RemotelyControlledSampler) pollControllerWithTicker(ticker *time.Ticker | |
} | ||
} | ||
|
||
func (s *RemotelyControlledSampler) getSampler() Sampler { | ||
func (s *RemotelyControlledSampler) getSampler() SamplerV2 { | ||
s.Lock() | ||
defer s.Unlock() | ||
return s.sampler | ||
} | ||
|
||
func (s *RemotelyControlledSampler) setSampler(sampler Sampler) { | ||
func (s *RemotelyControlledSampler) setSampler(sampler SamplerV2) { | ||
s.Lock() | ||
defer s.Unlock() | ||
s.sampler = sampler | ||
|
@@ -195,7 +167,7 @@ func (s *RemotelyControlledSampler) updateSampler() { | |
|
||
// NB: this function should only be called while holding a Write lock | ||
func (s *RemotelyControlledSampler) updateAdaptiveSampler(strategies *sampling.PerOperationSamplingStrategies) { | ||
if adaptiveSampler, ok := s.sampler.(*adaptiveSampler); ok { | ||
if adaptiveSampler, ok := s.sampler.(*AdaptiveSampler); ok { | ||
adaptiveSampler.update(strategies) | ||
} else { | ||
s.sampler = newAdaptiveSampler(strategies, s.maxOperations) | ||
|
@@ -204,16 +176,30 @@ func (s *RemotelyControlledSampler) updateAdaptiveSampler(strategies *sampling.P | |
|
||
// NB: this function should only be called while holding a Write lock | ||
func (s *RemotelyControlledSampler) updateRateLimitingOrProbabilisticSampler(res *sampling.SamplingStrategyResponse) error { | ||
var newSampler Sampler | ||
var newSampler SamplerV2 | ||
if probabilistic := res.GetProbabilisticSampling(); probabilistic != nil { | ||
newSampler = newProbabilisticSampler(probabilistic.SamplingRate) | ||
} else if rateLimiting := res.GetRateLimitingSampling(); rateLimiting != nil { | ||
newSampler = NewRateLimitingSampler(float64(rateLimiting.MaxTracesPerSecond)) | ||
} else { | ||
return fmt.Errorf("Unsupported sampling strategy type %v", res.GetStrategyType()) | ||
} | ||
if !s.sampler.Equal(newSampler) { | ||
if !s.sampler.(Sampler).Equal(newSampler.(Sampler)) { | ||
s.sampler = newSampler | ||
} | ||
return nil | ||
} | ||
|
||
type httpSamplingManager struct { | ||
serverURL string | ||
} | ||
|
||
func (s *httpSamplingManager) GetSamplingStrategy(serviceName string) (*sampling.SamplingStrategyResponse, error) { | ||
var out sampling.SamplingStrategyResponse | ||
v := url.Values{} | ||
v.Set("service", serviceName) | ||
if err := utils.GetJSON(s.serverURL+"?"+v.Encode(), &out); err != nil { | ||
return nil, err | ||
} | ||
return &out, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is new behavior - do we require this right now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean new to Go client?
I don't think we can avoid it. The main change is not actually here, but in OnCreateSpan, which must return retryable=true in order for tag sampling to work (otherwise the state will be finalized). So if we already have that in OnCreateSpan, adding OnSetOperationName() doesn't change things that much, imo.