FastAPI is a modern web framework for building APIs with Python. OpenTelemetry is a powerful observability framework that allows you to instrument your applications to collect, process, and export telemetry data such as traces, metrics, and logs. Jaeger is an open-source distributed tracing system that is part of the CNCF landscape. In this tutorial, we will show you how to combine FastAPI and OpenTelemetry to trace your API calls with Jaeger.
Step 1: Install the necessary packages
First, you need to install FastAPI, OpenTelemetry, and the Jaeger exporter for OpenTelemetry. You can do this using pip:
pip install fastapi opentelemetry-api opentelemetry-sdk jaeger-client
Step 2: Create a FastAPI application
Next, create a FastAPI application with a simple endpoint that we will trace using OpenTelemetry. Here is an example of a FastAPI application with a single endpoint:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
Save this code in a file named app.py
.
Step 3: Initialize OpenTelemetry
Import the necessary modules and initialize the OpenTelemetry tracer and Jaeger exporter:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.ext.jaeger import JaegerSpanExporter
trace.set_tracer_provider(TracerProvider())
# Initialize the Jaeger exporter
jaeger_exporter = JaegerSpanExporter(
service_name="my-fastapi-app",
agent_host_name="localhost",
agent_port=6831,
)
# Create a batch exporter and add Jaeger exporter
span_processor = BatchExportSpanProcessor(jaeger_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
This code initializes the OpenTelemetry tracer and configures it to use the Jaeger exporter to send trace data.
Step 4: Add middleware to trace requests
Add middleware to your FastAPI application to trace incoming requests:
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.fastapi import add_response_span_attribute
app = FastAPI()
@app.on_event('startup')
async def startup():
FastAPIInstrumentor.instrument_app(app, request_span_name_provider=add_response_span_attribute)
@app.get("/")
async def read_root():
return {"Hello": "World"}
This code uses the FastAPIInstrumentor to instrument your FastAPI application and automatically start and end spans for each incoming request.
Step 5: Start the Jaeger backend
Before running your FastAPI application, you need to start the Jaeger backend. You can do this using Docker:
docker run -d --name jaeger
-e COLLECTOR_ZIPKIN_HTTP_PORT=9411
-p 16686:16686
-p 14268:14268
-p 9411:9411
jaegertracing/all-in-one:latest
Step 6: Run your FastAPI application
Now you can run your FastAPI application:
uvicorn app:app --reload
Step 7: View traces in Jaeger
Open your browser and navigate to http://localhost:16686
to view the Jaeger UI. You should see traces being collected from your FastAPI application.
That’s it! You have successfully set up FastAPI with OpenTelemetry and Jaeger to trace your API calls. You can now explore the Jaeger UI to analyze and troubleshoot your application’s performance.
You are a funny Indian
Thank you very much for the video. I was just about to contact the CEO of Grafana and Prometheus to address my bugs. I really liked using OpenTelemetry and Jaeger.