Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions sentry_sdk/integrations/dedupe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Optional
from typing import Optional, Tuple, Type, Any

from sentry_sdk._types import Event, Hint


def _safe_args_hash(args: "Tuple[Any, ...]") -> int:
try:
return hash(args)
except Exception:
try:
return hash(repr(args))
except Exception:
return 0


def _fingerprint(exc: BaseException) -> "Tuple[Type[BaseException], int]":
return (type(exc), _safe_args_hash(exc.args))


class DedupeIntegration(Integration):
identifier = "dedupe"

Expand All @@ -35,22 +49,23 @@ def processor(event: "Event", hint: "Optional[Hint]") -> "Optional[Event]":
return event

last_seen = integration._last_seen.get(None)
if last_seen is not None:
# last_seen is either a weakref or the original instance
last_seen = (
last_seen() if isinstance(last_seen, weakref.ref) else last_seen
)

exc = exc_info[1]
if last_seen is exc:

is_duplicate = False
if isinstance(last_seen, weakref.ref):
is_duplicate = last_seen() is exc
elif isinstance(last_seen, tuple):
is_duplicate = last_seen == _fingerprint(exc)

if is_duplicate:
logger.info("DedupeIntegration dropped duplicated error event %s", exc)
return None

# we can only weakref non builtin types
try:
integration._last_seen.set(weakref.ref(exc))
except TypeError:
integration._last_seen.set(exc)
integration._last_seen.set(_fingerprint(exc))

return event

Expand Down
Loading