Skip to content

Commit 9890ddf

Browse files
committed
Cleanup and match spec better
1 parent 3c11c99 commit 9890ddf

File tree

22 files changed

+601
-346
lines changed

22 files changed

+601
-346
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
- Add experimental consistent samplers
10+
- Add experimental composite samplers
1111
([#4714](https://github.com/open-telemetry/opentelemetry-python/pull/4714))
1212

1313
## Version 1.36.0/0.57b0 (2025-07-29)
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
__all__ = [
216
"ComposableSampler",
3-
"ConsistentSampler",
417
"SamplingIntent",
5-
"consistent_always_off",
6-
"consistent_always_on",
7-
"consistent_parent_based",
8-
"consistent_probability_based",
18+
"composable_always_off",
19+
"composable_always_on",
20+
"composable_parent_threshold",
21+
"composable_traceid_ratio_based",
22+
"composite_sampler",
923
]
1024

1125

12-
from ._always_off import consistent_always_off
13-
from ._always_on import consistent_always_on
26+
from ._always_off import composable_always_off
27+
from ._always_on import composable_always_on
1428
from ._composable import ComposableSampler, SamplingIntent
15-
from ._fixed_threshold import consistent_probability_based
16-
from ._parent_based import consistent_parent_based
17-
from ._sampler import ConsistentSampler
29+
from ._parent_threshold import composable_parent_threshold
30+
from ._sampler import composite_sampler
31+
from ._traceid_ratio import composable_traceid_ratio_based
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,55 @@
1-
from typing import Optional, Sequence
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from typing import Sequence
218

319
from opentelemetry.context import Context
420
from opentelemetry.trace import Link, SpanKind, TraceState
521
from opentelemetry.util.types import Attributes
622

723
from ._composable import ComposableSampler, SamplingIntent
8-
from ._sampler import ConsistentSampler
924
from ._util import INVALID_THRESHOLD
1025

11-
_intent = SamplingIntent(
12-
threshold=INVALID_THRESHOLD, adjusted_count_reliable=False
13-
)
26+
_intent = SamplingIntent(threshold=INVALID_THRESHOLD, threshold_reliable=False)
1427

1528

16-
class ConsistentAlwaysOffSampler(ComposableSampler):
29+
class _ComposableAlwaysOffSampler(ComposableSampler):
1730
def sampling_intent(
1831
self,
19-
parent_ctx: Optional[Context],
32+
parent_ctx: Context | None,
2033
name: str,
21-
span_kind: Optional[SpanKind],
34+
span_kind: SpanKind | None,
2235
attributes: Attributes,
23-
links: Optional[Sequence[Link]],
24-
trace_state: Optional[TraceState] = None,
36+
links: Sequence[Link] | None,
37+
trace_state: TraceState | None = None,
2538
) -> SamplingIntent:
2639
return _intent
2740

2841
def get_description(self) -> str:
29-
return "ConsistentAlwaysOffSampler"
42+
return "ComposableAlwaysOff"
3043

3144

32-
_always_off = ConsistentSampler(ConsistentAlwaysOffSampler())
45+
_always_off = _ComposableAlwaysOffSampler()
3346

3447

35-
def consistent_always_off() -> ConsistentSampler:
36-
"""Returns a consistent sampler that does not sample any span."""
48+
def composable_always_off() -> ComposableSampler:
49+
"""Returns a composable sampler that does not sample any span.
50+
51+
- Always returns a SamplingIntent with no threshold, indicating all spans should be dropped
52+
- Sets threshold_reliable to false
53+
- Does not add any attributes
54+
"""
3755
return _always_off
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
1-
from typing import Optional, Sequence
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from typing import Sequence
218

319
from opentelemetry.context import Context
420
from opentelemetry.trace import Link, SpanKind, TraceState
521
from opentelemetry.util.types import Attributes
622

723
from ._composable import ComposableSampler, SamplingIntent
8-
from ._sampler import ConsistentSampler
924
from ._util import MIN_THRESHOLD
1025

1126
_intent = SamplingIntent(threshold=MIN_THRESHOLD)
1227

1328

14-
class ConsistentAlwaysOnSampler(ComposableSampler):
29+
class _ComposableAlwaysOnSampler(ComposableSampler):
1530
def sampling_intent(
1631
self,
17-
parent_ctx: Optional[Context],
32+
parent_ctx: Context | None,
1833
name: str,
19-
span_kind: Optional[SpanKind],
34+
span_kind: SpanKind | None,
2035
attributes: Attributes,
21-
links: Optional[Sequence[Link]],
22-
trace_state: Optional[TraceState] = None,
36+
links: Sequence[Link] | None,
37+
trace_state: TraceState | None = None,
2338
) -> SamplingIntent:
2439
return _intent
2540

2641
def get_description(self) -> str:
27-
return "ConsistentAlwaysOnSampler"
42+
return "ComposableAlwaysOn"
2843

2944

30-
_always_on = ConsistentSampler(ConsistentAlwaysOnSampler())
45+
_always_on = _ComposableAlwaysOnSampler()
3146

3247

33-
def consistent_always_on() -> ConsistentSampler:
34-
"""Returns a consistent sampler that samples all spans."""
48+
def composable_always_on() -> ComposableSampler:
49+
"""Returns a composable sampler that samples all spans.
50+
51+
- Always returns a SamplingIntent with threshold set to sample all spans (threshold = 0)
52+
- Sets threshold_reliable to true
53+
- Does not add any attributes
54+
"""
3555
return _always_on
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
117
from dataclasses import dataclass, field
2-
from typing import Callable, Optional, Protocol, Sequence
18+
from typing import Callable, Protocol, Sequence
319

420
from opentelemetry.context import Context
521
from opentelemetry.trace import Link, SpanKind, TraceState
@@ -11,26 +27,33 @@ class SamplingIntent:
1127
"""Information to make a consistent sampling decision."""
1228

1329
threshold: int
14-
adjusted_count_reliable: bool = field(default=True)
30+
"""The sampling threshold value. A lower threshold increases the likelihood of sampling."""
31+
32+
threshold_reliable: bool = field(default=True)
33+
"""Indicates whether the threshold is reliable for Span-to-Metrics estimation."""
34+
1535
attributes: Attributes = field(default=None)
36+
"""Any attributes to be added to a sampled span."""
37+
1638
update_trace_state: Callable[[TraceState], TraceState] = field(
1739
default=lambda ts: ts
1840
)
41+
"""Any updates to be made to trace state."""
1942

2043

2144
class ComposableSampler(Protocol):
22-
"""A sampler that can be composed to make a final consistent sampling decision."""
45+
"""A sampler that can be composed to make a final sampling decision."""
2346

2447
def sampling_intent(
2548
self,
26-
parent_ctx: Optional[Context],
49+
parent_ctx: Context | None,
2750
name: str,
28-
span_kind: Optional[SpanKind],
51+
span_kind: SpanKind | None,
2952
attributes: Attributes,
30-
links: Optional[Sequence[Link]],
31-
trace_state: Optional[TraceState],
53+
links: Sequence[Link] | None,
54+
trace_state: TraceState | None,
3255
) -> SamplingIntent:
33-
"""Returns information to make a consistent sampling decision."""
56+
"""Returns information to make a sampling decision."""
3457

3558
def get_description(self) -> str:
3659
"""Returns a description of the sampler."""

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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)