Skip to content

feat(api): Add opt-in max-age mode to ConditionalGetResponseMixin - #128828

Merged
BYK merged 2 commits into
masterfrom
feat/api-max-age-event-responses
Nov 2, 2026
Merged

BYK merged 2 commits into
masterfrom
feat/api-max-age-event-responses

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Prototype from the #128818 spike. Lets an endpoint that serves data that rarely changes once written tell the browser it can reuse the response for N seconds, instead of revalidating every time.

What changes

  • ConditionalGetResponseMixin gets a cache_max_age class attribute. When it is set and the endpoint class is listed in the new api.conditional_get.max_age_endpoints option, 200 GET/HEAD responses send Cache-Control: private, max-age=N and Vary: Cookie, Authorization, Origin. The ETag and 304 path stay, so a browser revalidates cheaply after the max-age runs out.
  • Origin is added to Vary only in this mode. API responses reflect the request origin in Access-Control-Allow-Origin (src/sentry/api/base.py). A response reused without revalidation keeps the origin it was fetched with, so a page on another origin, such as sentry.io versus <org>.sentry.io, would fail CORS.
  • Active superuser and staff sessions fall back to private, no-cache. They see extra fields, for example internal trace item attributes, and the session can end without the cookie changing.
  • uncacheable_query_params falls back to no-cache for query params that pull in live data. averageColumn on organization event details adds 24h span averages.
  • The api.conditional_get metric gets a mode tag (max_age / no_cache).
  • Opted in with cache_max_age = 300: OrganizationEventDetailsEndpoint and ProjectTraceItemDetailsEndpoint.

Rollout and rollback

Nothing changes until both options are set. api.conditional_get.enabled is the existing kill switch for the mixin. The new api.conditional_get.max_age_endpoints defaults to [], and endpoints are added to it one at a time. Rollback means removing the endpoint from the list. Browsers keep what they already cached for up to cache_max_age seconds, which is why it starts at 5 minutes.

Not immutable on purpose. Reprocessing, merges, release updates, deletion, and permission changes can all change or revoke a response under the same URL. Details are in the spike comment on #128818.

Tests run locally

  • .venv/bin/pytest -q --reuse-db tests/sentry/api/test_conditional_get.py tests/sentry/api/endpoints/test_project_trace_item_details.py tests/snuba/api/endpoints/test_organization_event_details.py tests/snuba/api/endpoints/test_project_trace_item_details.py tests/sentry/seer/endpoints/test_group_ai_autofix.py: 165 passed, 1 xpassed. Snuba came from devservices up.
  • .venv/bin/prek run -q and the pre-push mypy hook on the changed files both passed.

Refs #128818

via BYK.

--

View Junior Session in Sentry

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 26, 2026
@BYK
BYK marked this pull request as ready for review October 26, 2026 21:43
@BYK
BYK requested review from a team as code owners October 26, 2026 21:43
@BYK

BYK commented Oct 26, 2026

Copy link
Copy Markdown
Member

This LGTM but waiting for other folks to sign off as it is sensitive.

BYK added a commit to getsentry/toolkit that referenced this pull request Oct 27, 2026
The CLI's fallback "immutable" cache tier kept any `/events/<id>/`,
`/trace/<id>/`, or `/trace-items/<id>/` response for 24 hours. It was
also checked before the `/issues/` volatile pattern, so `getLatestEvent`
(`/issues/<id>/events/latest/`) was cached for a day: re-running `sentry
issue view` an hour later could show an outdated "latest" event.

