opentelemetry-sdk: inline the method _clean_attribute_value#5275
Open
codeboten wants to merge 7 commits into
Open
opentelemetry-sdk: inline the method _clean_attribute_value#5275codeboten wants to merge 7 commits into
_clean_attribute_value#5275codeboten wants to merge 7 commits into
Conversation
MikeGoldsmith
approved these changes
Jun 5, 2026
This removes an if statement to check if the value is None and embeds the method inline to remove the need for that check. This removes a second check for None in the loop through the Sequence type. Ran the following: ``` pytest opentelemetry-sdk/benchmarks/trace/test_benchmark_trace.py::test_set_attribute_types \ --benchmark-min-rounds=10 --benchmark-columns=mean,median,ops ``` And got these results for the different types: | type | Before (ms) | After (ms) | Δ | |-----------|------------|-----------|------| | bool | 204.1 | 197.3 | -3% | | str | 211.9 | 203.9 | -4% | | int | 221.3 | 213.5 | -4% | | bytes | 226.5 | 216.8 | -4% | | float | 227.3 | 222.1 | -2% | | seq_bool | 488.3 | 453.7 | -7% | | seq_bytes | 513.7 | 471.7 | -8% | | seq_float | 539.6 | 508.4 | -6% | | seq_str | 594.3 | 537.0 | -10% | | seq_int | 636.0 | 576.3 | -9% | Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
d9d6219 to
829474b
Compare
Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
Contributor
|
If we end up using inlining more often, it might be worth writing a custom Hatchling build hook to allow us to just write @inline
def _clean_attribute_value(
value: types.AttributeValue, limit: int | None
) -> types.AttributeValue | None:
if value is None:
return None
if isinstance(value, bytes):
try:
value = value.decode()
except UnicodeDecodeError:
_logger.warning("Byte attribute could not be decoded.")
return None
if limit is not None and isinstance(value, str):
value = value[:limit]
return value |
herin049
approved these changes
Jun 8, 2026
Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This removes an if statement to check if the value is None and embeds the method inline to remove the need for that check. A second check for None is removed in the loop through the Sequence type.
Note to reviewers: I bumped the number of spans in the benchmark test to 5000 to get a more consistent result, it doesn't have to stay here, let me know if you'd like it removed.
Follow up to #5274, can rebase after it's merged (if it merges) or without it if reviewers would prefer.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Ran the following:
And got these results for the different types:
Does This PR Require a Contrib Repo Change?
Checklist: