Skip to content

refactor: OTel instrument touch-ups (REF-1, LOW-3, LOW-5)#97

Merged
lesnik512 merged 1 commit into
mainfrom
refactor/ref-1-otel-touch-ups
Jun 1, 2026
Merged

refactor: OTel instrument touch-ups (REF-1, LOW-3, LOW-5)#97
lesnik512 merged 1 commit into
mainfrom
refactor/ref-1-otel-touch-ups

Conversation

@lesnik512

@lesnik512 lesnik512 commented Jun 1, 2026

Copy link
Copy Markdown
Member

Summary

Three small OTel-area cleanups:

  • REF-1: _build_excluded_urls() was character-for-character identical in FastAPIOpenTelemetryInstrument and LitestarOpenTelemetryInstrument. Hoisted to the base OpenTelemetryInstrument; the hoisted version uses 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 (produces \r\n on Windows). OTel SDK convention is plain \n; switched to a literal.
  • LOW-5: Added a two-line comment on LitestarOpenTelemetryInstrumentationMiddleware._otel_apps documenting the id(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 set prometheus_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.
  • Reviewer: confirm the getattr defaults match the pre-hoist behavior for FastAPI/Litestar.

🤖 Generated with Claude Code

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

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...te_bootstrap/bootstrappers/fastapi_bootstrapper.py 100.00% <ø> (ø)
...e_bootstrap/bootstrappers/litestar_bootstrapper.py 100.00% <ø> (ø)
..._bootstrap/instruments/opentelemetry_instrument.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lesnik512 lesnik512 self-assigned this Jun 1, 2026
@lesnik512 lesnik512 merged commit f46e539 into main Jun 1, 2026
13 of 14 checks passed
@lesnik512 lesnik512 deleted the refactor/ref-1-otel-touch-ups branch June 1, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant