Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(err): fix distinct_id, set personless and use a uuid #144

Merged
merged 9 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def page(
def capture_exception(
self,
exception=None,
distinct_id=DEFAULT_DISTINCT_ID,
oliverb123 marked this conversation as resolved.
Show resolved Hide resolved
distinct_id=None,
properties=None,
context=None,
timestamp=None,
Expand All @@ -385,7 +385,7 @@ def capture_exception(
self.log.warning("No exception information available")
return

# Format stack trace like sentry
# Format stack trace for cymbal
all_exceptions_with_trace = exceptions_from_error_tuple(exc_info)

# Add in-app property to frames in the exceptions
Expand Down
16 changes: 9 additions & 7 deletions posthog/exception_capture.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import sys
import threading
import uuid
from enum import Enum
from typing import TYPE_CHECKING, List, Optional

Expand Down Expand Up @@ -61,14 +62,15 @@ def exception_receiver(self, exc_info, extra_properties):

def capture_exception(self, exception, metadata=None):
try:
# if hasattr(sys, "ps1"):
# # Disable the excepthook for interactive Python shells
# return
distinct_id = metadata.get("distinct_id") if metadata else None

distinct_id = metadata.get("distinct_id") if metadata else DEFAULT_DISTINCT_ID
# Make sure we have a distinct_id if its empty in metadata
distinct_id = distinct_id or DEFAULT_DISTINCT_ID
# if there's no distinct_id, we'll generate one and set personless mode
# via $process_person_profile = false
properties = {}
if distinct_id is None:
properties["$process_person_profile"] = False
distinct_id = uuid.uuid4()
oliverb123 marked this conversation as resolved.
Show resolved Hide resolved

self.client.capture_exception(exception, distinct_id)
self.client.capture_exception(exception, distinct_id, properties)
except Exception as e:
self.log.exception(f"Failed to capture exception: {e}")
2 changes: 0 additions & 2 deletions posthog/exception_integrations/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class DjangoIntegration:
identifier = "django"

def __init__(self, capture_exception_fn=None):

if DJANGO_VERSION < (4, 2):
raise IntegrationEnablingError("Django 4.2 or newer is required.")

Expand Down Expand Up @@ -55,7 +54,6 @@ def uninstall(self):


class DjangoRequestExtractor:

def __init__(self, request):
# type: (Any) -> None
self.request = request
Expand Down
1 change: 0 additions & 1 deletion posthog/exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@


if TYPE_CHECKING:

from types import FrameType, TracebackType
from typing import ( # noqa: F401
Any,
Expand Down
6 changes: 3 additions & 3 deletions posthog/sentry/posthog_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def processor(event, hint):
not not Hub.current.client.dsn and Dsn(Hub.current.client.dsn).project_id
)
if project_id:
properties["$sentry_url"] = (
f"{PostHogIntegration.prefix}{PostHogIntegration.organization}/issues/?project={project_id}&query={event['event_id']}"
)
properties[
"$sentry_url"
] = f"{PostHogIntegration.prefix}{PostHogIntegration.organization}/issues/?project={project_id}&query={event['event_id']}"

posthog.capture(posthog_distinct_id, "$exception", properties)

Expand Down
7 changes: 0 additions & 7 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def test_basic_super_properties(self):
self.assertEqual(msg["properties"]["source"], "repo-name")

def test_basic_capture_exception(self):

with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
client = self.client
exception = Exception("test exception")
Expand Down Expand Up @@ -128,7 +127,6 @@ def test_basic_capture_exception(self):
)

def test_basic_capture_exception_with_distinct_id(self):

with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
client = self.client
exception = Exception("test exception")
Expand Down Expand Up @@ -156,7 +154,6 @@ def test_basic_capture_exception_with_distinct_id(self):
)

def test_basic_capture_exception_with_correct_host_generation(self):

with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, host="https://aloha.com")
exception = Exception("test exception")
Expand Down Expand Up @@ -184,7 +181,6 @@ def test_basic_capture_exception_with_correct_host_generation(self):
)

def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(self):

with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, host="https://app.posthog.com")
exception = Exception("test exception")
Expand Down Expand Up @@ -212,7 +208,6 @@ def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(s
)

def test_basic_capture_exception_with_no_exception_given(self):

with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
client = self.client
try:
Expand Down Expand Up @@ -249,10 +244,8 @@ def test_basic_capture_exception_with_no_exception_given(self):
self.assertEqual(capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0]["in_app"], True)

def test_basic_capture_exception_with_no_exception_happening(self):

with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
with self.assertLogs("posthog", level="WARNING") as logs:

client = self.client
client.capture_exception()

Expand Down
Loading