Skip to content

fix(gql): Scope the event processor to the query - #8307

Merged
sentrivana merged 2 commits into
getsentry:masterfrom
Mohil-Ahuja:fix/gql-scope-event-processor
Nov 18, 2026
Merged

sentrivana merged 2 commits into
getsentry:masterfrom
Mohil-Ahuja:fix/gql-scope-event-processor

Conversation

@Mohil-Ahuja

Copy link
Copy Markdown
Contributor

Fixes #8262

sentry_patched_execute added its event processor to the isolation scope on every Client.execute() and never removed it. Outside a per-request isolation scope (scripts, workers), or for many queries in one request, that meant:

  • errors captured after a query finished were sent with request.api_target: graphql and the last query attached;
  • after more than 20 queries the isolation scope went over its processor limit and dropped all its processors, including ones added by the app or other integrations.

The processor is now added to a scope forked with sentry_sdk.new_scope() around real_execute, so it applies to events captured during the query (including the TransportQueryError event) and is gone afterwards.

This also fixes contexts.response.type, which was set to the response dict itself and serialized as "<cyclic>". It is now "response".

Tests:

  • test_event_processor_does_not_outlive_query: a message captured after a successful query has no request or response data.
  • test_queries_do_not_remove_other_event_processors: a processor added to the isolation scope still runs after 25 queries.
  • test_response_context_type: contexts.response.type is "response".

All three fail on master. The gql test module passes with gql 4.4.0 and 3.4.1.

The event processor was added to the isolation scope on every
execute() and never removed, so later unrelated events got the last
query's request data, and more than 20 queries made the scope drop
all of its processors. Add it to a forked scope instead.

Also set contexts.response.type to "response" instead of the
response dict itself.

Fixes getsentry#8262

@sentrivana sentrivana left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good to me, but please check out my comment on the tests.

Comment thread tests/integrations/gql/test_gql.py Outdated
Comment on lines +378 to +406
def test_queries_do_not_remove_other_event_processors(sentry_init, capture_events):
"""
Running many queries must not fill up the isolation scope's event
processors, which would make the scope drop processors added by others.
"""
sentry_init(integrations=[GQLIntegration()])
events = capture_events()

def add_tag(event, hint):
event.setdefault("tags", {})["other_processor"] = "ran"
return event

sentry_sdk.get_isolation_scope().add_event_processor(add_tag)

for _ in range(25):
_execute_mock_query({"data": {"example": "This is the example"}})

sentry_sdk.capture_message("unrelated")

(event,) = events
assert event["tags"]["other_processor"] == "ran"


def test_response_context_type(sentry_init, capture_events):
sentry_init(integrations=[GQLIntegration()], send_default_pii=True)

event = _make_erroneous_query(capture_events, _execute_mock_query)

assert event["contexts"]["response"]["type"] == "response"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these two tests please. The first one is just testing a side-effect of the previous incorrect behavior, which we're now already testing in test_event_processor_does_not_outlive_query. The second one doesn't warrant its own test case. Can we add the assertion (assert event["contexts"]["response"]["type"] == "response") to an existing test instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8cbc64d: removed both tests and added the type assertion to test_real_gql_request_with_error_with_pii.

Drop the processor-count test, which only covered a side effect of the
old behavior, and move the response type assertion into
test_real_gql_request_with_error_with_pii.

@sentrivana sentrivana left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sentrivana
sentrivana marked this pull request as ready for review November 18, 2026 10:44
@sentrivana
sentrivana requested a review from a team as a code owner November 18, 2026 10:44
@sentrivana
sentrivana enabled auto-merge (squash) November 18, 2026 10:44
@sentrivana
sentrivana merged commit e607c3d into getsentry:master Nov 18, 2026
178 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gql: execute() leaves its event processor on the isolation scope, so later unrelated errors are reported as GraphQL requests

2 participants