-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
import json | ||
|
||
from asgiref.sync import iscoroutinefunction | ||
from django.urls import reverse | ||
from sentry_sdk import Scope | ||
|
||
|
||
class SentryTransactionMiddleware: | ||
graphql_url = reverse("graphql") | ||
|
||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
if request.path == self.graphql_url: | ||
operation_type = "Query" | ||
operation_name = "Unknown" | ||
try: | ||
body = request.body.decode("utf-8") | ||
if body: | ||
# XXX: This will be repeated by Strawberry as well. | ||
data = json.loads(body) | ||
operation_name = data.get("operationName", operation_name) | ||
if data.get("query", "").startswith("mutation"): | ||
operation_type = "Mutation" | ||
except Exception: | ||
... | ||
|
||
scope = Scope.get_current_scope() | ||
scope.set_transaction_name(f"GraphQL/{operation_type}/{operation_name}") | ||
|
||
return self.get_response(request) | ||
from django.utils.decorators import sync_and_async_middleware | ||
|
||
from main.sentry import SentryTransactionMiddlewareHelper | ||
|
||
|
||
@sync_and_async_middleware | ||
def sentry_middleware(get_response): | ||
from django.conf import settings | ||
|
||
# One-time configuration and initialization goes here. | ||
graphql_urls = set([reverse("graphql")]) | ||
if settings.DEBUG: | ||
graphql_urls.add(reverse("graphiql")) | ||
|
||
if iscoroutinefunction(get_response): | ||
|
||
async def amiddleware(request): | ||
if settings.SENTRY_ENABLED: | ||
await SentryTransactionMiddlewareHelper.atrack_transaction(graphql_urls, request) | ||
response = await get_response(request) | ||
return response | ||
|
||
return amiddleware | ||
|
||
def middleware(request): | ||
if settings.SENTRY_ENABLED: | ||
SentryTransactionMiddlewareHelper.track_transaction(graphql_urls, request) | ||
response = get_response(request) | ||
return response | ||
|
||
return middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.