Skip to content

ops(telemetry): Integrate OpenTelemetry tracing across FastAPI router, database connections, and Redis client #33

Description

Description
Integrate OpenTelemetry instrumentation middleware in FastAPI, SQLAlchemy, and Redis clients, ensuring that every HTTP request, database query, and cache call is correlated with a trace ID and outputted as structured JSON.

Context
Debugging synchronization issues across multi-user environments is highly complex. If a synchronization pull fails, we need to trace the exact sequence of DB queries, Redis locks, and exceptions back to a single request ID.

Technical Implementation Blueprint
OTel Instrumentation: Configure telemetry tracing:
python
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor

FastAPIInstrumentor.instrument_app(app)
SQLAlchemyInstrumentor().instrument(engine=db_engine.sync_engine)

Structured Log Formatter: Setup python logging to output JSON records containing trace_id and span_id extracted from the active context variables:
json
{
"timestamp": "2026-08-26T15:43:21Z",
"level": "INFO",
"message": "Sync pull returned 15 operations",
"trace_id": "8f89c67a840e4f3a9e102f9015c7e099",
"span_id": "01c9a6ef0e12ff90"
}

Acceptance Criteria
Outgoing JSON console logs contain valid OpenTelemetry trace IDs for tracing across microservices.
Exceptions thrown inside background arq tasks are correctly logged with the trace ID of the initiating request.
No trace/log processing overhead blocks runtime HTTP response delivery.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions