OpenTelemetry under the hood ¶
Because Pydantic Logfire is built on OpenTelemetry, you can use a wealth of existing tooling and infrastructure, including instrumentation for many common Python packages. Logfire also supports cross-language data integration and data export to any OpenTelemetry-compatible backend or proxy.
For example, we can instrument a simple FastAPI app with just 2 lines of code:
from datetime import date
import logfire
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
logfire.configure()
logfire.instrument_fastapi(app) # (1)!
# Here you'd instrument any other library that you use. (2)
class User(BaseModel):
name: str
country_code: str
dob: date
@app.post('/')
async def add_user(user: User):
# we would store the user here
return {'message': f'{user.name} added'}
- In addition to configuring logfire this line is all you need to instrument a FastAPI app with Logfire. The same applies to most other popular Python web frameworks.
- The integrations page has more information on how to instrument other parts of your app. Run the inspect command for package suggestions.
We'll need the FastAPI contrib package, FastAPI itself and uvicorn installed to run this:
pip install 'logfire[fastapi]' fastapi uvicorn # (1)!
uvicorn fastapi_example:app # (2)!
- Install the
logfire
package with thefastapi
extra, FastAPI, and uvicorn. - Run the FastAPI app with uvicorn.
This will give you information on the HTTP request and details of results from successful input validations:
And, importantly, details of failed input validations:
In the example above, we can see the FastAPI arguments failing (user
is null when it should always be populated). This demonstrates type-checking from Pydantic used out-of-the-box in FastAPI.