refactor: OTel instrument touch-ups (REF-1, LOW-3, LOW-5)#97
Merged
Conversation
REF-1: _build_excluded_urls() was character-for-character identical in FastAPIOpenTelemetryInstrument and LitestarOpenTelemetryInstrument. Hoist to the base OpenTelemetryInstrument and use getattr with safe defaults so the method works when the base instrument runs against a config without the framework-specific fields (opentelemetry_excluded_urls, prometheus_metrics_path, health_checks_path). LOW-3: _format_span used os.linesep, which produces \r\n on Windows. The OTel SDK convention is plain \n; change to a literal. LOW-5: Add a one-line comment on LitestarOpenTelemetryInstrumentationMiddleware._otel_apps documenting the id(next_app) cache assumption (ASGI app instances are stable for the middleware lifetime). No behavior change. Closes REF-1, LOW-3, LOW-5 from the audit.
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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
Three small OTel-area cleanups:
_build_excluded_urls()was character-for-character identical inFastAPIOpenTelemetryInstrumentandLitestarOpenTelemetryInstrument. Hoisted to the baseOpenTelemetryInstrument; the hoisted version usesgetattrwith safe defaults so the method works when the base instrument runs against a config without the framework-specific fields (opentelemetry_excluded_urls,prometheus_metrics_path,health_checks_path)._format_spanusedos.linesep(produces\r\non Windows). OTel SDK convention is plain\n; switched to a literal.LitestarOpenTelemetryInstrumentationMiddleware._otel_appsdocumenting theid(next_app)cache assumption (ASGI app instances are stable for the middleware lifetime, so id-reuse-after-GC isn't a concern).No behavior change. No new tests — existing framework integration tests verify.
Closes REF-1, LOW-3, LOW-5 from an internal audit.
Subtle semantic difference (non-issue, but worth noting)
Pre-hoist code called
excluded_urls.add(self.bootstrap_config.prometheus_metrics_path)unconditionally. Post-hoist guards on truthy (if prometheus_path:) to protect the Free bootstrapper path where the field is absent. For FastAPI/Litestar with the default/metrics, behavior is identical. If someone ever explicitly setprometheus_metrics_path=""on a FastAPI/Litestar config, the pre-hoist code would have added""to the exclusion set (harmless no-op for URL matching) while the post-hoist code skips it.PrometheusInstrument.is_ready()already rejects empty paths, so this is impossible to hit in practice.Test plan
just test -- tests/instruments/test_opentelemetry_instrument.py tests/test_fastapi_bootstrap.py tests/test_litestar_bootstrap.py -v— pass.just test— 89/89.just lint— clean.getattrdefaults match the pre-hoist behavior for FastAPI/Litestar.🤖 Generated with Claude Code