Skip to content

Commit db565bc

Browse files
oliverb123daibhin
andauthored
fix(err): fix distinct_id, set personless and use a uuid (#144)
Co-authored-by: David Newell <[email protected]>
1 parent 8ae3f2b commit db565bc

File tree

6 files changed

+24
-23
lines changed

6 files changed

+24
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.7.3 - 2024-11-25
2+
3+
1. Use personless mode when sending an exception without a provided `distinct_id`.
4+
15
## 3.7.2 - 2024-11-19
26

37
1. Add `type` property to exception stacks.

posthog/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Callable, Dict, List, Optional, Tuple # noqa: F401
33

44
from posthog.client import Client
5-
from posthog.exception_capture import DEFAULT_DISTINCT_ID, Integrations # noqa: F401
5+
from posthog.exception_capture import Integrations # noqa: F401
66
from posthog.version import VERSION
77

88
__version__ = VERSION
@@ -289,7 +289,7 @@ def capture_exception(
289289
return _proxy(
290290
"capture_exception",
291291
exception=exception,
292-
distinct_id=distinct_id or DEFAULT_DISTINCT_ID,
292+
distinct_id=distinct_id,
293293
properties=properties,
294294
context=context,
295295
timestamp=timestamp,

posthog/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import os
55
import sys
66
from datetime import datetime, timedelta
7-
from uuid import UUID
7+
from uuid import UUID, uuid4
88

99
from dateutil.tz import tzutc
1010
from six import string_types
1111

1212
from posthog.consumer import Consumer
13-
from posthog.exception_capture import DEFAULT_DISTINCT_ID, ExceptionCapture
13+
from posthog.exception_capture import ExceptionCapture
1414
from posthog.exception_utils import exc_info_from_error, exceptions_from_error_tuple, handle_in_app
1515
from posthog.feature_flags import InconclusiveMatchError, match_feature_flag_properties
1616
from posthog.poller import Poller
@@ -362,7 +362,7 @@ def page(
362362
def capture_exception(
363363
self,
364364
exception=None,
365-
distinct_id=DEFAULT_DISTINCT_ID,
365+
distinct_id=None,
366366
properties=None,
367367
context=None,
368368
timestamp=None,
@@ -373,6 +373,13 @@ def capture_exception(
373373
# this is important to ensure we don't unexpectedly re-raise exceptions in the user's code.
374374
try:
375375
properties = properties or {}
376+
377+
# if there's no distinct_id, we'll generate one and set personless mode
378+
# via $process_person_profile = false
379+
if distinct_id is None:
380+
properties["$process_person_profile"] = False
381+
distinct_id = uuid4()
382+
376383
require("distinct_id", distinct_id, ID_TYPES)
377384
require("properties", properties, dict)
378385

@@ -385,7 +392,7 @@ def capture_exception(
385392
self.log.warning("No exception information available")
386393
return
387394

388-
# Format stack trace like sentry
395+
# Format stack trace for cymbal
389396
all_exceptions_with_trace = exceptions_from_error_tuple(exc_info)
390397

391398
# Add in-app property to frames in the exceptions

posthog/exception_capture.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ class Integrations(str, Enum):
1212
Django = "django"
1313

1414

15-
DEFAULT_DISTINCT_ID = "python-exceptions"
16-
17-
1815
class ExceptionCapture:
1916
# TODO: Add client side rate limiting to prevent spamming the server with exceptions
2017

@@ -61,14 +58,7 @@ def exception_receiver(self, exc_info, extra_properties):
6158

6259
def capture_exception(self, exception, metadata=None):
6360
try:
64-
# if hasattr(sys, "ps1"):
65-
# # Disable the excepthook for interactive Python shells
66-
# return
67-
68-
distinct_id = metadata.get("distinct_id") if metadata else DEFAULT_DISTINCT_ID
69-
# Make sure we have a distinct_id if its empty in metadata
70-
distinct_id = distinct_id or DEFAULT_DISTINCT_ID
71-
61+
distinct_id = metadata.get("distinct_id") if metadata else None
7262
self.client.capture_exception(exception, distinct_id)
7363
except Exception as e:
7464
self.log.exception(f"Failed to capture exception: {e}")

posthog/test/test_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ def test_basic_capture_exception(self):
104104
with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
105105
client = self.client
106106
exception = Exception("test exception")
107-
client.capture_exception(exception)
107+
client.capture_exception(exception, distinct_id="distinct_id")
108108

109109
self.assertTrue(patch_capture.called)
110110
capture_call = patch_capture.call_args[0]
111-
self.assertEqual(capture_call[0], "python-exceptions")
111+
self.assertEqual(capture_call[0], "distinct_id")
112112
self.assertEqual(capture_call[1], "$exception")
113113
self.assertEqual(
114114
capture_call[2],
@@ -123,7 +123,7 @@ def test_basic_capture_exception(self):
123123
"value": "test exception",
124124
}
125125
],
126-
"$exception_personURL": "https://us.i.posthog.com/project/random_key/person/python-exceptions",
126+
"$exception_personURL": "https://us.i.posthog.com/project/random_key/person/distinct_id",
127127
},
128128
)
129129

@@ -218,11 +218,11 @@ def test_basic_capture_exception_with_no_exception_given(self):
218218
try:
219219
raise Exception("test exception")
220220
except Exception:
221-
client.capture_exception()
221+
client.capture_exception(distinct_id="distinct_id")
222222

223223
self.assertTrue(patch_capture.called)
224224
capture_call = patch_capture.call_args[0]
225-
self.assertEqual(capture_call[0], "python-exceptions")
225+
self.assertEqual(capture_call[0], "distinct_id")
226226
self.assertEqual(capture_call[1], "$exception")
227227
self.assertEqual(capture_call[2]["$exception_type"], "Exception")
228228
self.assertEqual(capture_call[2]["$exception_message"], "test exception")

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.7.2"
1+
VERSION = "3.7.3"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)