feat(observability): built-in OpenTelemetry auto-instrumentation (apso-packages#39) - #4
Open
cultron wants to merge 1 commit into
Open
feat(observability): built-in OpenTelemetry auto-instrumentation (apso-packages#39)#4cultron wants to merge 1 commit into
cultron wants to merge 1 commit into
Conversation
…o-packages#39) Wire OpenTelemetry into the Python service template, mirroring the TS template. A setup_tracing(app, engine) bootstrap builds a TracerProvider with an OTLP HTTP span exporter and service.name/service.version resource attributes, then auto-instruments the FastAPI app (FastAPIInstrumentor) and the SQLAlchemy engine (SQLAlchemyInstrumentor). It is called from the FastAPI lifespan in app/main.py after the app and engine exist, and the provider is flushed/shut down on app shutdown. Safe by default: tracing is a no-op unless OTEL_ENABLED is truthy or (unset) an OTEL_EXPORTER_OTLP_ENDPOINT is present. Setup is wrapped in try/except so telemetry can never take the app down, and BatchSpanProcessor exports on a background thread so an unreachable collector never blocks startup. Env vars: OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_SERVICE_NAME (falls back to APP_NAME, then .apsorc serviceName, then apso-service), OTEL_ENABLED. Added commented/disabled to .env.example and documented in a README "Observability (OpenTelemetry)" section. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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.
What this does
Adds built-in OpenTelemetry auto-instrumentation to the Python service template, matching what shipped for the TS template (
apsoai/service-template-ts#24).What got wired
app/observability/tracing.py—setup_tracing(app, engine)builds aTracerProviderwith an OTLP HTTP span exporter and resource attributes (service.name,service.version), then auto-instruments:FastAPIInstrumentor.instrument_appSQLAlchemyInstrumentor(usesengine.sync_enginefor the async engine)app/main.py—setup_tracing(app, engine)is called from the FastAPI lifespan after the app + engine exist;shutdown_tracing()flushes/shuts down the provider on shutdown so in-flight spans are exported.pyproject.toml— pinned, mutually-compatible deps:opentelemetry-sdk,opentelemetry-exporter-otlp-proto-http,opentelemetry-instrumentation-fastapi,opentelemetry-instrumentation-sqlalchemy..env.example— commented/disabled OTel vars.Environment variables
OTEL_EXPORTER_OTLP_ENDPOINTOTEL_SERVICE_NAMEservice.nameon every trace.APP_NAME, then.apsorcserviceName, thenapso-serviceOTEL_ENABLEDtrue/1/yes/on) or force-disable (false/0/no/off), overriding endpoint detection.Standard
OTEL_*SDK variables (headers, resource attrs, sampling) are honored too.Safe by default
Tracing is a no-op unless
OTEL_ENABLEDis truthy, or (unset) anOTEL_EXPORTER_OTLP_ENDPOINTis present. Setup is wrapped in try/except so telemetry can never take the app down, andBatchSpanProcessorexports on a background thread so an unreachable collector never blocks startup or requests.Verification
pip install -e ".[dev]"resolves cleanly (Python 3.12); OTel imports OK.pytest— 2 passed.http://127.0.0.1:4318) starts and shuts down without hanging.ruff/blackclean on the changed files.This matches the TS template's pattern: a tracing bootstrap started with the app, auto-instrumentation, OTLP exporter, and a safe no-op default.
🤖 Generated with Claude Code