11from sentry_sdk ._types import MYPY
2+ from sentry_sdk .client import Client
23from sentry_sdk .hub import Hub
34from sentry_sdk .integrations import Integration
45from sentry_sdk .scope import add_global_event_processor
6+ from sentry_sdk .utils import Dsn
57
68import posthog
79from 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
0 commit comments