Skip to content

Commit

Permalink
Merge pull request #70 from openzipkin/exposes_binary_samplers
Browse files Browse the repository at this point in the history
[#68] Exposes alwaysSample and NeverSample to easier usage.
  • Loading branch information
basvanbeek committed Jul 16, 2018
2 parents 3b2a7d2 + 9ab04bd commit 8a54c36
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNoopContext(t *testing.T) {
tr, _ = NewTracer(
reporter.NewNoopReporter(),
WithNoopSpan(true),
WithSampler(neverSample),
WithSampler(NeverSample),
WithSharedSpans(true),
)
)
Expand Down
20 changes: 13 additions & 7 deletions sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ import (
// traceID.
type Sampler func(id uint64) bool

func neverSample(_ uint64) bool { return false }
// NeverSample will always return false. If used by a service it will not allow
// the service to start traces but will still allow the service to participate
// in traces started upstream.
func NeverSample(_ uint64) bool { return false }

func alwaysSample(_ uint64) bool { return true }
// AlwaysSample will always return true. If used by a service it will always start
// traces if no upstream trace has been propagated. If an incoming upstream trace
// is not sampled the service will adhere to this and only propagate the context.
func AlwaysSample(_ uint64) bool { return true }

// NewModuloSampler provides a generic type Sampler.
func NewModuloSampler(mod uint64) Sampler {
if mod < 2 {
return alwaysSample
return AlwaysSample
}
return func(id uint64) bool {
return (id % mod) == 0
Expand All @@ -31,10 +37,10 @@ func NewModuloSampler(mod uint64) Sampler {
// It defends against nodes in the cluster selecting exactly the same ids.
func NewBoundarySampler(rate float64, salt int64) (Sampler, error) {
if rate == 0.0 {
return neverSample, nil
return NeverSample, nil
}
if rate == 1.0 {
return alwaysSample, nil
return AlwaysSample, nil
}
if rate < 0.0001 || rate > 1 {
return nil, fmt.Errorf("rate should be 0.0 or between 0.0001 and 1: was %f", rate)
Expand All @@ -55,10 +61,10 @@ func NewBoundarySampler(rate float64, salt int64) (Sampler, error) {
// on trace id).
func NewCountingSampler(rate float64) (Sampler, error) {
if rate == 0.0 {
return neverSample, nil
return NeverSample, nil
}
if rate == 1.0 {
return alwaysSample, nil
return AlwaysSample, nil
}
if rate < 0.01 || rate > 1 {
return nil, fmt.Errorf("rate should be 0.0 or between 0.01 and 1: was %f", rate)
Expand Down
2 changes: 1 addition & 1 deletion tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewTracer(rep reporter.Reporter, opts ...TracerOption) (*Tracer, error) {
t := &Tracer{
defaultTags: make(map[string]string),
extractFailurePolicy: ExtractFailurePolicyRestart,
sampler: alwaysSample,
sampler: AlwaysSample,
generate: idgenerator.NewRandom64(),
reporter: rep,
localEndpoint: nil,
Expand Down

0 comments on commit 8a54c36

Please sign in to comment.