-
Notifications
You must be signed in to change notification settings - Fork 783
opentelemetry-sdk: add experimental composable rule based sampler #4882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xrmx
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
xrmx:compose-rule-based-sampler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+290
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e88163
opentelemetry-sdk: add experimental composable rule based sampler
xrmx c3f82f8
Add CHANGELOG
xrmx d74e401
Use a protocol for PredicateT
xrmx 766b3ef
Update opentelemetry-sdk/src/opentelemetry/sdk/trace/_sampling_experi…
xrmx 9a43146
Extend Predicate protocol to require __str__ and use it on sampler ge…
xrmx aa259a1
Merge branch 'main' into compose-rule-based-sampler
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
opentelemetry-sdk/src/opentelemetry/sdk/trace/_sampling_experimental/_rule_based.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Protocol, Sequence | ||
|
|
||
| from opentelemetry.context import Context | ||
| from opentelemetry.trace import Link, SpanKind, TraceState | ||
| from opentelemetry.util.types import AnyValue, Attributes | ||
|
|
||
| from ._composable import ComposableSampler, SamplingIntent | ||
| from ._util import INVALID_THRESHOLD | ||
|
|
||
|
|
||
| class PredicateT(Protocol): | ||
| def __call__( | ||
| self, | ||
| parent_ctx: Context | None, | ||
| name: str, | ||
| span_kind: SpanKind | None, | ||
| attributes: Attributes, | ||
| links: Sequence[Link] | None, | ||
| trace_state: TraceState | None, | ||
| ) -> bool: ... | ||
|
|
||
| def __str__(self) -> str: ... | ||
|
|
||
|
|
||
| class AttributePredicate: | ||
| """An exact match of an attribute value""" | ||
|
|
||
| def __init__(self, key: str, value: AnyValue): | ||
| self.key = key | ||
| self.value = value | ||
|
|
||
| def __call__( | ||
| self, | ||
| parent_ctx: Context | None, | ||
| name: str, | ||
| span_kind: SpanKind | None, | ||
| attributes: Attributes, | ||
| links: Sequence[Link] | None, | ||
| trace_state: TraceState | None, | ||
| ) -> bool: | ||
| if not attributes: | ||
| return False | ||
| return attributes.get(self.key) == self.value | ||
|
|
||
| def __str__(self): | ||
| return f"{self.key}={self.value}" | ||
|
|
||
|
|
||
| RulesT = Sequence[tuple[PredicateT, ComposableSampler]] | ||
xrmx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| _non_sampling_intent = SamplingIntent( | ||
| threshold=INVALID_THRESHOLD, threshold_reliable=False | ||
| ) | ||
|
|
||
|
|
||
| class _ComposableRuleBased(ComposableSampler): | ||
| def __init__(self, rules: RulesT): | ||
| # work on an internal copy of the rules | ||
| self._rules = list(rules) | ||
|
|
||
| def sampling_intent( | ||
| self, | ||
| parent_ctx: Context | None, | ||
| name: str, | ||
| span_kind: SpanKind | None, | ||
| attributes: Attributes, | ||
| links: Sequence[Link] | None, | ||
| trace_state: TraceState | None = None, | ||
| ) -> SamplingIntent: | ||
| for predicate, sampler in self._rules: | ||
| if predicate( | ||
| parent_ctx=parent_ctx, | ||
| name=name, | ||
| span_kind=span_kind, | ||
| attributes=attributes, | ||
| links=links, | ||
| trace_state=trace_state, | ||
| ): | ||
| return sampler.sampling_intent( | ||
| parent_ctx=parent_ctx, | ||
| name=name, | ||
| span_kind=span_kind, | ||
| attributes=attributes, | ||
| links=links, | ||
| trace_state=trace_state, | ||
| ) | ||
| return _non_sampling_intent | ||
|
|
||
| def get_description(self) -> str: | ||
| rules_str = ",".join( | ||
| [ | ||
| f"({predicate}:{sampler.get_description()})" | ||
| for predicate, sampler in self._rules | ||
| ] | ||
| ) | ||
| return f"ComposableRuleBased{{[{rules_str}]}}" | ||
|
|
||
|
|
||
| def composable_rule_based( | ||
| rules: RulesT, | ||
| ) -> ComposableSampler: | ||
| """Returns a consistent sampler that: | ||
|
|
||
| - Evaluates a series of rules based on predicates and returns the SamplingIntent from the first matching sampler | ||
| - If no rules match, returns a non-sampling intent | ||
|
|
||
| Args: | ||
| rules: A list of (Predicate, ComposableSampler) pairs, where Predicate is a function that evaluates whether a rule applies | ||
| """ | ||
| return _ComposableRuleBased(rules) | ||
160 changes: 160 additions & 0 deletions
160
opentelemetry-sdk/tests/trace/composite_sampler/test_rule_based.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from opentelemetry.sdk.trace._sampling_experimental import ( | ||
| composable_always_off, | ||
| composable_always_on, | ||
| composable_rule_based, | ||
| composite_sampler, | ||
| ) | ||
| from opentelemetry.sdk.trace._sampling_experimental._rule_based import ( | ||
| AttributePredicate, | ||
| ) | ||
| from opentelemetry.sdk.trace.id_generator import RandomIdGenerator | ||
| from opentelemetry.sdk.trace.sampling import Decision | ||
|
|
||
|
|
||
| class NameIsFooPredicate: | ||
| def __call__( | ||
| self, | ||
| parent_ctx, | ||
| name, | ||
| span_kind, | ||
| attributes, | ||
| links, | ||
| trace_state, | ||
| ): | ||
| return name == "foo" | ||
|
|
||
| def __str__(self): | ||
| return "NameIsFooPredicate" | ||
|
|
||
|
|
||
| def test_description_with_no_rules(): | ||
| assert ( | ||
| composable_rule_based(rules=[]).get_description() | ||
| == "ComposableRuleBased{[]}" | ||
| ) | ||
|
|
||
|
|
||
| def test_description_with_rules(): | ||
| rules = [ | ||
| (AttributePredicate("foo", "bar"), composable_always_on()), | ||
| (NameIsFooPredicate(), composable_always_off()), | ||
| ] | ||
| assert ( | ||
| composable_rule_based(rules=rules).get_description() | ||
| == "ComposableRuleBased{[(foo=bar:ComposableAlwaysOn),(NameIsFooPredicate:ComposableAlwaysOff)]}" | ||
| ) | ||
|
|
||
|
|
||
| def test_sampling_intent_match(): | ||
| rules = [ | ||
| (NameIsFooPredicate(), composable_always_on()), | ||
| ] | ||
| assert ( | ||
| composable_rule_based(rules=rules) | ||
| .sampling_intent(None, "foo", None, {}, None, None) | ||
| .threshold | ||
| == 0 | ||
| ) | ||
|
|
||
|
|
||
| def test_sampling_intent_no_match(): | ||
| rules = [ | ||
| (NameIsFooPredicate(), composable_always_on()), | ||
| ] | ||
| assert ( | ||
| composable_rule_based(rules=rules) | ||
| .sampling_intent(None, "test", None, {}, None, None) | ||
| .threshold | ||
| == -1 | ||
| ) | ||
|
|
||
|
|
||
| def test_should_sample_match(): | ||
| rules = [ | ||
| (NameIsFooPredicate(), composable_always_on()), | ||
| ] | ||
| sampler = composite_sampler(composable_rule_based(rules=rules)) | ||
|
|
||
| res = sampler.should_sample( | ||
| None, | ||
| RandomIdGenerator().generate_trace_id(), | ||
| "foo", | ||
| None, | ||
| None, | ||
| None, | ||
| None, | ||
| ) | ||
|
|
||
| assert res.decision == Decision.RECORD_AND_SAMPLE | ||
| assert res.trace_state is not None | ||
| assert res.trace_state.get("ot", "") == "th:0" | ||
|
|
||
|
|
||
| def test_should_sample_no_match(): | ||
| rules = [ | ||
| (NameIsFooPredicate(), composable_always_on()), | ||
| ] | ||
| sampler = composite_sampler(composable_rule_based(rules=rules)) | ||
|
|
||
| res = sampler.should_sample( | ||
| None, | ||
| RandomIdGenerator().generate_trace_id(), | ||
| "test", | ||
| None, | ||
| None, | ||
| None, | ||
| None, | ||
| ) | ||
|
|
||
| assert res.decision == Decision.DROP | ||
| assert res.trace_state is None | ||
|
|
||
|
|
||
| def test_attribute_predicate_no_attributes(): | ||
| rules = [ | ||
| (AttributePredicate("foo", "bar"), composable_always_on()), | ||
| ] | ||
| assert ( | ||
| composable_rule_based(rules=rules) | ||
| .sampling_intent(None, "span", None, None, None, None) | ||
| .threshold | ||
| == -1 | ||
| ) | ||
|
|
||
|
|
||
| def test_attribute_predicate_no_match(): | ||
| rules = [ | ||
| (AttributePredicate("foo", "bar"), composable_always_on()), | ||
| ] | ||
| assert ( | ||
| composable_rule_based(rules=rules) | ||
| .sampling_intent(None, "span", None, {"foo": "foo"}, None, None) | ||
| .threshold | ||
| == -1 | ||
| ) | ||
|
|
||
|
|
||
| def test_attribute_predicate_match(): | ||
| rules = [ | ||
| (AttributePredicate("foo", "bar"), composable_always_on()), | ||
| ] | ||
| assert ( | ||
| composable_rule_based(rules=rules) | ||
| .sampling_intent(None, "span", None, {"foo": "bar"}, None, None) | ||
| .threshold | ||
| == 0 | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to export this in
__init__.py? Or otherwise probably better to move to the testThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I want to export this from
__init__.pysince this is not in the spec but I've seen java implements this (and isRoot) and can be handy for users I guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right - just basically, if not exporting it, it could just be treated as a test helper instead of here I think