Skip to content

Commit 3af3709

Browse files
committed
Fix lint
1 parent da6d90e commit 3af3709

File tree

5 files changed

+136
-101
lines changed

5 files changed

+136
-101
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/trace/_sampling_experimental/_composable.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def sampling_intent(
3131
trace_state: Optional[TraceState],
3232
) -> SamplingIntent:
3333
"""Returns information to make a consistent sampling decision."""
34-
...
3534

3635
def get_description(self) -> str:
3736
"""Returns a description of the sampler."""
38-
...

opentelemetry-sdk/src/opentelemetry/sdk/trace/_sampling_experimental/_fixed_threshold.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def consistent_probability_based(
4545
sampling_probability: float,
4646
) -> ConsistentSampler:
4747
"""Returns a consistent sampler that samples each span with a fixed probability."""
48-
if not (0.0 <= sampling_probability <= 1.0):
48+
if not 0.0 <= sampling_probability <= 1.0:
4949
raise ValueError("Sampling probability must be between 0.0 and 1.0")
5050

5151
return ConsistentSampler(

opentelemetry-sdk/src/opentelemetry/sdk/trace/_sampling_experimental/_parent_based.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def sampling_intent(
4343
threshold=ot_trace_state.threshold,
4444
adjusted_count_reliable=True,
4545
)
46-
else:
47-
threshold = (
48-
MIN_THRESHOLD
49-
if parent_span_ctx.trace_flags.sampled
50-
else INVALID_THRESHOLD
51-
)
52-
return SamplingIntent(
53-
threshold=threshold, adjusted_count_reliable=False
54-
)
46+
47+
threshold = (
48+
MIN_THRESHOLD
49+
if parent_span_ctx.trace_flags.sampled
50+
else INVALID_THRESHOLD
51+
)
52+
return SamplingIntent(
53+
threshold=threshold, adjusted_count_reliable=False
54+
)
5555

5656
def get_description(self) -> str:
5757
return self._description

opentelemetry-sdk/src/opentelemetry/sdk/trace/_sampling_experimental/_sampler.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,11 @@ def should_sample(
5151
else:
5252
ot_trace_state.threshold = INVALID_THRESHOLD
5353

54-
otts = ot_trace_state.serialize()
55-
if not trace_state:
56-
if otts:
57-
new_trace_state = TraceState(((OTEL_TRACE_STATE_KEY, otts),))
58-
else:
59-
new_trace_state = None
60-
else:
61-
new_trace_state = intent.update_trace_state(trace_state)
62-
if otts:
63-
new_trace_state = new_trace_state.update(
64-
OTEL_TRACE_STATE_KEY, otts
65-
)
66-
67-
return SamplingResult(decision, intent.attributes, new_trace_state)
54+
return SamplingResult(
55+
decision,
56+
intent.attributes,
57+
_update_trace_state(trace_state, ot_trace_state, intent),
58+
)
6859

6960
def sampling_intent(
7061
self,
@@ -81,3 +72,19 @@ def sampling_intent(
8172

8273
def get_description(self) -> str:
8374
return self._delegate.get_description()
75+
76+
77+
def _update_trace_state(
78+
trace_state: Optional[TraceState],
79+
ot_trace_state: OtelTraceState,
80+
intent: SamplingIntent,
81+
) -> Optional[TraceState]:
82+
otts = ot_trace_state.serialize()
83+
if not trace_state:
84+
if otts:
85+
return TraceState(((OTEL_TRACE_STATE_KEY, otts),))
86+
return None
87+
new_trace_state = intent.update_trace_state(trace_state)
88+
if otts:
89+
return new_trace_state.update(OTEL_TRACE_STATE_KEY, otts)
90+
return new_trace_state
Lines changed: 105 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from dataclasses import dataclass
12
from typing import Optional
23

34
import pytest
@@ -29,112 +30,141 @@
2930
SPAN_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

Comments
 (0)