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

Add python sentry (and sentry & django) integrations #13

Merged
merged 7 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions posthog/sentry/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import posthog
from posthog.request import DEFAULT_HOST

def update_posthog(event, _hint):
if event.get('tags').get("posthog_distinct_id"):
event['tags']["PostHog URL"] = f"{posthog.host or DEFAULT_HOST}/person/{event['tags']['posthog_distinct_id']}"
return event
24 changes: 24 additions & 0 deletions posthog/sentry/django.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sentry_sdk import configure_scope
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# settings.py
middlewares += ['ourSentryMiddleWare']

POSTHOG_DJANGO = {
  distinct_id: lambda request: request.user.id
}

from django.conf import settings

GET_DISTINCT_ID = getattr(settings, 'POSTHOG_DJANGO', {}).get('distinct_id')

def get_distinct_id(request):
if not GET_DISTINCT_ID:
return None
try:
return GET_DISTINCT_ID(request)
except:
return None

class PosthogDistinctIdMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
with configure_scope() as scope:
distinct_id = get_distinct_id(request)
if distinct_id:
scope.set_tag('posthog_distinct_id', distinct_id)
response = self.get_response(request)
return response
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
license='MIT License',
install_requires=install_requires,
tests_require=tests_require,
extras_require={
'sentry': 'sentry',
'django': 'django',
},
description='Integrate PostHog into any python application.',
long_description=long_description,
classifiers=[
Expand Down
4 changes: 4 additions & 0 deletions setup_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
license='MIT License',
install_requires=install_requires,
tests_require=tests_require,
extras_require={
'sentry': 'sentry',
'django': 'django',
},
description='Integrate PostHog into any python application.',
long_description=long_description,
classifiers=[
Expand Down