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

ref: Ariadne integration new scope API #2850

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 @@
old_handle_errors = ariadne_graphql.handle_graphql_errors
old_handle_query_result = ariadne_graphql.handle_query_result

@ensure_integration_enabled(AriadneIntegration, old_parse_query)

Check warning on line 56 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L56

Added line #L56 was not covered by tests
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)

Check warning on line 60 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L59-L60

Added lines #L59 - L60 were not covered by tests

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

@ensure_integration_enabled(AriadneIntegration, old_handle_errors)

Check warning on line 65 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L65

Added line #L65 was not covered by tests
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)

Check warning on line 71 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L70-L71

Added lines #L70 - L71 were not covered by tests

if hub.client:
client = get_client()

Check warning on line 73 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L73

Added line #L73 was not covered by tests
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)

Check warning on line 85 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L85

Added line #L85 was not covered by tests

return result

@ensure_integration_enabled(AriadneIntegration, old_handle_query_result)

Check warning on line 89 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L89

Added line #L89 was not covered by tests
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)

Check warning on line 95 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L94-L95

Added lines #L94 - L95 were not covered by tests

if hub.client:
client = get_client()

Check warning on line 97 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L97

Added line #L97 was not covered by tests
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)

Check warning on line 109 in sentry_sdk/integrations/ariadne.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/ariadne.py#L109

Added line #L109 was not covered by tests

return query_result

Expand All @@ -143,8 +132,8 @@
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 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
Loading