1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-03-17 20:57:51 +02:00

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
https://github.com/open-telemetry/opentelemetry-specification/pull/609

* Fix lint
This commit is contained in:
Tyler Yahn 2020-06-11 08:27:21 -07:00 committed by GitHub
parent 4bf35c611b
commit 7d5117fafd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 11 deletions

View File

@ -128,13 +128,29 @@ 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}
}
return SamplingResult{Decision: NotRecord}
}
return ps.fallback.ShouldSample(p)
}
func (ps parentSampler) Description() string {
return fmt.Sprintf("ParentOrElse{%s}", ps.fallback.Description())
}

View File

@ -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{
@ -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{
@ -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")
}
}