1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-25 22:41:46 +02:00
Files
opentelemetry-go/sdk/trace/sampling_test.go

249 lines
5.9 KiB
Go
Raw Normal View History

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package trace
import (
"context"
"fmt"
all: replace math/rand with math/rand/v2 (#6732) Update to new stdlib apis. The new Float64 is uniform, which resolves a long comment. https://cs.opensource.google/go/go/+/refs/tags/go1.24.2:src/math/rand/v2/rand.go;l=209 > // There are exactly 1<<53 float64s in [0,1). Use Intn(1<<53) / (1<<53). return float64(r.Uint64()<<11>>11) / (1 << 53) ``` goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/sdk/trace cpu: 12th Gen Intel(R) Core(TM) i7-1260P │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ TraceStart/with_a_simple_span-16 387.1n ± 5% 306.8n ± 15% -20.73% (p=0.000 n=10) TraceStart/with_several_links-16 542.2n ± 5% 501.0n ± 2% -7.61% (p=0.000 n=10) TraceStart/with_attributes-16 521.4n ± 14% 571.6n ± 6% +9.64% (p=0.009 n=10) geomean 478.3n 444.6n -7.05% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ TraceStart/with_a_simple_span-16 528.0 ± 0% 528.0 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_several_links-16 704.0 ± 0% 704.0 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_attributes-16 784.0 ± 0% 784.0 ± 0% ~ (p=1.000 n=10) ¹ geomean 663.0 663.0 +0.00% ¹ all samples are equal │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ TraceStart/with_a_simple_span-16 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_several_links-16 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_attributes-16 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ geomean 2.884 2.884 +0.00% ¹ all samples are equal ``` --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Damien Mathieu <42@dmathieu.com>
2025-05-15 08:21:49 +01:00
"math/rand/v2"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
)
func TestParentBasedDefaultLocalParentSampled(t *testing.T) {
sampler := ParentBased(AlwaysSample())
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
parentCtx := trace.ContextWithSpanContext(
context.Background(),
trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
}),
)
if sampler.ShouldSample(SamplingParameters{ParentContext: parentCtx}).Decision != RecordAndSample {
t.Error("Sampling decision should be RecordAndSample")
}
}
func TestParentBasedDefaultLocalParentNotSampled(t *testing.T) {
sampler := ParentBased(AlwaysSample())
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
parentCtx := trace.ContextWithSpanContext(
context.Background(),
trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
}),
)
if sampler.ShouldSample(SamplingParameters{ParentContext: parentCtx}).Decision != Drop {
t.Error("Sampling decision should be Drop")
}
}
func TestParentBasedWithNoParent(t *testing.T) {
params := SamplingParameters{}
sampler := ParentBased(AlwaysSample())
if sampler.ShouldSample(params).Decision != RecordAndSample {
t.Error("Sampling decision should be RecordAndSample")
}
sampler = ParentBased(NeverSample())
if sampler.ShouldSample(params).Decision != Drop {
t.Error("Sampling decision should be Drop")
}
}
func TestParentBasedWithSamplerOptions(t *testing.T) {
testCases := []struct {
name string
samplerOption ParentBasedSamplerOption
isParentRemote, isParentSampled bool
expectedDecision SamplingDecision
}{
{
"localParentSampled",
WithLocalParentSampled(NeverSample()),
false,
true,
Drop,
},
{
"localParentNotSampled",
WithLocalParentNotSampled(AlwaysSample()),
false,
false,
RecordAndSample,
},
{
"remoteParentSampled",
WithRemoteParentSampled(NeverSample()),
true,
true,
Drop,
},
{
"remoteParentNotSampled",
WithRemoteParentNotSampled(AlwaysSample()),
true,
false,
RecordAndSample,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
pscc := trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
Remote: tc.isParentRemote,
}
if tc.isParentSampled {
pscc.TraceFlags = trace.FlagsSampled
}
params := SamplingParameters{
ParentContext: trace.ContextWithSpanContext(
context.Background(),
trace.NewSpanContext(pscc),
),
}
sampler := ParentBased(
nil,
tc.samplerOption,
)
var wantStr, gotStr string
switch tc.expectedDecision {
case RecordAndSample:
wantStr = "RecordAndSample"
case Drop:
wantStr = "Drop"
default:
wantStr = "unknown"
}
actualDecision := sampler.ShouldSample(params).Decision
switch actualDecision {
case RecordAndSample:
gotStr = "RecordAndSample"
case Drop:
gotStr = "Drop"
default:
gotStr = "unknown"
}
assert.Equalf(t, tc.expectedDecision, actualDecision, "want %s, got %s", wantStr, gotStr)
})
}
}
func TestParentBasedDefaultDescription(t *testing.T) {
sampler := ParentBased(AlwaysSample())
expectedDescription := fmt.Sprintf("ParentBased{root:%s,remoteParentSampled:%s,"+
"remoteParentNotSampled:%s,localParentSampled:%s,localParentNotSampled:%s}",
AlwaysSample().Description(),
AlwaysSample().Description(),
NeverSample().Description(),
AlwaysSample().Description(),
NeverSample().Description())
if sampler.Description() != expectedDescription {
t.Errorf("Sampler description should be %s, got '%s' instead",
expectedDescription,
sampler.Description(),
)
}
}
// TraceIDRatioBased sampler requirements state
//
// "A TraceIDRatioBased sampler with a given sampling rate MUST also sample
// all traces that any TraceIDRatioBased sampler with a lower sampling rate
// would sample."
func TestTraceIdRatioSamplesInclusively(t *testing.T) {
const (
numSamplers = 1000
numTraces = 100
)
idg := defaultIDGenerator()
for range numSamplers {
ratioLo, ratioHi := rand.Float64(), rand.Float64()
if ratioHi < ratioLo {
ratioLo, ratioHi = ratioHi, ratioLo
}
samplerHi := TraceIDRatioBased(ratioHi)
samplerLo := TraceIDRatioBased(ratioLo)
for range numTraces {
traceID, _ := idg.NewIDs(context.Background())
params := SamplingParameters{TraceID: traceID}
if samplerLo.ShouldSample(params).Decision == RecordAndSample {
require.Equal(t, RecordAndSample, samplerHi.ShouldSample(params).Decision,
"%s sampled but %s did not", samplerLo.Description(), samplerHi.Description())
}
}
}
}
func TestTracestateIsPassed(t *testing.T) {
testCases := []struct {
name string
sampler Sampler
}{
{
"notSampled",
NeverSample(),
},
{
"sampled",
AlwaysSample(),
},
{
"parentSampled",
ParentBased(AlwaysSample()),
},
{
"parentNotSampled",
ParentBased(NeverSample()),
},
{
"traceIDRatioSampler",
TraceIDRatioBased(.5),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
traceState, err := trace.ParseTraceState("k=v")
if err != nil {
t.Error(err)
}
params := SamplingParameters{
ParentContext: trace.ContextWithSpanContext(
context.Background(),
trace.NewSpanContext(trace.SpanContextConfig{
TraceState: traceState,
}),
),
}
require.Equal(t, traceState, tc.sampler.ShouldSample(params).Tracestate, "TraceState is not equal")
})
}
}