| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package consistent |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"math/rand" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"go.opentelemetry.io/otel" |
| 15 |
"go.opentelemetry.io/otel/attribute" |
| 16 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 17 |
"go.opentelemetry.io/otel/trace" |
| 18 |
) |
| 19 |
|
| 20 |
type ( |
| 21 |
testDegrees int |
| 22 |
pValue int |
| 23 |
|
| 24 |
testErrorHandler struct { |
| 25 |
lock sync.Mutex |
| 26 |
errors []error |
| 27 |
} |
| 28 |
) |
| 29 |
|
| 30 |
func parsePR(s string) (p, r string) { |
| 31 |
for kvf := range strings.SplitSeq(s, ";") { |
| 32 |
kv := strings.SplitN(kvf, ":", 2) |
| 33 |
switch kv[0] { |
| 34 |
case "p": |
| 35 |
p = kv[1] |
| 36 |
case "r": |
| 37 |
r = kv[1] |
| 38 |
} |
| 39 |
} |
| 40 |
return p, r |
| 41 |
} |
| 42 |
|
| 43 |
func (eh *testErrorHandler) Handle(err error) { |
| 44 |
eh.lock.Lock() |
| 45 |
defer eh.lock.Unlock() |
| 46 |
eh.errors = append(eh.errors, err) |
| 47 |
} |
| 48 |
|
| 49 |
func (eh *testErrorHandler) Errors() []error { |
| 50 |
eh.lock.Lock() |
| 51 |
defer eh.lock.Unlock() |
| 52 |
return eh.errors |
| 53 |
} |
| 54 |
|
| 55 |
func TestSamplerDescription(t *testing.T) { |
| 56 |
const minProb = 0x1p-62 // 2.168404344971009e-19 |
| 57 |
|
| 58 |
for _, tc := range []struct { |
| 59 |
prob float64 |
| 60 |
expect string |
| 61 |
}{ |
| 62 |
{1, "ProbabilityBased{1}"}, |
| 63 |
{0, "ProbabilityBased{0}"}, |
| 64 |
{0.75, "ProbabilityBased{0.75}"}, |
| 65 |
{0.05, "ProbabilityBased{0.05}"}, |
| 66 |
{0.003, "ProbabilityBased{0.003}"}, |
| 67 |
{0.99999999, "ProbabilityBased{0.99999999}"}, |
| 68 |
{0.00000001, "ProbabilityBased{1e-08}"}, |
| 69 |
{minProb, "ProbabilityBased{2.168404344971009e-19}"}, |
| 70 |
{minProb * 1.5, "ProbabilityBased{3.2526065174565133e-19}"}, |
| 71 |
{3e-19, "ProbabilityBased{3e-19}"}, |
| 72 |
|
| 73 |
// out-of-range > 1 |
| 74 |
{1.01, "ProbabilityBased{1}"}, |
| 75 |
{101.1, "ProbabilityBased{1}"}, |
| 76 |
|
| 77 |
// out-of-range < 2^-62 |
| 78 |
{-1, "ProbabilityBased{0}"}, |
| 79 |
{-0.001, "ProbabilityBased{0}"}, |
| 80 |
{minProb * 0.999, "ProbabilityBased{0}"}, |
| 81 |
} { |
| 82 |
s := ProbabilityBased(tc.prob) |
| 83 |
require.Equal(t, tc.expect, s.Description(), "%#v", tc.prob) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func getUnknowns(otts otelTraceState) string { |
| 88 |
otts.pvalue = invalidValue |
| 89 |
otts.rvalue = invalidValue |
| 90 |
return otts.serialize() |
| 91 |
} |
| 92 |
|
| 93 |
func TestSamplerBehavior(t *testing.T) { |
| 94 |
type testGroup struct { |
| 95 |
probability float64 |
| 96 |
minP uint8 |
| 97 |
maxP uint8 |
| 98 |
} |
| 99 |
type testCase struct { |
| 100 |
isRoot bool |
| 101 |
parentSampled bool |
| 102 |
ctxTracestate string |
| 103 |
hasErrors bool |
| 104 |
} |
| 105 |
|
| 106 |
for _, group := range []testGroup{ |
| 107 |
{1.0, 0, 0}, |
| 108 |
{0.75, 0, 1}, |
| 109 |
{0.5, 1, 1}, |
| 110 |
{0, 63, 63}, |
| 111 |
} { |
| 112 |
t.Run(fmt.Sprint(group.probability), func(t *testing.T) { |
| 113 |
for _, test := range []testCase{ |
| 114 |
// roots do not care if the context is |
| 115 |
// sampled, however preserve other |
| 116 |
// otel tracestate keys |
| 117 |
{true, false, "", false}, |
| 118 |
{true, false, "a:b", false}, |
| 119 |
|
| 120 |
// non-roots insert r |
| 121 |
{false, true, "", false}, |
| 122 |
{false, true, "a:b", false}, |
| 123 |
{false, false, "", false}, |
| 124 |
{false, false, "a:b", false}, |
| 125 |
|
| 126 |
// error cases: r-p inconsistency |
| 127 |
{false, true, "r:10;p:20", true}, |
| 128 |
{false, true, "r:10;p:20;a:b", true}, |
| 129 |
{false, false, "r:10;p:5", true}, |
| 130 |
{false, false, "r:10;p:5;a:b", true}, |
| 131 |
|
| 132 |
// error cases: out-of-range |
| 133 |
{false, false, "r:100", true}, |
| 134 |
{false, false, "r:100;a:b", true}, |
| 135 |
{false, true, "r:100;p:100", true}, |
| 136 |
{false, true, "r:100;p:100;a:b", true}, |
| 137 |
{false, true, "r:10;p:100", true}, |
| 138 |
{false, true, "r:10;p:100;a:b", true}, |
| 139 |
} { |
| 140 |
t.Run(testName(test.ctxTracestate), func(t *testing.T) { |
| 141 |
handler := &testErrorHandler{} |
| 142 |
otel.SetErrorHandler(handler) |
| 143 |
|
| 144 |
src := rand.NewSource(99999199999) |
| 145 |
sampler := ProbabilityBased(group.probability, WithRandomSource(src)) |
| 146 |
|
| 147 |
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") |
| 148 |
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7") |
| 149 |
|
| 150 |
traceState := trace.TraceState{} |
| 151 |
if test.ctxTracestate != "" { |
| 152 |
var err error |
| 153 |
traceState, err = traceState.Insert(traceStateKey, test.ctxTracestate) |
| 154 |
require.NoError(t, err) |
| 155 |
} |
| 156 |
|
| 157 |
sccfg := trace.SpanContextConfig{ |
| 158 |
TraceState: traceState, |
| 159 |
} |
| 160 |
|
| 161 |
if !test.isRoot { |
| 162 |
sccfg.TraceID = traceID |
| 163 |
sccfg.SpanID = spanID |
| 164 |
} |
| 165 |
|
| 166 |
if test.parentSampled { |
| 167 |
sccfg.TraceFlags = trace.FlagsSampled |
| 168 |
} |
| 169 |
|
| 170 |
parentCtx := trace.ContextWithSpanContext( |
| 171 |
t.Context(), |
| 172 |
trace.NewSpanContext(sccfg), |
| 173 |
) |
| 174 |
|
| 175 |
// Note: the error below is sometimes expected |
| 176 |
testState, _ := parseOTelTraceState(test.ctxTracestate, test.parentSampled) |
| 177 |
hasRValue := testState.hasRValue() |
| 178 |
|
| 179 |
const repeats = 10 |
| 180 |
for range repeats { |
| 181 |
result := sampler.ShouldSample( |
| 182 |
sdktrace.SamplingParameters{ |
| 183 |
ParentContext: parentCtx, |
| 184 |
TraceID: traceID, |
| 185 |
Name: "test", |
| 186 |
Kind: trace.SpanKindServer, |
| 187 |
}, |
| 188 |
) |
| 189 |
sampled := result.Decision == sdktrace.RecordAndSample |
| 190 |
|
| 191 |
// The result is deterministically random. Parse the tracestate |
| 192 |
// to see that it is consistent. |
| 193 |
otts, err := parseOTelTraceState(result.Tracestate.Get(traceStateKey), sampled) |
| 194 |
require.NoError(t, err) |
| 195 |
require.True(t, otts.hasRValue()) |
| 196 |
require.Equal(t, []attribute.KeyValue(nil), result.Attributes) |
| 197 |
|
| 198 |
if otts.hasPValue() { |
| 199 |
require.LessOrEqual(t, group.minP, otts.pvalue) |
| 200 |
require.LessOrEqual(t, otts.pvalue, group.maxP) |
| 201 |
require.Equal(t, sdktrace.RecordAndSample, result.Decision) |
| 202 |
} else { |
| 203 |
require.Equal(t, sdktrace.Drop, result.Decision) |
| 204 |
} |
| 205 |
|
| 206 |
require.Equal(t, getUnknowns(testState), getUnknowns(otts)) |
| 207 |
|
| 208 |
if hasRValue { |
| 209 |
require.Equal(t, testState.rvalue, otts.rvalue) |
| 210 |
} |
| 211 |
|
| 212 |
if test.hasErrors { |
| 213 |
require.NotEmpty(t, handler.Errors()) |
| 214 |
} else { |
| 215 |
require.Empty(t, handler.Errors()) |
| 216 |
} |
| 217 |
} |
| 218 |
}) |
| 219 |
} |
| 220 |
}) |
| 221 |
} |
| 222 |
} |