Logfire: Uncomplicated Observability for Python Applications
Learn about Logfire, an observability platform designed to provide developers with powerful insights into their Python applications.
Join the DZone community and get the full member experience.
Join For FreeIn my previous article on Pydantic, I introduced you to Logfire in one of the code examples as an observability platform designed to provide developers with insights into Python applications. In this article, you will get a deep dive into Logfire and its capabilities that will eventually simplify your Observability journey from tracing to debugging to logging.
Logfire is an innovative observability platform developed by the creators of Pydantic, designed to provide developers with powerful insights into their Python applications. Built on the same principles that made Pydantic a success, Logfire aims to make observability easy to implement and understand while offering deep insights into application behavior.
Key Features
Seamless Integration
Logfire integrates effortlessly with existing Python projects, requiring minimal setup. With just a few lines of code, developers can start gathering valuable data about their application's performance and behavior.
Pydantic Integration
Logfire offers exceptional integration with Pydantic models. This allows developers to gain unprecedented insights into data validation and model usage throughout their applications.
Automatic Instrumentation
Logfire can automatically instrument popular libraries and frameworks, reducing the need for manual logging and tracing. This feature saves time and ensures comprehensive coverage of application behavior.
OpenTelemetry Compatible
Built on top of OpenTelemetry, Logfire ensures compatibility with industry standards and allows for flexible data export options.
Prerequisites
- Log into Logfire.
- Follow the prompts to create your account. This account will help you to organize your projects.
- From your organization, click New project to create your first project.
Getting Started
To begin using Logfire, follow these simple steps:
Step 1. Install the Logfire package with the command below. If you are using Jupyter Notebook, run !pip install logfire
.
pip install logfire
Step 2. Authenticate with the Logfire service. This will open a browser and ask for logfire login credentials.
logfire auth
3. Configure Logfire in your application. Once the package is installed, you need to use the below commands to configure logfire.
import logfire
logfire.configure()
You can follow the steps as part of your project setup:
Basic Usage
Span in Logfire is an essential building block of a trace. You can define a span with logfire.span. In the example below, The outer span sets the topic — the user's birthday. The user will be prompted for their birthday and captured through the terminal or command prompt. The space captures the entered DOB of the user.
Here's a simple example of how to use Logfire for manual tracing:
import logfire
from datetime import date
logfire.configure()
logfire.info('Hello, {name}!', name='world')
with logfire.span('Asking the user their {question}', question='age'):
user_input = input('How old are you [YYYY-mm-dd]? ')
dob = date.fromisoformat(user_input)
logfire.debug('{dob=} {age=!r}', dob=dob, age=date.today() - dob)
If you don't see the DEBUG output, click on Filter local data --> Levels --> debug.
Pydantic Integration
As discussed in my previous article, Pydantic's core functionality is data validation. It uses Python-type hints to automatically validate the structure and types of data. When you define a Pydantic model, each field is annotated with its expected type. Pydantic then ensures that any data assigned to these fields conforms to the specified types.
Logfire's integration with Pydantic is particularly powerful. Here's how you can use it to log Pydantic model validations:
from datetime import date
import logfire
from pydantic import BaseModel
logfire.configure()
logfire.instrument_pydantic()
class User(BaseModel):
name: str
country_code: str
dob: date
User(name='Anne', country_code='USA', dob='2000-01-01')
User(name='Ben', country_code='USA', dob='2000-02-02')
User(name='Charlie', country_code='GBR', dob='1990-03-03')
This configuration will automatically log details about all Pydantic model validations, providing valuable insights into data processing within your application.
Framework Integrations
Logfire offers integrations with popular Python frameworks and libraries. For the Python code example below, you need to provide the OpenAI API key and install the required packages (FASTAPI, instructor, OpenAI, etc.).
For starters, FastAPI is a Python web framework that helps developers create application programming interfaces (APIs). FastAPI is known for its scalability, ease of use, and high performance.
Instructor is a python package that helps you to easily extract structured data like JSON from the LLMs output.
Here's an example of how to integrate Logfire with FastAPI, OpenAI API, and instructor:
from fastapi import FastAPI
from openai import AsyncOpenAI
import os
import instructor
import logfire
from pydantic import BaseModel
app = FastAPI()
openai_client = AsyncOpenAI(
api_key=os.environ.get("OPENAI_API_KEY"))
logfire.configure()
logfire.instrument_pydantic()
logfire.instrument_openai(openai_client)
logfire.instrument_fastapi(app)
client = instructor.from_openai(openai_client)
class UserData(BaseModel):
query: str
class UserDetail(BaseModel):
name: str
age: int
@app.post("/user", response_model=UserDetail)
async def endpoint_function(data: UserData) -> UserDetail:
user_detail = await client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=UserDetail,
messages=[
{"role": "user", "content": f"Extract: `{data.query}`"},
],
)
return user_detail
This setup provides comprehensive logging for FastAPI requests, OpenAI API calls, and Pydantic model validations, offering a holistic view of your application's behavior.
Conclusion
Logfire represents a significant step forward in making observability accessible and powerful for Python developers. Logfire enables developers to better understand and optimize their applications by combining ease of use with deep insights. If you're working on a small project or a large-scale application, Logfire provides the tools to gain valuable insights into your code's behavior and performance.
Please shower your love with a like and share with your Dev community.
Opinions expressed by DZone contributors are their own.
Comments