Skip to content
Merged
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
35 changes: 19 additions & 16 deletions sentry_sdk/integrations/gql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -200,7 +203,7 @@ def processor(event: "Event", hint: "dict[str, Any]") -> "Event":
response.update(
{
"data": {"errors": errors},
"type": response,
"type": "response",
}
)

Expand All @@ -221,7 +224,7 @@ def processor(event: "Event", hint: "dict[str, Any]") -> "Event":
response.update(
{
"data": {"errors": errors},
"type": response,
"type": "response",
}
)

Expand Down
18 changes: 18 additions & 0 deletions tests/integrations/gql/test_gql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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", {})
Loading