Tracing helps you understand what’s happening under the hood when an api is called. You get to understand the path, context used, latency taken when you run your fast api using TrueFoundry’s tracing functionality.

TrueFoundry enhances FastAPI applications with powerful observability features, offering real-time monitoring of API requests, response times, and performance metrics. By integrating with TrueFoundry’s tracing, metrics collection, and error detection capabilities, you gain deep insights into how your FastAPI endpoints are performing, where bottlenecks occur, and how to optimize your API workflows. Additionally, TrueFoundry provides end-to-end traceability for your API-driven systems, making your FastAPI implementations more transparent, reliable, and scalable.

setup requirements.txt:

# Pin these according to your application
fastapi
uvicorn
# For loading envirnoment variables from env file
dotenv>= 0.9.9
# Packages for OTEL client
opentelemetry-instrumentation-fastapi>=0.54,<1.0.0
traceloop-sdk>=0.40.5,<1.0.0

Install requirements:

pip install -r requirements.txt

Setup environment variables:

Add the necessary environment variables to enable tracing. You can create a .env file for this example

TRACELOOP_BASE_URL=<<control-plane-url>>/api/otel
TRACELOOP_HEADERS="Authorization=Bearer <<api-key>>,tfy-tracing-project=<<tracing-project-fqn>>"

Create a Tracing Project

Follow the steps to create a tracing project here to get the tracing project FQN.

Generate API Key

Generate a API Key here. This can be a Person Access Token or Virtual Account Token which has access to the ML Repo of the Tracing Project you created.

Demo FastAPI

The following is a simple FastAPI application that demonstrates how to integrate TrueFoundry’s tracing capabilities. It includes a basic /greet endpoint that returns a Hi there, wishing you a great day! message, with OpenTelemetry instrumentation enabled for tracing.

from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from traceloop.sdk import Traceloop
from dotenv import load_dotenv
import uvicorn

load_dotenv()

Traceloop.init()
app = FastAPI()

@app.get("/greet")
async def root():
    return {"message": "Hi there, wishing you a great day!"}

FastAPIInstrumentor.instrument_app(app)

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True, log_level="debug") 

View Logged Trace on UI