Repository navigation
Use Litestar's OpenTelemetryPlugin in the Litestar bootstrapper - #154
Open
personage-hub wants to merge 1 commit into
Open
personage-hub wants to merge 1 commit into
personage-hub wants to merge 1 commit into
Conversation
Litestar ships its own OpenTelemetry plugin since the custom middleware was written, so the bootstrapper now registers OpenTelemetryPlugin instead of building an ASGI middleware around OpenTelemetryMiddleware by hand (which also re-created the OTel middleware on every request). opentelemetry_exclude_urls map to the plugin's exclude, OTEL_PYTHON_LITESTAR_EXCLUDED_URLS keeps working through the plugin's exclude_urls_env_key. Litestar applies the plugin middleware outside the router, before path_template is known, so a small LitestarOpentelemetryRouteMiddleware inside the handler stack renames the server span by the route template and sets http.route, keeping the span names unchanged. Requires litestar>=2.22.0, where litestar.plugins.opentelemetry appeared. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
personage-hub
force-pushed
the
litestar-opentelemetry-plugin
branch
from
October 7, 2026 06:33
5e8d302 to
80a9b11
Compare
This branch has not been 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.
Summary
Litestar ships its own
OpenTelemetryPluginnow, so the Litestar bootstrapper registers it instead of hand-building an ASGI middleware aroundOpenTelemetryMiddleware(the oldLitestarOpenTelemetryInstrumentationMiddleware, which also re-created the OTel middleware on every request inhandle).opentelemetry_exclude_urls(plus the health check path whenopentelemetry_generate_health_check_spansis off) map to the plugin'sexclude;OTEL_PYTHON_LITESTAR_EXCLUDED_URLSkeeps working through the plugin'sexclude_urls_env_key, soCombinedExcludeListis no longer needed for Litestar.pluginsare preserved, the lists are merged like any other container config.litestar>=2.22.0, wherelitestar.plugins.opentelemetryappeared (2.21 only has the deprecatedlitestar.contrib.opentelemetrypath).Why there is still a middleware
Litestar applies the plugin's middleware outside the router (
Litestar.__init__wrapsasgi_handlerwith it), i.e. beforepath_templateis known. That is also why Litestar's defaultscope_span_details_extractorreportsGET /users/123. To keep the span names andhttp.routeas they are today (GET /users/{user_id}), a tinyLitestarOpentelemetryRouteMiddlewareinside the route handler stack renames the current server span by the route template. It does nothing for excluded urls (the span is non-recording there).Tests
test_litestar_opentelemetry_server_spans_use_path_templateasserts real exported spans:GET /users/{user_id}withhttp.route, andGET /missingfor a 404.OTEL_PYTHON_LITESTAR_EXCLUDED_URLSare now integration tests over exported spans.just test: 344 passed.Note:
uv run mypy .reports two pre-existing errors inmicrobootstrap/helpers.py:29with the locked mypy on Python 3.14; untouched here.🤖 Generated with Claude Code