Repository navigation
perf(modules): cache serialized installed modules mapping #8418
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,111 @@ | |
| import sentry_sdk | ||
| from sentry_sdk.integrations import Integration | ||
| from sentry_sdk.scope import add_global_event_processor | ||
| from sentry_sdk.serializer import serialize | ||
| from sentry_sdk.utils import _get_installed_modules | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any | ||
| from typing import Any, Callable, Dict, Optional, Tuple | ||
|
|
||
| from sentry_sdk._types import Event | ||
|
|
||
| _ModulesCacheEntry = Tuple[ | ||
| Dict[str, str], | ||
| Optional[int], | ||
| Optional[Callable[..., Optional[str]]], | ||
| Optional[Dict[str, Any]], | ||
| ] | ||
|
|
||
|
|
||
| class _SerializedModules(dict): # type: ignore[type-arg] | ||
| """Pre-serialized modules dict that returns a fresh plain dict to serialize().""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| data: "Dict[str, Any]", | ||
| max_value_length: "Optional[int]" = None, | ||
| custom_repr: "Optional[Callable[..., Optional[str]]]" = None, | ||
| ) -> None: | ||
| super().__init__(data) | ||
| self._max_value_length = max_value_length | ||
| self._custom_repr = custom_repr | ||
| self._mutated = False | ||
|
|
||
| def __sentry_repr__(self) -> "Dict[str, Any]": | ||
| if self._mutated: | ||
| return serialize( | ||
| {"modules": dict(self)}, | ||
| max_value_length=self._max_value_length, | ||
| custom_repr=self._custom_repr, | ||
| )["modules"] | ||
|
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. Mutation path drops truncation metadataMedium Severity When an event processor mutates Reviewed by Cursor Bugbot for commit 7472999. Configure here. 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. Nested serialize clears capture-event guardLow Severity Mutated modules call Reviewed by Cursor Bugbot for commit 7472999. Configure here. |
||
| return dict(self) | ||
|
|
||
| def __setitem__(self, key: "Any", value: "Any") -> None: | ||
| self._mutated = True | ||
| super().__setitem__(key, value) | ||
|
|
||
| def __delitem__(self, key: "Any") -> None: | ||
| self._mutated = True | ||
| super().__delitem__(key) | ||
|
|
||
| def clear(self) -> None: | ||
| self._mutated = True | ||
| super().clear() | ||
|
|
||
| def pop(self, *args: "Any") -> "Any": | ||
| self._mutated = True | ||
| return super().pop(*args) | ||
|
|
||
| def popitem(self) -> "Tuple[Any, Any]": | ||
| self._mutated = True | ||
| return super().popitem() | ||
|
|
||
| def setdefault(self, *args: "Any") -> "Any": | ||
| self._mutated = True | ||
| return super().setdefault(*args) | ||
|
|
||
| def update(self, *args: "Any", **kwargs: "Any") -> None: | ||
| self._mutated = True | ||
| super().update(*args, **kwargs) | ||
|
|
||
|
|
||
| _cached_modules: "Optional[_ModulesCacheEntry]" = None | ||
|
|
||
|
|
||
| def _get_serialized_modules( | ||
| raw_modules: "Dict[str, str]", | ||
| max_value_length: "Optional[int]", | ||
| custom_repr: "Optional[Callable[..., Optional[str]]]", | ||
| ) -> "Dict[str, Any]": | ||
| global _cached_modules | ||
| cached = _cached_modules | ||
| if ( | ||
| cached is None | ||
| or cached[0] is not raw_modules | ||
| or cached[1] != max_value_length | ||
| or cached[2] != custom_repr | ||
| or (cached[3] is not None and len(cached[3]) != len(raw_modules)) | ||
| ): | ||
| serialized = serialize( | ||
| {"modules": raw_modules}, | ||
| max_value_length=max_value_length, | ||
| custom_repr=custom_repr, | ||
| ) | ||
| pre_serialized: "Optional[Dict[str, Any]]" = ( | ||
| None if serialized.get("_meta") else serialized["modules"] | ||
| ) | ||
| cached = (raw_modules, max_value_length, custom_repr, pre_serialized) | ||
| _cached_modules = cached | ||
|
|
||
| if cached[3] is None: | ||
| return dict(raw_modules) | ||
|
|
||
| return _SerializedModules( | ||
| cached[3], | ||
| max_value_length=max_value_length, | ||
| custom_repr=custom_repr, | ||
| ) | ||
|
|
||
|
|
||
| class ModulesIntegration(Integration): | ||
| identifier = "modules" | ||
|
|
@@ -21,8 +119,13 @@ def processor(event: "Event", hint: "Any") -> "Event": | |
| if event.get("type") == "transaction": | ||
| return event | ||
|
|
||
| if sentry_sdk.get_client().get_integration(ModulesIntegration) is None: | ||
| client = sentry_sdk.get_client() | ||
| if client.get_integration(ModulesIntegration) is None: | ||
| return event | ||
|
|
||
| event["modules"] = _get_installed_modules() | ||
| event["modules"] = _get_serialized_modules( | ||
| _get_installed_modules(), | ||
| client.options.get("max_value_length"), | ||
| client.options.get("custom_repr"), | ||
| ) | ||
| return event | ||


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.
Dict subclass rejects no-arg construction
Medium Severity
_SerializedModules.__init__requiresdata, so same-type construction with no arguments fails. Standard dict operations that do that (copy(),|,copy.copy(),deepcopy) raiseTypeErroronevent["modules"]. If an event processor hits one of those, the callback error drops the event.Reviewed by Cursor Bugbot for commit 7472999. Configure here.