Repository navigation
feat!: trace Litestar through its own OpenTelemetryPlugin - #279
Merged
Merged
Conversation
LitestarOpenTelemetryInstrument registers litestar.plugins.opentelemetry's OpenTelemetryPlugin instead of hand-wrapping OpenTelemetryMiddleware in a cached ASGI middleware. A small route middleware renames the server span to the route template, which the plugin cannot see because it runs before routing. Breaking: the litestar-otl extra needs litestar>=2.22; opentelemetry_excluded_urls are searched in the request path rather than the full URL; unmatched paths now produce a method-only span; OTEL_PYTHON_LITESTAR_EXCLUDED_URLS is honored.
This was referenced Oct 9, 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.
Why
Litestar ships
OpenTelemetryPluginsince 2.22. This replacesLitestarOpenTelemetryInstrumentationMiddleware, our hand-rolled wrapper aroundOpenTelemetryMiddlewarewith itsWeakKeyDictionarycache of per-next_appinstances, so we stop maintaining it. Ported from community-of-python/microbootstrap#154.What changes
LitestarOpenTelemetryInstrumentappendsOpenTelemetryPlugintoapplication_config.pluginswith the installed tracer and meter providers. User plugins are kept.path_templateis unknown. A smallLitestarOpenTelemetryRouteMiddlewareruns inside the route stack and renames the current server span toGET /users/{user_id}and setshttp.route. This is the only custom piece left, and it holds no state.exclude, which Litestar searches inscope["path"]. Derived paths (metrics, and health whenopentelemetry_generate_health_check_spans=False) are anchored as^/path(?:/|$), so/custom-health/excludes both slash forms but not/custom-healthy. An empty list is passed asNone, because Litestar compiles[]into a pattern that skips every path.is_litestar_opentelemetry_installedalso requireslitestar.plugins.opentelemetry. With Litestar < 2.22 plus OpenTelemetry installed, the instrument is now skipped with a dependency warning instead of failing atimport lite_bootstrap.Breaking
litestar-otlextra requireslitestar>=2.22(thelitestarextra stays at 2.19).opentelemetry_excluded_urlsentries are searched in the request path instead of the full URL. Path fragments such as/items/\d+$behave the same; a pattern anchored on scheme or host (^https?://...) no longer matches.GET), withouthttp.route, so scanners cannot inflate span-name cardinality.OTEL_PYTHON_LITESTAR_EXCLUDED_URLSis now honored, through the plugin'sexclude_urls_env_key.Tests
litestar.plugins.opentelemetry.just lintandjust testpass (343 passed).litestar==2.22.0,opentelemetry-instrumentation-asgi==0.49b0, Python 3.11) passes. At those floors, real requests produceGET /items/{item_id},GETfor a 404 andGET /hfor the health route, and/metricsis excluded.