1+ from dataclasses import dataclass
12from typing import Optional
23
34import pytest
2930SPAN_ID = int ("0123456789abcdef" , 16 )
3031
3132
33+ @dataclass
34+ class Input :
35+ sampler : Sampler
36+ sampled : bool
37+ threshold : Optional [int ]
38+ random_value : Optional [int ]
39+
40+
41+ @dataclass
42+ class Output :
43+ sampled : bool
44+ threshold : int
45+ random_value : int
46+
47+
3248@pytest .mark .parametrize (
33- "sampler,parent_sampled,parent_threshold,parent_random_value,sampled,threshold,random_value " ,
49+ "input,output " ,
3450 (
3551 p (
36- consistent_always_on (),
37- True ,
38- None ,
39- None ,
40- True ,
41- 0 ,
42- INVALID_RANDOM_VALUE ,
52+ Input (
53+ sampler = consistent_always_on (),
54+ sampled = True ,
55+ threshold = None ,
56+ random_value = None ,
57+ ),
58+ Output (
59+ sampled = True , threshold = 0 , random_value = INVALID_RANDOM_VALUE
60+ ),
4361 id = "min threshold no parent random value" ,
4462 ),
4563 p (
46- consistent_always_on (),
47- True ,
48- None ,
49- 0x7F99AA40C02744 ,
50- True ,
51- 0 ,
52- 0x7F99AA40C02744 ,
64+ Input (
65+ sampler = consistent_always_on () ,
66+ sampled = True ,
67+ threshold = None ,
68+ random_value = 0x7F99AA40C02744 ,
69+ ) ,
70+ Output ( sampled = True , threshold = 0 , random_value = 0x7F99AA40C02744 ) ,
5371 id = "min threshold with parent random value" ,
5472 ),
5573 p (
56- consistent_always_off (),
57- True ,
58- None ,
59- None ,
60- False ,
61- INVALID_THRESHOLD ,
62- INVALID_RANDOM_VALUE ,
74+ Input (
75+ sampler = consistent_always_off (),
76+ sampled = True ,
77+ threshold = None ,
78+ random_value = None ,
79+ ),
80+ Output (
81+ sampled = False ,
82+ threshold = INVALID_THRESHOLD ,
83+ random_value = INVALID_RANDOM_VALUE ,
84+ ),
6385 id = "max threshold" ,
6486 ),
6587 p (
66- consistent_parent_based (consistent_always_on ()),
67- False , # should be ignored
68- 0x7F99AA40C02744 ,
69- 0x7F99AA40C02744 ,
70- True ,
71- 0x7F99AA40C02744 ,
72- 0x7F99AA40C02744 ,
88+ Input (
89+ sampler = consistent_parent_based (consistent_always_on ()),
90+ sampled = False ,
91+ threshold = 0x7F99AA40C02744 ,
92+ random_value = 0x7F99AA40C02744 ,
93+ ),
94+ Output (
95+ sampled = True ,
96+ threshold = 0x7F99AA40C02744 ,
97+ random_value = 0x7F99AA40C02744 ,
98+ ),
7399 id = "parent based in consistent mode" ,
74100 ),
75101 p (
76- consistent_parent_based (consistent_always_on ()),
77- True ,
78- None ,
79- None ,
80- True ,
81- INVALID_THRESHOLD ,
82- INVALID_RANDOM_VALUE ,
102+ Input (
103+ sampler = consistent_parent_based (consistent_always_on ()),
104+ sampled = True ,
105+ threshold = None ,
106+ random_value = None ,
107+ ),
108+ Output (
109+ sampled = True ,
110+ threshold = INVALID_THRESHOLD ,
111+ random_value = INVALID_RANDOM_VALUE ,
112+ ),
83113 id = "parent based in legacy mode" ,
84114 ),
85115 p (
86- consistent_probability_based (0.5 ),
87- True ,
88- None ,
89- 0x7FFFFFFFFFFFFF ,
90- False ,
91- INVALID_THRESHOLD ,
92- 0x7FFFFFFFFFFFFF ,
116+ Input (
117+ sampler = consistent_probability_based (0.5 ),
118+ sampled = True ,
119+ threshold = None ,
120+ random_value = 0x7FFFFFFFFFFFFF ,
121+ ),
122+ Output (
123+ sampled = False ,
124+ threshold = INVALID_THRESHOLD ,
125+ random_value = 0x7FFFFFFFFFFFFF ,
126+ ),
93127 id = "half threshold not sampled" ,
94128 ),
95129 p (
96- consistent_probability_based (0.5 ),
97- False ,
98- None ,
99- 0x80000000000000 ,
100- True ,
101- 0x80000000000000 ,
102- 0x80000000000000 ,
130+ Input (
131+ sampler = consistent_probability_based (0.5 ),
132+ sampled = False ,
133+ threshold = None ,
134+ random_value = 0x80000000000000 ,
135+ ),
136+ Output (
137+ sampled = True ,
138+ threshold = 0x80000000000000 ,
139+ random_value = 0x80000000000000 ,
140+ ),
103141 id = "half threshold sampled" ,
104142 ),
105143 p (
106- consistent_probability_based ( 1.0 ),
107- False ,
108- 0x80000000000000 ,
109- 0x80000000000000 ,
110- True ,
111- 0 ,
112- 0x80000000000000 ,
144+ Input (
145+ sampler = consistent_probability_based ( 1.0 ) ,
146+ sampled = False ,
147+ threshold = 0x80000000000000 ,
148+ random_value = 0x80000000000000 ,
149+ ) ,
150+ Output ( sampled = True , threshold = 0 , random_value = 0x80000000000000 ) ,
113151 id = "half threshold sampled" ,
114152 ),
115153 ),
116154)
117- def test_sample (
118- sampler : Sampler ,
119- parent_sampled : bool ,
120- parent_threshold : Optional [int ],
121- parent_random_value : Optional [int ],
122- sampled : bool ,
123- threshold : float ,
124- random_value : float ,
125- ):
155+ def test_sample (input : Input , output : Output ):
126156 parent_state = OtelTraceState .invalid ()
127- if parent_threshold is not None :
128- parent_state .threshold = parent_threshold
129- if parent_random_value is not None :
130- parent_state .random_value = parent_random_value
157+ if input . threshold is not None :
158+ parent_state .threshold = input . threshold
159+ if input . random_value is not None :
160+ parent_state .random_value = input . random_value
131161 parent_state_str = parent_state .serialize ()
132162 parent_trace_state = (
133163 TraceState ((("ot" , parent_state_str ),)) if parent_state_str else None
134164 )
135165 flags = (
136166 TraceFlags (TraceFlags .SAMPLED )
137- if parent_sampled
167+ if input . sampled
138168 else TraceFlags .get_default ()
139169 )
140170 parent_span_context = SpanContext (
@@ -143,13 +173,13 @@ def test_sample(
143173 parent_span = NonRecordingSpan (parent_span_context )
144174 parent_context = set_span_in_context (parent_span )
145175
146- result = sampler .should_sample (
176+ result = input . sampler .should_sample (
147177 parent_context , TRACE_ID , "name" , trace_state = parent_trace_state
148178 )
149179
150- decision = Decision .RECORD_AND_SAMPLE if sampled else Decision .DROP
180+ decision = Decision .RECORD_AND_SAMPLE if output . sampled else Decision .DROP
151181 state = OtelTraceState .parse (result .trace_state )
152182
153183 assert result .decision == decision
154- assert state .threshold == threshold
155- assert state .random_value == random_value
184+ assert state .threshold == output . threshold
185+ assert state .random_value == output . random_value
0 commit comments