This guide demonstrates how to use the TrueFoundry OtelCollector in combination with the Traceloop SDK to capture distributed traces in distributed FastAPI applications.
In this example, we’ll build two simple FastAPI services. The first service exposes a /say-hello
endpoint, which internally calls the /generate-greeting
endpoint on the second service.
Create Tracing Project, API Key and copy tracing code
Follow the instructions in Getting Started to create a tracing project, generate API key and copy the
tracing code.
Install Dependencies
First, you need to install the following
pip install fastapi uvicorn dotenv opentelemetry-instrumentation-fastapi==0.55b0 traceloop-sdk==0.38.12 opentelemetry-instrumentation-httpx
Add Tracing code to FastAPI application 1
For FastAPI applications, we need to add the Traceloop.init()
call to initialize tracing and instrument both the FastAPI app and httpx client with OpenTelemetry to capture distributed traces.
from dotenv import load_dotenv
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
import uvicorn
import os
import httpx
# importing traceloop sdk
from traceloop.sdk import Traceloop
load_dotenv()
# Add the traceloop init code to your application
TFY_API_KEY = os.environ.get("TFY_API_KEY")
Traceloop.init(
api_endpoint="<enter_your_api_endpoint>",
headers = {
"Authorization": f"Bearer {TFY_API_KEY}",
"TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
},
)
app = FastAPI()
# httpx client
client = httpx.AsyncClient()
@app.get("/say-hello")
async def root():
try:
response = await client.get("http://localhost:8000/generate-greeting")
return response.json()
except httpx.HTTPStatusError as e:
return {"error": f"Upstream error: {e.response.status_code}", "body": e.response.text}
except ValueError:
return {"error": "Invalid JSON response from upstream"}
# Instrument the FastAPI app
FastAPIInstrumentor.instrument_app(app)
# Allows tracing Http requests made by the httpx library
HTTPXClientInstrumentor().instrument()
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8001, reload=True, log_level="debug")
Add Tracing code to FastAPI application 2
For FastAPI applications, we need to add the Traceloop.init()
call to the application and instrument the FastAPI app with OpenTelemetry.
from dotenv import load_dotenv
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
import uvicorn
import os
# importing traceloop sdk
from traceloop.sdk import Traceloop
load_dotenv()
# Add the traceloop init code to your application
TFY_API_KEY = os.environ.get("TFY_API_KEY")
Traceloop.init(
api_endpoint="<enter_your_api_endpoint>",
headers = {
"Authorization": f"Bearer {TFY_API_KEY}",
"TFY-Tracing-Project": "<enter_your_tracing_project_fqn>",
},
)
app = FastAPI()
@app.get("/generate-greeting")
async def root():
return {"message": "Hi there, wishing you a great day!"}
# Instrument the FastAPI app
FastAPIInstrumentor.instrument_app(app)
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True, log_level="debug")
Make sure to use the same tracing project fqn in the above 2 fastapi applications
View traces
- Start both FastAPI applications
- Hit the endpoint
http://localhost:8081/say-hello
- View the logged trace in your TrueFoundry dashboard