Repository navigation
perf(control): Decide request-only routing before the integration lookups - #123298
Merged
Merged
Conversation
… lookup The three drop filters read the X-GitHub-Event header and the request body and nothing else, but they sat below get_integration_from_request() and get_cells_from_organizations(). Every webhook we were about to discard still paid for resolving the integration and the cells it would never be routed to — and the filters discard roughly half of inbound GitHub traffic. Hoisting them takes that half off those queries entirely. The filters move into _get_drop_response() so get_response() still reads as one pass. A droppable event for an unknown or removed integration now returns 202 where it returned 400. No delivery is lost: neither ordering stored a payload for these, so the only change is that GitHub is no longer told a webhook we never process failed to deliver. The ORG_INTEGRATION_DOES_NOT_EXIST halt stops being recorded for that slice, which is the population we just decided not to route; orgs with live activity still trip it on their forwarded events.
… lookups Follows the same rule as the drop filters: anything decidable from the request alone belongs above the integration and cell queries, whatever its current share of traffic. A shed condition that names no integration_id sheds a whole provider, so it matches on the provider alone. Checking it before the lookups matters more than the drop reorder does — shedding is break-glass for a flood, and it was waiting on three queries per request to take effect. Conditions that do name an integration_id cannot match a None context, so those are still evaluated after the lookup. Counting ignored shed conditions moves into a per-parser memo. It is a property of the killswitch config rather than of any one check, and GitHub now consults the killswitch three times per webhook. MSTeams decides installationUpdate from the payload and hands it to control, so it no longer resolves an integration and its cells first. Reachable via uninstalls: an "add" is already answered by the new-installation check above it. Jira looked like the same shape but is not. Every URL in its urlconf maps to a class in one of the three view lists, so the trailing control-silo fallthrough is unreachable and hoisting it would only add a dead branch.
…verywhere The shed killswitch is not GitHub's. Every parser that enqueues a WebhookPayload evaluated it only inside get_response_from_webhookpayload, which is after the integration, org-integration and org-mapping queries — so on every provider the break-glass for a flood was waiting on the queries it exists to avoid. A condition naming no integration_id sheds a whole provider and matches on the provider alone, so gitlab, msteams and bitbucket now check it as soon as they know the request will be enqueued. jira_server checks it with the id it already holds from the token, before resolving organizations and cells. Placement is below each parser's own request validation, not above it. The check costs an option read, and an invalid request that would have been rejected without touching anything should not start paying for one. vsts and jira are left alone for the same reason: in both, the point where "this will be enqueued" is known is inside get_integration_from_request, which validates and queries together, so an early check could only precede their validation. Confirmed by test_handles_missing_integration, which caught an earlier attempt turning an unsigned Jira request into a DB read. jira_server's changelog check reads only the body and now sits above the organization and cell lookups, though still below the token lookup so the log line can still name the integration that sent it.
joseph-sentry
approved these changes
Sep 2, 2026
…hub-webhook-drop-before-lookup # Conflicts: # src/sentry/middleware/integrations/parsers/jira_server.py # tests/sentry/middleware/integrations/parsers/test_github.py # tests/sentry/middleware/integrations/parsers/test_gitlab.py
vaind
added a commit
that referenced
this pull request
Sep 2, 2026
The base branch (#123298) was squash-merged, so its commits reach master as one new commit that shares no history with the copies still on this branch. Every hunk they both touch conflicts even where the two sides agree. Resolutions: - github.py `_get_drop_response`: took master. The two sides differ only in the `github_drop_checks_without_own_repo_pr` gate, which master deleted in #122581 once the behaviour was baselined; keeping this branch's copy would resurrect an option that no longer exists. - github.py `get_response`: kept `integration_for_request()`. Routing through the memo is what this branch is for. - parser.py `__init__`: kept the `_integration` / `_integration_fetched` fields backing that memo. - jira_server.py: took master's `get_request_body()` over the inlined `orjson.loads`. Same parse, plus the non-dict guard, in the shared helper. - test_gitlab.py: took master's added imports; this side added none. - test_github.py: both sides carried byte-identical copies of the four drop/shed tests, and git kept both. Dropped the second copy. Co-Authored-By: Claude Opus 5 <[email protected]>
vaind
added a commit
that referenced
this pull request
Sep 2, 2026
…e per webhook (#123299) > Stacked on #123298 — review that first. Routing a webhook read three tables more than it needed to, in the shared base, so every provider paid for it. The integration was fetched and Fernet-decrypted twice (1.97 reads per request on the GitHub path) because `get_response` and the base's org lookup each resolved it independently; org integrations were counted and then fetched when only the ids are used; and cells were resolved by re-querying org mappings for `cell_name`, a column already carried on the `RpcOrganizationMapping` objects just fetched. Together roughly a third of the DB span time on the control-side GitHub webhook transaction. - `integration_for_request()` memoizes for the life of the parser, which is one request. The subclass hook stays uncached; the parsers that called it directly now go through the memo, which is what makes the dedup real rather than moving the second query. It needs the paired `_integration_fetched` flag because `None` is a legitimate cached result. - `get_organizations_from_integration` uses one `values_list("organization_id", flat=True)` instead of count-then-fetch. - `find_cells_for_org_mappings()` reads `cell_name` off mappings the caller already holds. It repeats the monolith short-circuit rather than reading the field — `SENTRY_FALLBACK_CELL` is the synthetic `--monolith--` and appears in no mapping row, so inlining the comprehension would pass in prod and silently break monolith. `find_cells_for_orgs` stays for id-only callers. - `GitlabRequestParser` loses its own integration cache, which the base memo now covers. Its version was a class-level mutable default and only cached hits, so a webhook with no matching integration re-ran the lookup on every call. `jira_server` still passes its integration explicitly: it resolves one from a URL token rather than from the request, so the memo cannot supply it. No behaviour change, which is why this needs query-count assertions. `test_forwarding_reads_each_routing_table_once` (GitHub) asserts one read each of the three tables and `test_forwarding_reads_the_integration_once` (GitLab) covers the parser that had its own cache — both verified to fail when the memo wiring is undone. `test_find_cells_for_org_mappings` covers the monolith branch a siloed run would not reach. CW-1892 --------- Co-authored-by: Claude Opus 5 <[email protected]>
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Anything decidable from the request alone belongs above the integration and cell queries. Four places broke that rule.
get_response_from_webhookpayload, after all three queries, so the break-glass for a flood was waiting on the queries it exists to avoid. A condition naming nointegration_idmatches on the provider alone, sogithub,gitlab,msteamsandbitbucketnow check it as soon as they know the request will be enqueued;jira_serveruses the id it already holds from its token. Integration-scoped conditions can't match that context and are still evaluated later._get_drop_response().installationUpdateis decided from the payload, so it no longer resolves an integration and cells first. Reachable via uninstalls; anaddis caught by the new-installation check above it.Placement is below each parser's own validation, not above it: the check costs an option read, and an invalid request that would have been rejected without touching anything shouldn't pay for one.
vstsandjiraare untouched for that reason — in both, "this will be enqueued" is only known insideget_integration_from_request, which validates and queries together. Slack is exempt too:self.slack_requestis produced by the lookup.Behaviour change: a droppable GitHub event for an unknown or removed integration now returns 202 instead of 400. No delivery is lost — neither ordering stored a payload — so GitHub is just no longer told that a webhook we never process failed.
drop_unprocessed_eventpicks those up (a step in the counter, not in traffic), andORG_INTEGRATION_DOES_NOT_EXISTstops firing for the dropped share; orgs with live activity still trip it on forwarded events.Counting ignored shed conditions moved to a per-parser memo — it's a property of the config, and GitHub now consults the killswitch three times per webhook.
Six new tests, each verified to fail on master.
CW-1892