This document shows how to instrument a Python service with OpenTelemetry and forward traces, metrics, and logs to the local Docker Compose stack (otel-collector, prometheus, loki, tempo).
pip install opentelemetry-sdk opentelemetry-exporter-otlp opentelemetry-instrumentationOptional log/metrics helpers:
pip install opentelemetry-instrumentation-logging opentelemetry-instrumentation-requestsAssuming the stack is running on the same machine:
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
export OTEL_SERVICE_NAME="my-python-app"If MCP_TOKEN is required by mcp-server you still only need the collector URL; the collector forwards data internally.
Install the FastAPI instrumentation shim:
pip install opentelemetry-instrumentation-fastapiIn your main.py (or wherever you create the ASGI app):
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware
app = FastAPI()
# Attach instrumentation (captures requests, errors, latency)
FastAPIInstrumentor.instrument_app(app)
# optional: explicit middleware as alternative
# app.add_middleware(OpenTelemetryMiddleware)
@app.get("/hello")
async def hello():
return {"msg": "hello"}The instrumentation automatically creates spans for each request with attributes like http.method, http.route, http.status_code.
If you deployed mcp-observability via Docker Compose the default collector endpoint is http://localhost:4318.
For Kubernetes / remote clusters, point to the otel-collector ClusterIP service:
export OTEL_EXPORTER_OTLP_ENDPOINT="http://otel-collector.observability.svc:4318"
export OTEL_EXPORTER_OTLP_HEADERS="mcp-token=<YOUR_MCP_TOKEN>"Use the OpenTelemetry launcher to patch popular libraries automatically:
opentelemetry-instrument \
--traces_exporter otlp \
--metrics_exporter otlp \
--service_name my-python-app \
python app.py-
Open http://localhost:3000 (admin/admin by default).
-
Go to Dashboards ▸ Browse ▸ Traces to see spans, or Browse ▸ Metrics for latency graphs.
-
Use Explore to query logs:
{service="my-python-app",level="error"}
- No spans appear – confirm the
otel-collectorcontainer is healthy and the endpoint URL/port are correct. - Connection refused – ensure Docker Compose stack is running and port 4318 is not firewalled.
- High cardinality warnings – set
OTEL_ATTRIBUTE_COUNT_LIMIT,OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMITas needed.
"You are an AI code assistant. Modify the existing FastAPI project to emit OpenTelemetry traces, metrics and logs to a remote OTLP endpoint. Requirements:\n\n* Use
opentelemetry-sdk>=1.25,opentelemetry-instrumentation-fastapi, andopentelemetry-exporter-otlp.\n* Set exporter endpoint to${OTEL_EXPORTER_OTLP_ENDPOINT}and add${MCP_TOKEN}tomcp-tokenHTTP header.\n* Instrument FastAPI routes automatically and ensure startup/shutdown events are traced.\n* Capture HTTP client calls viaopentelemetry-instrumentation-requests.\n* Output example.envfile with OTEL variables."
Paste this into your preferred agent (e.g. Cursor, Claude) to auto-patch the service.