Skip to content

perf(control): Decide request-only routing before the integration lookups - #123298

Merged
vaind merged 5 commits into
masterfrom
ivandlugos/perf/github-webhook-drop-before-lookup
Sep 2, 2026
Merged

vaind merged 5 commits into
masterfrom
ivandlugos/perf/github-webhook-drop-before-lookup

Conversation

@vaind

@vaind vaind commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Anything decidable from the request alone belongs above the integration and cell queries. Four places broke that rule.

  • The shed killswitch, on every provider — the one that matters most. Each enqueueing parser evaluated it only inside 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 no integration_id matches on the provider alone, so github, gitlab, msteams and bitbucket now check it as soon as they know the request will be enqueued; jira_server uses the id it already holds from its token. Integration-scoped conditions can't match that context and are still evaluated later.
  • GitHub's drop filters — they read the event header and the parsed body and nothing else. Moved verbatim into _get_drop_response().
  • MSTeams — installationUpdate is decided from the payload, so it no longer resolves an integration and cells first. Reachable via uninstalls; an add is caught by the new-installation check above it.
  • jira_server's changelog check — now above the org and cell lookups, still below the token lookup so its log can name the integration.

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. vsts and jira are untouched for that reason — in both, "this will be enqueued" is only known inside get_integration_from_request, which validates and queries together. Slack is exempt too: self.slack_request is 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_event picks those up (a step in the counter, not in traffic), and ORG_INTEGRATION_DOES_NOT_EXIST stops 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

… 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.
@vaind
vaind requested a review from a team as a code owner September 1, 2026 08:22
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Sep 1, 2026
@linear-code

linear-code Bot commented Sep 1, 2026

Copy link
Copy Markdown

CW-1892

… 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.
@vaind vaind changed the title perf(control): Drop unconsumed GitHub webhooks before the integration lookup perf(control): Decide request-only routing before the integration lookups Sep 1, 2026
…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.
@vaind
vaind requested review from a team and joseph-sentry September 1, 2026 11:57

@joseph-sentry joseph-sentry left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems like targeted_shed_conditions is unused? otherwise lgtm

edit: targeted_shed_conditions is a cache field, i thought it was meant to be an override defined on the subclasses

…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
vaind merged commit 21b09f4 into master Sep 2, 2026
68 checks passed
@vaind
vaind deleted the ivandlugos/perf/github-webhook-drop-before-lookup branch September 2, 2026 18:38
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

1 active deployment
Preview — 1da085ee Deployed Sep 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants