Skip to content

Commit edb1328

Browse files
authored
Changes AlwaysParentSample to ParentSample(fallback) (#810)
* Changes AlwaysParentSample to ParentSample(fallback) To match https://github.com/open-telemetry/opentelemetry-specification/blob/v0.5.0/specification/trace/sdk.md#parentorelse introduced in open-telemetry/opentelemetry-specification#609 * Fix lint
1 parent 5baebce commit edb1328

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

sdk/trace/sampling.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,29 @@ func NeverSample() Sampler {
128128
return alwaysOffSampler{}
129129
}
130130

131-
// AlwaysParentSample returns a Sampler that samples a trace only
132-
// if the parent span is sampled.
133-
// This Sampler is a passthrough to the ProbabilitySampler with
134-
// a fraction of value 0.
135-
func AlwaysParentSample() Sampler {
136-
return &probabilitySampler{
137-
traceIDUpperBound: 0,
138-
description: "AlwaysParentSampler",
131+
// ParentSample returns a Sampler that samples a trace only
132+
// if the the span has a parent span and it is sampled. If the span has
133+
// parent span but it is not sampled, neither will this span. If the span
134+
// does not have a parent the fallback Sampler is used to determine if the
135+
// span should be sampled.
136+
func ParentSample(fallback Sampler) Sampler {
137+
return parentSampler{fallback}
138+
}
139+
140+
type parentSampler struct {
141+
fallback Sampler
142+
}
143+
144+
func (ps parentSampler) ShouldSample(p SamplingParameters) SamplingResult {
145+
if p.ParentContext.IsValid() {
146+
if p.ParentContext.IsSampled() {
147+
return SamplingResult{Decision: RecordAndSampled}
148+
}
149+
return SamplingResult{Decision: NotRecord}
139150
}
151+
return ps.fallback.ShouldSample(p)
152+
}
153+
154+
func (ps parentSampler) Description() string {
155+
return fmt.Sprintf("ParentOrElse{%s}", ps.fallback.Description())
140156
}

sdk/trace/sampling_test.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ import (
2323
)
2424

2525
func TestAlwaysParentSampleWithParentSampled(t *testing.T) {
26-
sampler := sdktrace.AlwaysParentSample()
26+
sampler := sdktrace.ParentSample(sdktrace.AlwaysSample())
27+
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
28+
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
29+
parentCtx := trace.SpanContext{
30+
TraceID: traceID,
31+
SpanID: spanID,
32+
TraceFlags: trace.FlagsSampled,
33+
}
34+
if sampler.ShouldSample(sdktrace.SamplingParameters{ParentContext: parentCtx}).Decision != sdktrace.RecordAndSampled {
35+
t.Error("Sampling decision should be RecordAndSampled")
36+
}
37+
}
38+
39+
func TestNeverParentSampleWithParentSampled(t *testing.T) {
40+
sampler := sdktrace.ParentSample(sdktrace.NeverSample())
2741
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
2842
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
2943
parentCtx := trace.SpanContext{
@@ -37,7 +51,7 @@ func TestAlwaysParentSampleWithParentSampled(t *testing.T) {
3751
}
3852

3953
func TestAlwaysParentSampleWithParentNotSampled(t *testing.T) {
40-
sampler := sdktrace.AlwaysParentSample()
54+
sampler := sdktrace.ParentSample(sdktrace.AlwaysSample())
4155
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
4256
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
4357
parentCtx := trace.SpanContext{
@@ -48,3 +62,17 @@ func TestAlwaysParentSampleWithParentNotSampled(t *testing.T) {
4862
t.Error("Sampling decision should be NotRecord")
4963
}
5064
}
65+
66+
func TestParentSampleWithNoParent(t *testing.T) {
67+
params := sdktrace.SamplingParameters{}
68+
69+
sampler := sdktrace.ParentSample(sdktrace.AlwaysSample())
70+
if sampler.ShouldSample(params).Decision != sdktrace.RecordAndSampled {
71+
t.Error("Sampling decision should be RecordAndSampled")
72+
}
73+
74+
sampler = sdktrace.ParentSample(sdktrace.NeverSample())
75+
if sampler.ShouldSample(params).Decision != sdktrace.NotRecord {
76+
t.Error("Sampling decision should be NotRecord")
77+
}
78+
}

0 commit comments

Comments
 (0)