Repository navigation
perf(sdk): reduce span creation overhead #5824
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
herin049
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
herin049:perf/spans
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.
+146
−84
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| import copy | ||
| import logging | ||
| import sys | ||
| import threading | ||
| from collections.abc import Mapping, MutableMapping, Sequence | ||
| from types import NoneType | ||
|
|
@@ -27,41 +28,41 @@ def _is_non_custom_str(key: Any) -> bool: | |
| @overload | ||
| def _clean_attribute_value( | ||
| value: Mapping[str, types.AnyValue], | ||
| max_string_value_length: int | None, | ||
| max_string_value_length: int, | ||
| ) -> Mapping[str, types.AnyValue]: ... | ||
|
|
||
|
|
||
| @overload | ||
| def _clean_attribute_value( | ||
| value: types.AnyValue, | ||
| max_string_value_length: int | None, | ||
| max_string_value_length: int, | ||
| ) -> types.AnyValue: ... | ||
|
|
||
|
|
||
| def _clean_attribute_value( | ||
| value: types.AnyValue, | ||
| max_string_value_length: int | None, | ||
| max_string_value_length: int, | ||
| ) -> types.AnyValue: | ||
| """Recursively checks if an attribute value is valid and cleans it if required. | ||
|
|
||
| String values are truncated to max_string_value_length if provided. | ||
| String values are truncated to max_string_value_length. | ||
| Anything that isn't of `types.AnyValue`, we attempt to cast to `str`. | ||
| If this fails, the value is replaced with None. Sequence's are converted to tuples and mappings | ||
| are copied into new dicts. | ||
|
|
||
| Returns: | ||
| The recursively cleaned AnyValue. | ||
| """ | ||
| if isinstance(value, (NoneType, bool, int, float, bytes)): | ||
| return value | ||
| if isinstance(value, str): | ||
| if max_string_value_length is not None and len(value) > max_string_value_length: | ||
| if len(value) > max_string_value_length: | ||
| _logger.warning( | ||
| "String attribute value exceeds max length of %d, truncating.", | ||
| max_string_value_length, | ||
| ) | ||
| value = value[:max_string_value_length] | ||
| return value | ||
| if isinstance(value, (NoneType, bool, int, float, bytes)): | ||
| return value | ||
| if isinstance(value, Sequence): | ||
| return tuple(_clean_attribute_value(v, max_string_value_length) for v in value) | ||
| if isinstance(value, Mapping): | ||
|
|
@@ -149,9 +150,9 @@ def __init__( | |
| if max_value_len is not None and max_value_len < 0: | ||
| raise ValueError("max_value_len must be valid int greater or equal to 0") | ||
| self._dict: dict[str, types.AnyValue] = {} | ||
| self.maxlen = maxlen | ||
| self.maxlen: int = sys.maxsize if maxlen is None else maxlen | ||
| self.dropped = 0 | ||
| self.max_value_len = max_value_len | ||
| self.max_value_len: int = sys.maxsize if max_value_len is None else max_value_len | ||
| self._lock = threading.Lock() | ||
| # The spec allows the limit log to be emitted at most once per record, so track | ||
| # it separately from ``dropped``, which also counts other kinds of drops. | ||
|
|
@@ -168,13 +169,10 @@ def __repr__(self) -> str: | |
| def __getitem__(self, key: str) -> types.AnyValue: | ||
| return self._dict[key] | ||
|
|
||
| def _raise_if_immutable(self) -> None: | ||
| def __setitem__(self, key: str, value: types.AnyValue) -> None: | ||
| if self._immutable: | ||
| raise TypeError("Cannot mutate immutable BoundedAttributes") | ||
|
|
||
| def __setitem__(self, key: str, value: types.AnyValue) -> None: | ||
| self._raise_if_immutable() | ||
| if self.maxlen is not None and self.maxlen == 0: | ||
| if self.maxlen == 0: | ||
| with self._lock: | ||
| self.dropped += 1 | ||
| return | ||
|
|
@@ -191,21 +189,42 @@ def __setitem__(self, key: str, value: types.AnyValue) -> None: | |
| self._setitem_locked(key, cleaned) | ||
|
|
||
| def _set_items(self, attributes: Mapping[str, types.AnyValue]) -> None: | ||
| self._raise_if_immutable() | ||
| if self.maxlen is not None and self.maxlen == 0: | ||
| if self._immutable: | ||
| raise TypeError("Cannot mutate immutable BoundedAttributes") | ||
| if self.maxlen == 0: | ||
| with self._lock: | ||
| self.dropped += len(attributes) | ||
| return | ||
| cleaned_attributes: Mapping[str, types.AnyValue] = _clean_attribute_value(attributes, self.max_value_len) | ||
| with self._lock: | ||
| self.dropped += len(attributes) - len(cleaned_attributes) | ||
| for key, value in cleaned_attributes.items(): | ||
| self._setitem_locked(key, value) | ||
| for key, value in attributes.items(): | ||
| if not key: | ||
| _logger.warning( | ||
| "invalid attribute key `%s`. must be non-empty string. Dropping key from attributes.", | ||
| key, | ||
| ) | ||
| self.dropped += 1 | ||
| continue | ||
| # Spec says to convert unknown types to strings if possible. | ||
| if not isinstance(key, str): | ||
| _logger.warning( | ||
| "Invalid type `%s` for attribute key `%s`, must be a str. Key's `__str__/__repr__` method will be called if it exists, otherwise the key/value pair will be dropped.", | ||
| type(key), | ||
| key, | ||
| ) | ||
| if _is_non_custom_str(key): | ||
| key = str(key) | ||
| else: | ||
| self.dropped += 1 | ||
| continue | ||
| self._setitem_locked(key, _clean_attribute_value(value, self.max_value_len)) | ||
|
|
||
| def _setitem_locked(self, key: str, value: types.AnyValue) -> None: | ||
| if len(self._dict) < self.maxlen: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updating an existing attribute below capacity no longer moves it to the end, so a recently updated attribute can now be dropped instead of an older one. Could we preserve the previous eviction behavior? |
||
| self._dict[key] = value | ||
| return | ||
| if key in self._dict: | ||
| del self._dict[key] | ||
| if self.maxlen is not None and len(self._dict) >= self.maxlen: | ||
| if len(self._dict) >= self.maxlen: | ||
| if not self._dropped_logged: | ||
| self._dropped_logged = True | ||
| _logger.warning( | ||
|
|
@@ -218,7 +237,8 @@ def _setitem_locked(self, key: str, value: types.AnyValue) -> None: | |
| self._dict[key] = value | ||
|
|
||
| def __delitem__(self, key: str) -> None: | ||
| self._raise_if_immutable() | ||
| if self._immutable: | ||
| raise TypeError("Cannot mutate immutable BoundedAttributes") | ||
| del self._dict[key] | ||
|
|
||
| def __iter__(self): | ||
|
|
||
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
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
Oops, something went wrong.
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.
Could we keep input iteration and cleanup outside this lock? For a span with existing attributes,
span.set_attributes(span.attributes)tries to acquire the same lock twice and hangs.