A slow API response is a symptom. The useful question is where the request spent its time: waiting for a database connection, executing SQL, calling another service or running application code. Monitoring should make those possibilities distinguishable.
For a FastAPI backend, I would start with a small set of questions rather than a large dashboard: are requests succeeding, how long do they take, and which dependency explains a change?
Establish the request boundary
OpenTelemetry’s FastAPIInstrumentor instruments inbound HTTP handling. It still needs a configured SDK and exporter to send traces somewhere useful. The following example exports to a local Collector over OTLP HTTP; it is an illustrative starting point, not a complete monitoring stack.
pip install fastapi uvicorn opentelemetry-sdk \
opentelemetry-instrumentation-fastapi opentelemetry-exporter-otlp-proto-http
from contextlib import asynccontextmanager
from fastapi import FastAPI
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
provider = TracerProvider(
resource=Resource.create({"service.name": "engineering-api"})
)
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
provider.shutdown()
app = FastAPI(lifespan=lifespan)
FastAPIInstrumentor.instrument_app(app, tracer_provider=provider)
@app.get("/health")
async def health():
return {"status": "ok"}
Run a local Collector that accepts OTLP HTTP on port 4318 before exercising this example. Configure the exporter endpoint, authentication and TLS for the target environment. Initialise the provider in each serving process, avoid combining this setup with a second automatic instrumentation setup, and pin compatible package versions in the application lockfile.
Follow the expensive work
An inbound span alone will not explain every database or network delay. Add the instrumentation appropriate to the actual SQL and HTTP clients, then check that context reaches downstream services. For business operations, consider a manual span such as inspection.validate, with a small set of stable attributes.
Separate queue wait from execution time. A calculation taking two seconds to run after waiting five minutes needs a different fix from a calculation that itself takes five minutes. Likewise, a fast SQL query does not rule out a saturated connection pool.
Make telemetry safe and affordable
Treat identifiers, request bodies, headers and exception messages as potential sources of sensitive data. Do not enable unrestricted header or payload capture. Keep credentials and personal information out of spans and logs. Prefer route templates over raw URLs as metric labels, and avoid labels for every user, document or job.
Start with latency distributions, request volume, error rate and saturation signals for the important resources. Trace sampling and metric aggregation are separate choices: a sample of traces is useful for diagnosis, but should not silently become the only basis for an availability calculation.
Verify the investigation path
In a test environment, introduce a slow dependency and a failing request. Confirm that an alert leads to the affected route, a representative trace and an actionable dependency failure. Test exporter unavailability as well: the API should not become dependent on a healthy telemetry backend.
An alert earns its place when someone can explain the user impact, likely next investigation and recovery action. Everything else can remain a dashboard until there is a clear reason to wake somebody up.
References
- OpenTelemetry FastAPI instrumentation
- OpenTelemetry Python exporters
- OneUptime’s FastAPI instrumentation walkthrough, the topic suggested for this article.