Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .sampo/changesets/capture-exception-no-arg-dedupe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

`capture_exception()` called without an exception now marks the exception it takes from `sys.exc_info()` as captured, so a later automatic capture of the same exception no longer sends a second `$exception` event.
10 changes: 8 additions & 2 deletions posthog/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,10 @@ def capture_exception(
flags=kwargs.get("flags"),
disable_geoip=kwargs.get("disable_geoip"),
)
if exception is not None and result is not None:
mark_exception_as_captured(exception, result)
# Mark the exception as captured to prevent duplicate captures,
# including one taken from sys.exc_info()
if result is not None:
mark_exception_as_captured(exc_info, result)
return result
except Exception as error:
self.log.exception("Failed to capture exception: %s", error)
Expand Down Expand Up @@ -983,6 +985,10 @@ def _exception_info(self, exception: Optional[ExceptionArg]):
self.log.warning("No exception information available")
return None

if exception is None and exception_is_already_captured(exc_info):
self.log.debug("Exception already captured, skipping")
return None

return exc_info

def _get_feature_flag_evaluations_host(self) -> _FeatureFlagEvaluationsHost:
Expand Down
11 changes: 8 additions & 3 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2932,9 +2932,10 @@ def capture_exception(
disable_geoip=disable_geoip,
)

# Mark the exception as captured to prevent duplicate captures
if exception is not None and res is not None:
mark_exception_as_captured(exception, res)
# Mark the exception as captured to prevent duplicate captures,
# including one taken from sys.exc_info()
if res is not None:
mark_exception_as_captured(exc_info, res)

return res
except Exception as e:
Expand All @@ -2956,6 +2957,10 @@ def _exception_info(self, exception):
self.log.warning("No exception information available")
return None

if exception is None and exception_is_already_captured(exc_info):
self.log.debug("Exception already captured, skipping")
return None

return exc_info

def _exception_properties(self, exc_info, properties, kwargs):
Expand Down
46 changes: 46 additions & 0 deletions posthog/test/test_exception_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,49 @@ def test_never_raised_exception_stack_omits_sdk_frames_in_renamed_package():
functions = [frame["function"] for frame in exception["stacktrace"]["frames"]]
assert functions[-1] == "handler"
assert exception["mechanism"]["synthetic"] is True


def _captured_events(client_class, capture_handled, pass_exception):
from posthog.async_client import AsyncClient
from posthog.client import Client

clients = {"Client": Client, "AsyncClient": AsyncClient}
client = clients[client_class]("phc_test", send=False)
with patch.object(client, "capture", wraps=client.capture) as capture:
try:
capture_handled(client, pass_exception)
except ZeroDivisionError:
pass
return [call.args[0] for call in capture.call_args_list]


def _handled_then_reraised_in_context(client, pass_exception):
from posthog.contexts import new_context

with new_context(capture_exceptions=True, client=client):
try:
1 / 0
except ZeroDivisionError as error:
client.capture_exception(error if pass_exception else None)
raise


def _handled_twice(client, pass_exception):
try:
1 / 0
except ZeroDivisionError as error:
client.capture_exception(error if pass_exception else None)
client.capture_exception(error if pass_exception else None)


@pytest.mark.parametrize("client_class", ["Client", "AsyncClient"])
@pytest.mark.parametrize("pass_exception", [True, False])
@pytest.mark.parametrize(
"capture_handled", [_handled_then_reraised_in_context, _handled_twice]
)
def test_exception_from_sys_exc_info_is_captured_once(
client_class, capture_handled, pass_exception
):
assert _captured_events(client_class, capture_handled, pass_exception) == [
"$exception"
]
Loading