Skip to content

Commit a97fe0a

Browse files
authored
Use Sentry DSN to obtain project ID (#48)
1 parent a474fcf commit a97fe0a

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

posthog/sentry/posthog_integration.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from sentry_sdk._types import MYPY
2+
from sentry_sdk.client import Client
23
from sentry_sdk.hub import Hub
34
from sentry_sdk.integrations import Integration
45
from sentry_sdk.scope import add_global_event_processor
6+
from sentry_sdk.utils import Dsn
57

68
import posthog
79
from posthog.request import DEFAULT_HOST
@@ -17,7 +19,7 @@ class PostHogIntegration(Integration):
1719
identifier = "posthog-python"
1820
organization = None # The Sentry organization, used to send a direct link from PostHog to Sentry
1921
project_id = None # The Sentry project id, used to send a direct link from PostHog to Sentry
20-
prefix = "https://sentry.io/organizations/" # Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
22+
prefix = "https://sentry.io/organizations/" # URL of a hosted sentry instance (default: https://sentry.io/organizations/)
2123

2224
@staticmethod
2325
def setup_once():
@@ -37,10 +39,14 @@ def processor(event, hint):
3739
"$sentry_exception": event["exception"],
3840
}
3941

40-
if PostHogIntegration.organization and PostHogIntegration.project_id:
41-
properties[
42-
"$sentry_url"
43-
] = f"{PostHogIntegration.prefix}{PostHogIntegration.organization}/issues/?project={PostHogIntegration.project_id}&query={event['event_id']}"
42+
if PostHogIntegration.organization:
43+
project_id = PostHogIntegration.project_id or (
44+
not not Hub.current.client.dsn and Dsn(Hub.current.client.dsn).project_id
45+
)
46+
if project_id:
47+
properties[
48+
"$sentry_url"
49+
] = f"{PostHogIntegration.prefix}{PostHogIntegration.organization}/issues/?project={project_id}&query={event['event_id']}"
4450

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

sentry_django_example/sentry_django_example/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
from posthog.sentry.posthog_integration import PostHogIntegration
4545

4646
PostHogIntegration.organization = "posthog" # TODO: your sentry organization
47-
PostHogIntegration.project_id = "5624115" # TODO: your sentry projectID
4847
# PostHogIntegration.prefix = # TODO: your self hosted Sentry url. (default: https://sentry.io/organizations/)
4948

5049
# Since Sentry doesn't allow Integrations configuration (see https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/__init__.py#L171-L183)

0 commit comments

Comments
 (0)