99from functools import wraps
1010from itertools import chain
1111
12+ from sentry_sdk ._types import AnnotatedValue
1213from sentry_sdk .attachments import Attachment
1314from sentry_sdk .consts import DEFAULT_MAX_BREADCRUMBS , FALSE_VALUES , INSTRUMENTER
1415from sentry_sdk .feature_flags import FlagBuffer , DEFAULT_FLAG_CAPACITY
@@ -186,6 +187,7 @@ class Scope:
186187 "_contexts" ,
187188 "_extras" ,
188189 "_breadcrumbs" ,
190+ "_n_breadcrumbs_truncated" ,
189191 "_event_processors" ,
190192 "_error_processors" ,
191193 "_should_capture" ,
@@ -210,6 +212,7 @@ def __init__(self, ty=None, client=None):
210212
211213 self ._name = None # type: Optional[str]
212214 self ._propagation_context = None # type: Optional[PropagationContext]
215+ self ._n_breadcrumbs_truncated = 0 # type: int
213216
214217 self .client = NonRecordingClient () # type: sentry_sdk.client.BaseClient
215218
@@ -243,6 +246,7 @@ def __copy__(self):
243246 rv ._extras = dict (self ._extras )
244247
245248 rv ._breadcrumbs = copy (self ._breadcrumbs )
249+ rv ._n_breadcrumbs_truncated = copy (self ._n_breadcrumbs_truncated )
246250 rv ._event_processors = list (self ._event_processors )
247251 rv ._error_processors = list (self ._error_processors )
248252 rv ._propagation_context = self ._propagation_context
@@ -916,6 +920,7 @@ def clear_breadcrumbs(self):
916920 # type: () -> None
917921 """Clears breadcrumb buffer."""
918922 self ._breadcrumbs = deque () # type: Deque[Breadcrumb]
923+ self ._n_breadcrumbs_truncated = 0
919924
920925 def add_attachment (
921926 self ,
@@ -983,6 +988,7 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
983988
984989 while len (self ._breadcrumbs ) > max_breadcrumbs :
985990 self ._breadcrumbs .popleft ()
991+ self ._n_breadcrumbs_truncated += 1
986992
987993 def start_transaction (
988994 self ,
@@ -1366,17 +1372,23 @@ def _apply_level_to_event(self, event, hint, options):
13661372
13671373 def _apply_breadcrumbs_to_event (self , event , hint , options ):
13681374 # type: (Event, Hint, Optional[Dict[str, Any]]) -> None
1369- event .setdefault ("breadcrumbs" , {}).setdefault ("values" , []).extend (
1370- self ._breadcrumbs
1371- )
1375+ event .setdefault ("breadcrumbs" , {})
1376+
1377+ # This check is just for mypy -
1378+ if not isinstance (event ["breadcrumbs" ], AnnotatedValue ):
1379+ event ["breadcrumbs" ].setdefault ("values" , [])
1380+ event ["breadcrumbs" ]["values" ].extend (self ._breadcrumbs )
13721381
13731382 # Attempt to sort timestamps
13741383 try :
1375- for crumb in event ["breadcrumbs" ]["values" ]:
1376- if isinstance (crumb ["timestamp" ], str ):
1377- crumb ["timestamp" ] = datetime_from_isoformat (crumb ["timestamp" ])
1384+ if not isinstance (event ["breadcrumbs" ], AnnotatedValue ):
1385+ for crumb in event ["breadcrumbs" ]["values" ]:
1386+ if isinstance (crumb ["timestamp" ], str ):
1387+ crumb ["timestamp" ] = datetime_from_isoformat (crumb ["timestamp" ])
13781388
1379- event ["breadcrumbs" ]["values" ].sort (key = lambda crumb : crumb ["timestamp" ])
1389+ event ["breadcrumbs" ]["values" ].sort (
1390+ key = lambda crumb : crumb ["timestamp" ]
1391+ )
13801392 except Exception as err :
13811393 logger .debug ("Error when sorting breadcrumbs" , exc_info = err )
13821394 pass
@@ -1564,6 +1576,10 @@ def update_from_scope(self, scope):
15641576 self ._extras .update (scope ._extras )
15651577 if scope ._breadcrumbs :
15661578 self ._breadcrumbs .extend (scope ._breadcrumbs )
1579+ if scope ._n_breadcrumbs_truncated :
1580+ self ._n_breadcrumbs_truncated = (
1581+ self ._n_breadcrumbs_truncated + scope ._n_breadcrumbs_truncated
1582+ )
15671583 if scope ._span :
15681584 self ._span = scope ._span
15691585 if scope ._attachments :
0 commit comments