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

capture user/org tags in sentry #449

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions commcare_connect/utils/middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf import settings
from django.contrib import messages
from django.core.exceptions import MiddlewareNotUsed
from django.http import HttpResponseRedirect
from django.utils.safestring import mark_safe
from rest_framework.settings import api_settings
Expand Down Expand Up @@ -40,3 +42,29 @@ def __call__(self, request):
def process_view(self, request, view_func, view_args, view_kwargs):
if hasattr(view_func, "cls") and view_func.cls.versioning_class is not None:
request.include_version_headers = True


class SentryContextMiddleware:
"""Add details to Sentry context.
Should be placed after '"commcare_connect.users.middleware.OrganizationMiddleware",'
"""

def __init__(self, get_response):
self.get_response = get_response
try:
from sentry_sdk import Scope # noqa: F401
except ImportError:
raise MiddlewareNotUsed

if not getattr(settings, "SENTRY_DSN", None):
raise MiddlewareNotUsed

def process_view(self, request, view_func, view_args, view_kwargs):
from sentry_sdk import configure_scope

with configure_scope() as scope:
Copy link
Collaborator

@calellowitz calellowitz Dec 10, 2024

Choose a reason for hiding this comment

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

Why use this pattern instead of the one shown in the docs using get_current_scope or just sentry_sdk.set_tag? Is there a difference between them. I can't actually find this one in the docs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

On further look, it looks like this function is deprecated https://docs.sentry.io/platforms/python/migration/1.x-to-2.x#custom-instrumentation

if getattr(request, "user", None):
scope.set_user("username", request.user.username)

if getattr(request, "org", None):
scope.set_tag("org", request.org)
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"commcare_connect.utils.middleware.CustomErrorHandlingMiddleware",
"commcare_connect.utils.middleware.CurrentVersionMiddleware",
"commcare_connect.utils.middleware.SentryContextMiddleware",
]

# STATIC
Expand Down
Loading