The getsentry/sentry#128818 spike
([findings](getsentry/sentry#128818 (comment)))
found that none of these responses are actually immutable. Event
payloads embed `nextEventID`/`previousEventID`, user reports, and
release data, reprocessing and merges can change them for the same ID,
`latest`/`oldest`/`recommended` resolve to different events over time,
and full traces gain spans as they arrive.

This removes the `immutable` tier:

| Tier | TTL | Matches |
| --- | --- | --- |
| no-cache | 0 | autofix and root-cause polling (unchanged) |
| volatile | 60 s | `/issues/` (now including issue events), project
event details, full traces, `dataset=logs\|spans`, trace logs |
| stable | 5 m | everything else, now including organization event
details and trace-item details |

Server `Cache-Control` headers still take precedence, so when
getsentry/sentry#128828 ships its `private, max-age=300` headers the CLI
follows them without a release. A TODO points at
getsentry/sentry-api-schema#81 and #102 for classifying by generated
`@sentry/api` operation metadata instead of URL regexes.

Notes for review:

- **No migration.** Freshness is recomputed from `createdAt` and the
current tier on read, so existing 24-hour entries on disk are treated by
the new rules immediately.
- **`immutableMinTimeToLive` removed.** It pointed at the deleted tier
constant. The `http-cache-semantics` default is the same 24 hours, and
it only applies to responses where the server sends `immutable`.
- **Cost:** repeated reads of project/issue events and full traces will
hit the API after 60 seconds instead of 24 hours; organization event and
trace-item details after 5 minutes.

Task group 2 of the `unify-sentry-api-layer` OpenSpec change in #1669.

**Checks run locally:**

- New `classifyUrl` cases plus fake-clock expiry tests for org event
details (hit at 4 min, miss at 6 min), issue latest event, project event
details, full traces, and server `max-age` overriding the fallback. 8 of
them fail against the old classifier.
- `pnpm exec vitest run test/lib/response-cache*.test.ts`: 60 passed
- CLI `typecheck`, `lint`, and `oxlint`/`oxfmt` on the changed files:
clean
- `pnpm run test:unit` (first revision): 11,583 passed, 6 failed. The
same 6 fail on unmodified `main` in my sandbox: filesystem-permission
tests (`completions`, `code-scanner`, `file-changes`, `telemetry`
readonly DB, `upgrade` lock) that depend on the environment, not this
change.

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3A0ab4fc2bd5a2e23f2c25efcf -->

--

[View Junior Session in
Sentry](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A0ab4fc2bd5a2e23f2c25efcf/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
@sentry-junior
sentry-junior Bot force-pushed the feat/api-max-age-event-responses branch from 74e2957 to 8bbfa33 Compare October 28, 2026 19:45
Endpoints that set cache_max_age send Cache-Control: private, max-age=N
and add Origin to Vary, so a browser can reuse event and trace item
details without a round trip. Nothing changes until an endpoint class is
listed in the new api.conditional_get.max_age_endpoints option, which
defaults to empty. Superuser and staff sessions, and requests with
live-data query params, keep private, no-cache.

Opts in OrganizationEventDetailsEndpoint and
ProjectTraceItemDetailsEndpoint with a 300 second max-age.

Refs #128818

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot force-pushed the feat/api-max-age-event-responses branch from 8bbfa33 to 7121dcc Compare October 28, 2026 19:46

@BYK BYK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We not doing ETags?

Replace hard-coded max-age seconds with a CacheTier enum that matches the
response cache tiers used by the CLI in getsentry/toolkit.
@sentry-junior

sentry-junior Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Re: "We not doing ETags?" — yes, ETags are still in. Max-age is layered on top of them, not a replacement. Every response from these endpoints still gets an ETag, and If-None-Match still gets a 304. max-age only lets the browser skip the request for the tier's window. Once that window ends, the browser revalidates with the ETag, so an unchanged event costs a 304 instead of a full body. Endpoints not on the api.conditional_get.max_age_endpoints allowlist behave as before (ETag only). Named tiers were added in 6c1047e per the inline comment.

@BYK
BYK merged commit 1c8b21b into master Nov 2, 2026
89 checks passed
@BYK
BYK deleted the feat/api-max-age-event-responses branch November 2, 2026 22:26
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.

1 participant