Skip to content

Commit

Permalink
Changes AlwaysParentSample to ParentSample(fallback)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jun 10, 2020
1 parent 4bf35c6 commit 86da2f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
33 changes: 25 additions & 8 deletions sdk/trace/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,30 @@ func NeverSample() Sampler {
return alwaysOffSampler{}
}

// AlwaysParentSample returns a Sampler that samples a trace only
// if the parent span is sampled.
// This Sampler is a passthrough to the ProbabilitySampler with
// a fraction of value 0.
func AlwaysParentSample() Sampler {
return &probabilitySampler{
traceIDUpperBound: 0,
description: "AlwaysParentSampler",
// ParentSample returns a Sampler that samples a trace only
// if the the span has a parent span and it is sampled. If the span has
// parent span but it is not sampled, neither will this span. If the span
// does not have a parent the fallback Sampler is used to determine if the
// span should be sampled.
func ParentSample(fallback Sampler) Sampler {
return parentSampler{fallback}
}

type parentSampler struct {
fallback Sampler
}

func (ps parentSampler) ShouldSample(p SamplingParameters) SamplingResult {
if p.ParentContext.IsValid() {
if p.ParentContext.IsSampled() {
return SamplingResult{Decision: RecordAndSampled}
} else {
return SamplingResult{Decision: NotRecord}
}
}
return ps.fallback.ShouldSample(p)
}

func (ps parentSampler) Description() string {
return fmt.Sprintf("ParentOrElse{%s}", ps.fallback.Description())
}
32 changes: 30 additions & 2 deletions sdk/trace/sampling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ import (
)

func TestAlwaysParentSampleWithParentSampled(t *testing.T) {
sampler := sdktrace.AlwaysParentSample()
sampler := sdktrace.ParentSample(sdktrace.AlwaysSample())
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
parentCtx := trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
}
if sampler.ShouldSample(sdktrace.SamplingParameters{ParentContext: parentCtx}).Decision != sdktrace.RecordAndSampled {
t.Error("Sampling decision should be RecordAndSampled")
}
}

func TestNeverParentSampleWithParentSampled(t *testing.T) {
sampler := sdktrace.ParentSample(sdktrace.NeverSample())
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
parentCtx := trace.SpanContext{
Expand All @@ -37,7 +51,7 @@ func TestAlwaysParentSampleWithParentSampled(t *testing.T) {
}

func TestAlwaysParentSampleWithParentNotSampled(t *testing.T) {
sampler := sdktrace.AlwaysParentSample()
sampler := sdktrace.ParentSample(sdktrace.AlwaysSample())
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
parentCtx := trace.SpanContext{
Expand All @@ -48,3 +62,17 @@ func TestAlwaysParentSampleWithParentNotSampled(t *testing.T) {
t.Error("Sampling decision should be NotRecord")
}
}

func TestParentSampleWithNoParent(t *testing.T) {
params := sdktrace.SamplingParameters{}

sampler := sdktrace.ParentSample(sdktrace.AlwaysSample())
if sampler.ShouldSample(params).Decision != sdktrace.RecordAndSampled {
t.Error("Sampling decision should be RecordAndSampled")
}

sampler = sdktrace.ParentSample(sdktrace.NeverSample())
if sampler.ShouldSample(params).Decision != sdktrace.NotRecord {
t.Error("Sampling decision should be NotRecord")
}
}

0 comments on commit 86da2f5

Please sign in to comment.