2020-03-25 23:47:17 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2020-02-03 20:22:52 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-09-03 16:28:01 +02:00
|
|
|
package trace
|
2020-02-03 20:22:52 +02:00
|
|
|
|
|
|
|
import (
|
2020-12-10 03:30:32 +02:00
|
|
|
"context"
|
2020-09-10 21:39:04 +02:00
|
|
|
"fmt"
|
2020-09-03 16:28:01 +02:00
|
|
|
"math/rand"
|
2020-02-03 20:22:52 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-09-03 16:28:01 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-02 14:17:11 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-02-03 20:22:52 +02:00
|
|
|
)
|
|
|
|
|
2020-09-10 21:39:04 +02:00
|
|
|
func TestParentBasedDefaultLocalParentSampled(t *testing.T) {
|
|
|
|
sampler := ParentBased(AlwaysSample())
|
2020-11-07 00:13:31 +02:00
|
|
|
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
|
|
|
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
|
2021-03-09 18:17:29 +02:00
|
|
|
parentCtx := trace.NewSpanContext(trace.SpanContextConfig{
|
2020-06-11 17:27:21 +02:00
|
|
|
TraceID: traceID,
|
|
|
|
SpanID: spanID,
|
2020-11-07 00:13:31 +02:00
|
|
|
TraceFlags: trace.FlagsSampled,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2020-09-22 19:34:43 +02:00
|
|
|
if sampler.ShouldSample(SamplingParameters{ParentContext: parentCtx}).Decision != RecordAndSample {
|
|
|
|
t.Error("Sampling decision should be RecordAndSample")
|
2020-06-11 17:27:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 21:39:04 +02:00
|
|
|
func TestParentBasedDefaultLocalParentNotSampled(t *testing.T) {
|
|
|
|
sampler := ParentBased(AlwaysSample())
|
2020-11-07 00:13:31 +02:00
|
|
|
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
|
|
|
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
|
2021-03-09 18:17:29 +02:00
|
|
|
parentCtx := trace.NewSpanContext(trace.SpanContextConfig{
|
2020-02-03 20:22:52 +02:00
|
|
|
TraceID: traceID,
|
|
|
|
SpanID: spanID,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2020-09-22 19:34:43 +02:00
|
|
|
if sampler.ShouldSample(SamplingParameters{ParentContext: parentCtx}).Decision != Drop {
|
|
|
|
t.Error("Sampling decision should be Drop")
|
2020-02-03 20:22:52 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-11 17:27:21 +02:00
|
|
|
|
2020-09-10 21:39:04 +02:00
|
|
|
func TestParentBasedWithNoParent(t *testing.T) {
|
2020-09-03 16:28:01 +02:00
|
|
|
params := SamplingParameters{}
|
2020-06-11 17:27:21 +02:00
|
|
|
|
2020-09-10 21:39:04 +02:00
|
|
|
sampler := ParentBased(AlwaysSample())
|
2020-09-22 19:34:43 +02:00
|
|
|
if sampler.ShouldSample(params).Decision != RecordAndSample {
|
|
|
|
t.Error("Sampling decision should be RecordAndSample")
|
2020-06-11 17:27:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 21:39:04 +02:00
|
|
|
sampler = ParentBased(NeverSample())
|
2020-09-22 19:34:43 +02:00
|
|
|
if sampler.ShouldSample(params).Decision != Drop {
|
|
|
|
t.Error("Sampling decision should be Drop")
|
2020-06-11 17:27:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-03 16:28:01 +02:00
|
|
|
|
2020-09-10 21:39:04 +02:00
|
|
|
func TestParentBasedWithSamplerOptions(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
samplerOption ParentBasedSamplerOption
|
|
|
|
isParentRemote, isParentSampled bool
|
|
|
|
expectedDecision SamplingDecision
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"localParentSampled",
|
|
|
|
WithLocalParentSampled(NeverSample()),
|
|
|
|
false,
|
|
|
|
true,
|
2020-09-22 19:34:43 +02:00
|
|
|
Drop,
|
2020-09-10 21:39:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"localParentNotSampled",
|
|
|
|
WithLocalParentNotSampled(AlwaysSample()),
|
|
|
|
false,
|
|
|
|
false,
|
2020-09-22 19:34:43 +02:00
|
|
|
RecordAndSample,
|
2020-09-10 21:39:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"remoteParentSampled",
|
|
|
|
WithRemoteParentSampled(NeverSample()),
|
|
|
|
true,
|
|
|
|
true,
|
2020-09-22 19:34:43 +02:00
|
|
|
Drop,
|
2020-09-10 21:39:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"remoteParentNotSampled",
|
|
|
|
WithRemoteParentNotSampled(AlwaysSample()),
|
|
|
|
true,
|
|
|
|
false,
|
2020-09-22 19:34:43 +02:00
|
|
|
RecordAndSample,
|
2020-09-10 21:39:04 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-11-07 00:13:31 +02:00
|
|
|
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
|
|
|
|
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
|
2021-03-09 18:17:29 +02:00
|
|
|
parentCtx := trace.NewSpanContext(trace.SpanContextConfig{
|
2020-09-10 21:39:04 +02:00
|
|
|
TraceID: traceID,
|
|
|
|
SpanID: spanID,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2020-09-10 21:39:04 +02:00
|
|
|
|
|
|
|
if tc.isParentSampled {
|
2021-03-09 18:17:29 +02:00
|
|
|
parentCtx = parentCtx.WithTraceFlags(trace.FlagsSampled)
|
2020-09-10 21:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
params := SamplingParameters{ParentContext: parentCtx}
|
|
|
|
if tc.isParentRemote {
|
|
|
|
params.HasRemoteParent = true
|
|
|
|
}
|
|
|
|
|
|
|
|
sampler := ParentBased(
|
|
|
|
nil,
|
|
|
|
tc.samplerOption,
|
|
|
|
)
|
|
|
|
|
|
|
|
switch tc.expectedDecision {
|
2020-09-22 19:34:43 +02:00
|
|
|
case RecordAndSample:
|
2020-09-10 21:39:04 +02:00
|
|
|
if sampler.ShouldSample(params).Decision != tc.expectedDecision {
|
2020-09-22 19:34:43 +02:00
|
|
|
t.Error("Sampling decision should be RecordAndSample")
|
2020-09-10 21:39:04 +02:00
|
|
|
}
|
2020-09-22 19:34:43 +02:00
|
|
|
case Drop:
|
2020-09-10 21:39:04 +02:00
|
|
|
if sampler.ShouldSample(params).Decision != tc.expectedDecision {
|
2020-09-22 19:34:43 +02:00
|
|
|
t.Error("Sampling decision should be Drop")
|
2020-09-10 21:39:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.Error(fmt.Sprintf("Sampler description should be %s, got '%s' instead",
|
|
|
|
expectedDescription,
|
|
|
|
sampler.Description(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:28:01 +02:00
|
|
|
// 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
|
|
|
|
)
|
2020-12-10 03:30:32 +02:00
|
|
|
idg := defaultIDGenerator()
|
2020-09-03 16:28:01 +02:00
|
|
|
|
|
|
|
for i := 0; i < numSamplers; i++ {
|
|
|
|
ratioLo, ratioHi := rand.Float64(), rand.Float64()
|
|
|
|
if ratioHi < ratioLo {
|
|
|
|
ratioLo, ratioHi = ratioHi, ratioLo
|
|
|
|
}
|
|
|
|
samplerHi := TraceIDRatioBased(ratioHi)
|
|
|
|
samplerLo := TraceIDRatioBased(ratioLo)
|
|
|
|
for j := 0; j < numTraces; j++ {
|
2020-12-10 03:30:32 +02:00
|
|
|
traceID, _ := idg.NewIDs(context.Background())
|
2020-09-03 16:28:01 +02:00
|
|
|
|
|
|
|
params := SamplingParameters{TraceID: traceID}
|
2020-09-22 19:34:43 +02:00
|
|
|
if samplerLo.ShouldSample(params).Decision == RecordAndSample {
|
|
|
|
require.Equal(t, RecordAndSample, samplerHi.ShouldSample(params).Decision,
|
2020-09-03 16:28:01 +02:00
|
|
|
"%s sampled but %s did not", samplerLo.Description(), samplerHi.Description())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-06 21:09:48 +02:00
|
|
|
|
|
|
|
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) {
|
2021-02-18 19:59:37 +02:00
|
|
|
traceState, err := trace.TraceStateFromKeyValues(attribute.String("k", "v"))
|
2021-01-06 21:09:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-03-09 18:17:29 +02:00
|
|
|
parentCtx := trace.NewSpanContext(trace.SpanContextConfig{
|
2021-01-06 21:09:48 +02:00
|
|
|
TraceState: traceState,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2021-01-06 21:09:48 +02:00
|
|
|
params := SamplingParameters{ParentContext: parentCtx}
|
|
|
|
|
|
|
|
require.Equal(t, traceState, tc.sampler.ShouldSample(params).Tracestate, "TraceState is not equal")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|