Skip to content

Commit aaa78c4

Browse files
committed
store a hash of the exc instead of full exc
1 parent c2d5a76 commit aaa78c4

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

sentry_sdk/integrations/dedupe.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@ def __init__(self):
1818
# type: () -> None
1919
self._last_seen = ContextVar("last-seen")
2020

21+
@staticmethod
22+
def _get_exception_hash(exc):
23+
# type: (Exception) -> int
24+
"""
25+
Create a memory-efficient hash for an exception.
26+
27+
Instead of storing the entire exception object, we store just enough
28+
information to identify it uniquely. This avoids keeping the traceback
29+
and local variables in memory.
30+
"""
31+
# Get the exception type name and message
32+
exc_type = type(exc).__name__
33+
exc_message = str(exc)
34+
35+
# Get the first frame of the traceback if it exists
36+
if hasattr(exc, "__traceback__") and exc.__traceback__:
37+
frame = exc.__traceback__.tb_frame
38+
filename = frame.f_code.co_filename
39+
lineno = frame.f_lineno
40+
func_name = frame.f_code.co_name
41+
location = f"{filename}:{lineno}:{func_name}" # noqa: E231
42+
else:
43+
location = None
44+
45+
# Create a tuple of the essential information and hash it
46+
return hash((exc_type, exc_message, location))
47+
2148
@staticmethod
2249
def setup_once():
2350
# type: () -> None
@@ -36,9 +63,12 @@ def processor(event, hint):
3663
return event
3764

3865
exc = exc_info[1]
39-
if integration._last_seen.get(None) is exc:
66+
exc_hash = DedupeIntegration._get_exception_hash(exc)
67+
68+
if integration._last_seen.get(None) == exc_hash:
4069
return None
41-
integration._last_seen.set(exc)
70+
71+
integration._last_seen.set(exc_hash)
4272
return event
4373

4474
@staticmethod

0 commit comments

Comments
 (0)