diff --git a/sentry_sdk/integrations/gql.py b/sentry_sdk/integrations/gql.py index a05eeaa202..0b5e1e7c2d 100644 --- a/sentry_sdk/integrations/gql.py +++ b/sentry_sdk/integrations/gql.py @@ -144,22 +144,25 @@ def sentry_patched_execute( *args: "Any", **kwargs: "Any", ) -> "Any": - scope = sentry_sdk.get_isolation_scope() - # document is a gql.GraphQLRequest with gql v4.0.0. - scope.add_event_processor(_make_gql_event_processor(self, document)) - - try: + # Add the processor to a scope that lives only as long as this + # query, so that it doesn't apply to unrelated events captured + # later on the same isolation scope. + with sentry_sdk.new_scope() as scope: # document is a gql.GraphQLRequest with gql v4.0.0. - return real_execute(self, document, *args, **kwargs) - except TransportQueryError as e: - event, hint = event_from_exception( - e, - client_options=sentry_sdk.get_client().options, - mechanism={"type": "gql", "handled": False}, - ) + scope.add_event_processor(_make_gql_event_processor(self, document)) + + try: + # document is a gql.GraphQLRequest with gql v4.0.0. + return real_execute(self, document, *args, **kwargs) + except TransportQueryError as e: + event, hint = event_from_exception( + e, + client_options=sentry_sdk.get_client().options, + mechanism={"type": "gql", "handled": False}, + ) - sentry_sdk.capture_event(event, hint) - raise e + sentry_sdk.capture_event(event, hint) + raise e gql.Client.execute = sentry_patched_execute @@ -200,7 +203,7 @@ def processor(event: "Event", hint: "dict[str, Any]") -> "Event": response.update( { "data": {"errors": errors}, - "type": response, + "type": "response", } ) @@ -221,7 +224,7 @@ def processor(event: "Event", hint: "dict[str, Any]") -> "Event": response.update( { "data": {"errors": errors}, - "type": response, + "type": "response", } ) diff --git a/tests/integrations/gql/test_gql.py b/tests/integrations/gql/test_gql.py index 9c0c1c94ed..13fc2177df 100644 --- a/tests/integrations/gql/test_gql.py +++ b/tests/integrations/gql/test_gql.py @@ -6,6 +6,7 @@ from gql.transport.exceptions import TransportQueryError from gql.transport.requests import RequestsHTTPTransport +import sentry_sdk from sentry_sdk.integrations.gql import GQLIntegration from sentry_sdk.utils import parse_version @@ -173,6 +174,7 @@ def test_real_gql_request_with_error_with_pii( assert "data" in event["request"] assert "response" in event["contexts"] + assert event["contexts"]["response"]["type"] == "response" @pytest.mark.parametrize("execute_query", _execute_query_funcs) @@ -356,3 +358,19 @@ def test_transport_url_is_sanitized( assert event["request"].get("query_string") == expected_query_string assert "password" not in str(event["request"]) assert "secret" not in str(event["request"]) + + +def test_event_processor_does_not_outlive_query(sentry_init, capture_events): + """ + Events captured after a query has finished must not get the query's + request data. + """ + sentry_init(integrations=[GQLIntegration()], send_default_pii=True) + events = capture_events() + + _execute_mock_query({"data": {"example": "This is the example"}}) + sentry_sdk.capture_message("unrelated") + + (event,) = events + assert "request" not in event + assert "response" not in event.get("contexts", {})