| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package consistent |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 13 |
"go.opentelemetry.io/otel/trace" |
| 14 |
) |
| 15 |
|
| 16 |
func TestParentSamplerDescription(t *testing.T) { |
| 17 |
opts := []sdktrace.ParentBasedSamplerOption{ |
| 18 |
sdktrace.WithRemoteParentNotSampled(sdktrace.AlwaysSample()), |
| 19 |
} |
| 20 |
root := ProbabilityBased(1) |
| 21 |
compare := sdktrace.ParentBased(root, opts...) |
| 22 |
parent := ParentProbabilityBased(root, opts...) |
| 23 |
require.Equal(t, |
| 24 |
strings.Replace( |
| 25 |
compare.Description(), |
| 26 |
"ParentBased", |
| 27 |
"ParentProbabilityBased", |
| 28 |
1, |
| 29 |
), |
| 30 |
parent.Description(), |
| 31 |
) |
| 32 |
} |
| 33 |
|
| 34 |
func TestParentSamplerValidContext(t *testing.T) { |
| 35 |
parent := ParentProbabilityBased(sdktrace.NeverSample()) |
| 36 |
type testCase struct { |
| 37 |
in string |
| 38 |
sampled bool |
| 39 |
} |
| 40 |
for _, valid := range []testCase{ |
| 41 |
// sampled tests |
| 42 |
{"r:10", true}, |
| 43 |
{"r:10;a:b", true}, |
| 44 |
{"r:10;p:1", true}, |
| 45 |
{"r:10;p:10", true}, |
| 46 |
{"r:10;p:10;a:b", true}, |
| 47 |
{"r:10;p:63", true}, |
| 48 |
{"r:10;p:63;a:b", true}, |
| 49 |
{"p:0", true}, |
| 50 |
{"p:10;a:b", true}, |
| 51 |
{"p:63", true}, |
| 52 |
{"p:63;a:b", true}, |
| 53 |
|
| 54 |
// unsampled tests |
| 55 |
{"r:10", false}, |
| 56 |
{"r:10;a:b", false}, |
| 57 |
} { |
| 58 |
t.Run(testName(valid.in), func(t *testing.T) { |
| 59 |
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") |
| 60 |
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7") |
| 61 |
traceState, err := trace.TraceState{}.Insert(traceStateKey, valid.in) |
| 62 |
require.NoError(t, err) |
| 63 |
|
| 64 |
sccfg := trace.SpanContextConfig{ |
| 65 |
TraceID: traceID, |
| 66 |
SpanID: spanID, |
| 67 |
TraceState: traceState, |
| 68 |
} |
| 69 |
|
| 70 |
if valid.sampled { |
| 71 |
sccfg.TraceFlags = trace.FlagsSampled |
| 72 |
} |
| 73 |
|
| 74 |
parentCtx := trace.ContextWithSpanContext( |
| 75 |
t.Context(), |
| 76 |
trace.NewSpanContext(sccfg), |
| 77 |
) |
| 78 |
|
| 79 |
result := parent.ShouldSample( |
| 80 |
sdktrace.SamplingParameters{ |
| 81 |
ParentContext: parentCtx, |
| 82 |
TraceID: traceID, |
| 83 |
Name: "test", |
| 84 |
Kind: trace.SpanKindServer, |
| 85 |
}, |
| 86 |
) |
| 87 |
|
| 88 |
if valid.sampled { |
| 89 |
require.Equal(t, sdktrace.RecordAndSample, result.Decision) |
| 90 |
} else { |
| 91 |
require.Equal(t, sdktrace.Drop, result.Decision) |
| 92 |
} |
| 93 |
require.Equal(t, []attribute.KeyValue(nil), result.Attributes) |
| 94 |
require.Equal(t, valid.in, result.Tracestate.Get(traceStateKey)) |
| 95 |
}) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func TestParentSamplerInvalidContext(t *testing.T) { |
| 100 |
parent := ParentProbabilityBased(sdktrace.NeverSample()) |
| 101 |
type testCase struct { |
| 102 |
in string |
| 103 |
sampled bool |
| 104 |
expect string |
| 105 |
} |
| 106 |
for _, invalid := range []testCase{ |
| 107 |
// sampled |
| 108 |
{"r:100", true, ""}, |
| 109 |
{"r:100;p:1", true, ""}, |
| 110 |
{"r:100;p:1;a:b", true, "a:b"}, |
| 111 |
{"r:10;p:100", true, "r:10"}, |
| 112 |
{"r:10;p:100;a:b", true, "r:10;a:b"}, |
| 113 |
|
| 114 |
// unsampled |
| 115 |
{"r:63;p:1", false, ""}, |
| 116 |
{"r:10;p:1", false, "r:10"}, |
| 117 |
{"r:10;p:1;a:b", false, "r:10;a:b"}, |
| 118 |
} { |
| 119 |
testInvalid := func(t *testing.T, isChildContext bool) { |
| 120 |
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736") |
| 121 |
traceState, err := trace.TraceState{}.Insert(traceStateKey, invalid.in) |
| 122 |
require.NoError(t, err) |
| 123 |
|
| 124 |
sccfg := trace.SpanContextConfig{ |
| 125 |
TraceState: traceState, |
| 126 |
} |
| 127 |
if isChildContext { |
| 128 |
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7") |
| 129 |
|
| 130 |
sccfg.TraceID = traceID |
| 131 |
sccfg.SpanID = spanID |
| 132 |
// Note: the other branch is testing a fabricated |
| 133 |
// situation where the context has a tracestate and |
| 134 |
// no TraceID. |
| 135 |
} |
| 136 |
if invalid.sampled { |
| 137 |
sccfg.TraceFlags = trace.FlagsSampled |
| 138 |
} |
| 139 |
|
| 140 |
parentCtx := trace.ContextWithSpanContext( |
| 141 |
t.Context(), |
| 142 |
trace.NewSpanContext(sccfg), |
| 143 |
) |
| 144 |
|
| 145 |
result := parent.ShouldSample( |
| 146 |
sdktrace.SamplingParameters{ |
| 147 |
ParentContext: parentCtx, |
| 148 |
TraceID: sccfg.TraceID, |
| 149 |
Name: "test", |
| 150 |
Kind: trace.SpanKindServer, |
| 151 |
}, |
| 152 |
) |
| 153 |
|
| 154 |
if isChildContext && invalid.sampled { |
| 155 |
require.Equal(t, sdktrace.RecordAndSample, result.Decision) |
| 156 |
} else { |
| 157 |
// if we're not a child context, ShouldSample |
| 158 |
// falls through to the delegate, which is NeverSample. |
| 159 |
require.Equal(t, sdktrace.Drop, result.Decision) |
| 160 |
} |
| 161 |
require.Equal(t, []attribute.KeyValue(nil), result.Attributes) |
| 162 |
require.Equal(t, invalid.expect, result.Tracestate.Get(traceStateKey)) |
| 163 |
} |
| 164 |
|
| 165 |
t.Run(testName(invalid.in)+"_with_parent", func(t *testing.T) { |
| 166 |
testInvalid(t, true) |
| 167 |
}) |
| 168 |
t.Run(testName(invalid.in)+"_no_parent", func(t *testing.T) { |
| 169 |
testInvalid(t, false) |
| 170 |
}) |
| 171 |
} |
| 172 |
} |