Skip to content

Commit 07b72d4

Browse files
committed
add AnnotatedDeque for better handling
1 parent f6f9dc7 commit 07b72d4

File tree

3 files changed

+54
-18
lines changed

3 files changed

+54
-18
lines changed

sentry_sdk/_types.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, TypeVar, Union
1+
from typing import TYPE_CHECKING, Deque, TypeVar, Union
22

33

44
# Re-exported for compat, since code out there in the wild might use this variable.
@@ -81,18 +81,53 @@ def substituted_because_contains_sensitive_data(cls):
8181
},
8282
)
8383

84+
T = TypeVar("T")
85+
Annotated = Union[AnnotatedValue, T]
86+
class AnnotatedDeque(AnnotatedValue):
87+
"""
88+
Meta information for a data field in the event payload.
89+
This is to tell Relay that we have tampered with the fields value.
90+
See:
91+
https://github.com/getsentry/relay/blob/be12cd49a0f06ea932ed9b9f93a655de5d6ad6d1/relay-general/src/types/meta.rs#L407-L423
92+
"""
93+
94+
__slots__ = ("value", "metadata")
95+
96+
def __init__(self, value, metadata):
97+
# type: (Deque[Any], Dict[str, Any]) -> None
98+
self.value = value
99+
self.metadata = metadata
100+
101+
def __eq__(self, other):
102+
# type: (Any) -> bool
103+
if not isinstance(other, AnnotatedValue):
104+
return False
105+
106+
return self.value == other.value and self.metadata == other.metadata
107+
108+
def append(self, other):
109+
# type: (Any) -> None
110+
self.value.append(other)
111+
112+
def extend(self, other):
113+
# type: (Any) -> None
114+
self.value.extend(other)
115+
116+
def popleft(self):
117+
self.value.popleft()
118+
119+
def __len__(self):
120+
return len(self.value)
121+
84122
@classmethod
85-
def truncated_breadcrumbs(cls, breadcrumbs, n_truncated):
86-
# type: (list[Breadcrumb], int) -> AnnotatedValue
87-
"""Breadcrumbs were removed because the number of breadcrumbs exceeded their maximum limit."""
88-
return AnnotatedValue(
89-
value=breadcrumbs,
123+
def truncated(cls, value, n_truncated):
124+
# type: (Deque[Any], int) -> AnnotatedValue
125+
"""Data was removed because the number of elements exceeded the maximum limit."""
126+
return AnnotatedDeque(
127+
value=value,
90128
metadata={"len": [n_truncated]}, # Remark
91129
)
92130

93-
T = TypeVar("T")
94-
Annotated = Union[AnnotatedValue, T]
95-
96131
if TYPE_CHECKING:
97132
from collections.abc import Container, MutableMapping, Sequence
98133

sentry_sdk/scope.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
Transaction,
3434
)
3535
from sentry_sdk.utils import (
36-
AnnotatedValue,
36+
AnnotatedDeque,
3737
capture_internal_exception,
3838
capture_internal_exceptions,
3939
ContextVar,
@@ -1388,12 +1388,13 @@ def _apply_breadcrumbs_to_event(self, event, hint, options):
13881388
pass
13891389

13901390
# Add annotation that breadcrumbs were truncated
1391-
original_length = (
1392-
len(event["breadcrumbs"]["values"]) + self._n_breadcrumbs_truncated
1393-
)
1394-
event["breadcrumbs"]["values"] = AnnotatedValue.truncated_breadcrumbs(
1395-
event["breadcrumbs"]["values"], original_length
1396-
)
1391+
if self._n_breadcrumbs_truncated:
1392+
original_length = (
1393+
len(event["breadcrumbs"]["values"]) + self._n_breadcrumbs_truncated
1394+
)
1395+
event["breadcrumbs"]["values"] = AnnotatedDeque.truncated_breadcrumbs(
1396+
event["breadcrumbs"]["values"], original_length
1397+
)
13971398

13981399
def _apply_user_to_event(self, event, hint, options):
13991400
# type: (Event, Hint, Optional[Dict[str, Any]]) -> None

sentry_sdk/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
Callable,
4444
cast,
4545
ContextManager,
46+
Deque,
4647
Dict,
4748
Iterator,
4849
List,
@@ -59,7 +60,7 @@
5960

6061
from gevent.hub import Hub
6162

62-
from sentry_sdk._types import Breadcrumb, Event, ExcInfo
63+
from sentry_sdk._types import Event, ExcInfo
6364

6465
P = ParamSpec("P")
6566
R = TypeVar("R")
@@ -411,7 +412,6 @@ def to_header(self):
411412
rv.append(("sentry_secret", self.secret_key))
412413
return "Sentry " + ", ".join("%s=%s" % (key, value) for key, value in rv)
413414

414-
415415
def get_type_name(cls):
416416
# type: (Optional[type]) -> Optional[str]
417417
return getattr(cls, "__qualname__", None) or getattr(cls, "__name__", None)

0 commit comments

Comments
 (0)