Skip to content

ref: Ariadne integration new scope API #2850

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

Merged
merged 1 commit into from
Mar 19, 2024
Merged
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
61 changes: 25 additions & 36 deletions sentry_sdk/integrations/ariadne.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from importlib import import_module

from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk import get_client, capture_event
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.integrations.logging import ignore_logger
from sentry_sdk.integrations._wsgi_common import request_body_within_bounds
from sentry_sdk.scope import Scope, should_send_default_pii
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
package_version,
)
Expand Down Expand Up @@ -51,73 +53,60 @@ def _patch_graphql():
old_handle_errors = ariadne_graphql.handle_graphql_errors
old_handle_query_result = ariadne_graphql.handle_query_result

@ensure_integration_enabled(AriadneIntegration, old_parse_query)
def _sentry_patched_parse_query(context_value, query_parser, data):
# type: (Optional[Any], Optional[QueryParser], Any) -> DocumentNode
hub = Hub.current
integration = hub.get_integration(AriadneIntegration)
if integration is None:
return old_parse_query(context_value, query_parser, data)

with hub.configure_scope() as scope:
event_processor = _make_request_event_processor(data)
scope.add_event_processor(event_processor)
event_processor = _make_request_event_processor(data)
Scope.get_isolation_scope().add_event_processor(event_processor)

result = old_parse_query(context_value, query_parser, data)
return result

@ensure_integration_enabled(AriadneIntegration, old_handle_errors)
def _sentry_patched_handle_graphql_errors(errors, *args, **kwargs):
# type: (List[GraphQLError], Any, Any) -> GraphQLResult
hub = Hub.current
integration = hub.get_integration(AriadneIntegration)
if integration is None:
return old_handle_errors(errors, *args, **kwargs)

result = old_handle_errors(errors, *args, **kwargs)

with hub.configure_scope() as scope:
event_processor = _make_response_event_processor(result[1])
scope.add_event_processor(event_processor)
event_processor = _make_response_event_processor(result[1])
Scope.get_isolation_scope().add_event_processor(event_processor)

if hub.client:
client = get_client()
if client.is_active():
with capture_internal_exceptions():
for error in errors:
event, hint = event_from_exception(
error,
client_options=hub.client.options,
client_options=client.options,
mechanism={
"type": integration.identifier,
"type": AriadneIntegration.identifier,
"handled": False,
},
)
hub.capture_event(event, hint=hint)
capture_event(event, hint=hint)

return result

@ensure_integration_enabled(AriadneIntegration, old_handle_query_result)
def _sentry_patched_handle_query_result(result, *args, **kwargs):
# type: (Any, Any, Any) -> GraphQLResult
hub = Hub.current
integration = hub.get_integration(AriadneIntegration)
if integration is None:
return old_handle_query_result(result, *args, **kwargs)

query_result = old_handle_query_result(result, *args, **kwargs)

with hub.configure_scope() as scope:
event_processor = _make_response_event_processor(query_result[1])
scope.add_event_processor(event_processor)
event_processor = _make_response_event_processor(query_result[1])
Scope.get_isolation_scope().add_event_processor(event_processor)

if hub.client:
client = get_client()
if client.is_active():
with capture_internal_exceptions():
for error in result.errors or []:
event, hint = event_from_exception(
error,
client_options=hub.client.options,
client_options=client.options,
mechanism={
"type": integration.identifier,
"type": AriadneIntegration.identifier,
"handled": False,
},
)
hub.capture_event(event, hint=hint)
capture_event(event, hint=hint)

return query_result

Expand All @@ -143,8 +132,8 @@ def inner(event, hint):
except (TypeError, ValueError):
return event

if _should_send_default_pii() and request_body_within_bounds(
Hub.current.client, content_length
if should_send_default_pii() and request_body_within_bounds(
get_client(), content_length
):
request_info = event.setdefault("request", {})
request_info["api_target"] = "graphql"
Expand All @@ -165,7 +154,7 @@ def _make_response_event_processor(response):
def inner(event, hint):
# type: (Event, dict[str, Any]) -> Event
with capture_internal_exceptions():
if _should_send_default_pii() and response.get("errors"):
if should_send_default_pii() and response.get("errors"):
contexts = event.setdefault("contexts", {})
contexts["response"] = {
"data": response,
Expand Down