From 4dc403c05751f5f9a8829c18e08c4e9a91a3fa2c Mon Sep 17 00:00:00 2001 From: "Amr Hanafi (MAHDI))" Date: Thu, 30 Jan 2020 13:55:39 -0800 Subject: [PATCH 1/3] Add AlwaysParentSample --- sdk/trace/sampling.go | 7 ++++++ sdk/trace/sampling_test.go | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 sdk/trace/sampling_test.go diff --git a/sdk/trace/sampling.go b/sdk/trace/sampling.go index 736752367da..f33810c97a3 100644 --- a/sdk/trace/sampling.go +++ b/sdk/trace/sampling.go @@ -77,3 +77,10 @@ func NeverSample() Sampler { return SamplingDecision{Sample: false} } } + +// AlwaysParentSample returns a Sampler that samples only if the parent span is sampled. +func AlwaysParentSample() Sampler { + return func(p SamplingParameters) SamplingDecision { + return SamplingDecision{Sample: p.ParentContext.IsSampled()} + } +} diff --git a/sdk/trace/sampling_test.go b/sdk/trace/sampling_test.go new file mode 100644 index 00000000000..7c86ed0b716 --- /dev/null +++ b/sdk/trace/sampling_test.go @@ -0,0 +1,49 @@ +// Copyright 2019, OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package trace_test + +import ( + "testing" + + "go.opentelemetry.io/otel/api/core" + sdktrace "go.opentelemetry.io/otel/sdk/trace" +) + +func TestAlwaysParentSampleWithParentSampled(t *testing.T) { + sampler := sdktrace.AlwaysParentSample() + traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") + spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7") + parentCtx := core.SpanContext{ + TraceID: traceID, + SpanID: spanID, + TraceFlags: core.TraceFlagsSampled, + } + if !sampler(sdktrace.SamplingParameters{ParentContext: parentCtx}).Sample { + t.Error("Sampling decision should be true") + } +} + +func TestAlwaysParentSampleWithParentNotSampled(t *testing.T) { + sampler := sdktrace.AlwaysParentSample() + traceID, _ := core.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") + spanID, _ := core.SpanIDFromHex("00f067aa0ba902b7") + parentCtx := core.SpanContext{ + TraceID: traceID, + SpanID: spanID, + } + if sampler(sdktrace.SamplingParameters{ParentContext: parentCtx}).Sample { + t.Error("Sampling decision should be false") + } +} From cd50ca1095cd9f9ea14dc0068a6a806f03d13c58 Mon Sep 17 00:00:00 2001 From: Amr Hanafi Date: Fri, 31 Jan 2020 09:45:57 -0800 Subject: [PATCH 2/3] Update sampler doc Co-Authored-By: Krzesimir Nowak --- sdk/trace/sampling.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/trace/sampling.go b/sdk/trace/sampling.go index f33810c97a3..03a73d88ee3 100644 --- a/sdk/trace/sampling.go +++ b/sdk/trace/sampling.go @@ -78,7 +78,8 @@ func NeverSample() Sampler { } } -// AlwaysParentSample returns a Sampler that samples only if the parent span is sampled. +// AlwaysParentSample returns a Sampler that samples a trace only +// if the parent span is sampled. func AlwaysParentSample() Sampler { return func(p SamplingParameters) SamplingDecision { return SamplingDecision{Sample: p.ParentContext.IsSampled()} From 943a9a68de7522c074653a1684e2542945bc97ce Mon Sep 17 00:00:00 2001 From: "Amr Hanafi (MAHDI))" Date: Fri, 31 Jan 2020 23:51:50 -0800 Subject: [PATCH 3/3] Address PR comments --- sdk/trace/sampling.go | 6 +++--- sdk/trace/sampling_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/trace/sampling.go b/sdk/trace/sampling.go index 03a73d88ee3..4f91d4d95c6 100644 --- a/sdk/trace/sampling.go +++ b/sdk/trace/sampling.go @@ -80,8 +80,8 @@ func NeverSample() Sampler { // 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 func(p SamplingParameters) SamplingDecision { - return SamplingDecision{Sample: p.ParentContext.IsSampled()} - } + return ProbabilitySampler(0) } diff --git a/sdk/trace/sampling_test.go b/sdk/trace/sampling_test.go index 7c86ed0b716..488bfe946ef 100644 --- a/sdk/trace/sampling_test.go +++ b/sdk/trace/sampling_test.go @@ -1,4 +1,4 @@ -// Copyright 2019, OpenTelemetry Authors +// Copyright 2020, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.