Repository navigation
Conversation
This was referenced Oct 24, 2026
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.
Adds an opt-in
LitestarConfig.sentry_lean_error_capture. When on, lite-bootstrap disables sentry-sdk'sLitestarIntegrationand reports request errors through a middleware and anafter_exceptionhook of its own.Why
With
traces_sample_rateunset,LitestarIntegrationstill builds aTransactionand a span per middleware on every request and throws them away. getsentry/sentry-python#7400 (closed as not planned, since the next major drops transactions) measured this on FastAPI. On litestar-sqlalchemy-template it is about two thirds of Sentry's per-request cost. Measured with one Granian worker on 2 CPUs, Postgres 17 and oha at 32 connections. Each cell is the median of 3 × 10 s runs, with two rounds averaged:LitestarIntegration(default)sentry_lean_error_capture=Truesentry_default_integrations=FalseWhat lean capture still costs is mostly Sentry's other default integrations: SQLAlchemy and asyncpg breadcrumbs, logging and the rest.
What it does
LitestarSentryCaptureMiddleware, inserted first inapplication_config.middleware, runs each HTTP request in its own isolation scope and release-health session. It continues an incomingsentry-trace/baggageand attaches the request (method, URL, query string, headers) and an endpoint-style transaction name to every event sent during the request. Withoutsend_default_pii, the headers sentry-sdk's ASGI integration masks are masked here too.after_exceptionruns before Litestar's exception handler builds the response, so inside the middleware the hook only records the exception. The middleware reports it whenhttp.response.startcarries a 5xx. An exception thatexception_handlersanswer with a 404 is not reported.LitestarIntegrationreports it, because it only skipsHTTPExceptions below 500. Outside the middleware, the hook falls back to that sameHTTPExceptionrule. That covers routing errors and an exception handler that itself raises.ignore_logger("litestar"), asLitestarIntegrationdoes, so Litestar's own exception log does not produce a second event.SentryInstrumentis now generic over its config, and init kwargs are built in_build_init_params(), soLitestarSentryInstrumentcan appendLitestarIntegrationtodisabled_integrations. That appends to anydisabled_integrationsthe user passes insentry_additional_params.sentry_traces_sample_rateset alongside lean capture (no request transactions are started) and for aLitestarIntegrationinsentry_integrations(it gets dropped).Lean capture gives up Sentry performance monitoring for requests, and the user details
LitestarIntegrationadds withsend_default_pii.Floors
LitestarIntegrationappeared in sentry-sdk 2.13, afterdisabled_integrations(2.11). The newimport_checker.is_sentry_litestar_integration_installedprobe therefore also tells whether that option exists. At the 2.1 floor nothing is disabled.track_sessionis new; 2.1 only hasauto_session_tracking_scope, which takes the same arguments.generate_propagation_context,isolation_scope,event_from_exceptionandtransaction_from_functionexist at 2.1.scripts/floor_smoke.py litestarnow sends one failing request through lean capture before the default bootstrap. I ran it locally at the 3.11 floors (sentry-sdk 2.1.0, litestar 2.22.0) and the 3.14 floors (sentry-sdk 2.59.0, whereLitestarIntegrationended up inactive).A sentry-sdk quirk the tests work around
sentry-sdk records installed integrations for the whole process, and
disabled_integrationscannot remove one that an earliersentry_sdk.initinstalled. That only matters when one process callsinittwice. Lean capture warns in that case, because errors could then be reported twice. The new test module resets just the Litestar entry around each test, so it passes in any order.Tests
tests/test_litestar_sentry_capture.py, written first. It covers:HTTPExceptions, and reporting mapped 5xx and serverHTTPExceptions;The full suite passes with 100% coverage. Ruff is clean.
mkdocs build --strictpasses.