Introduction to Pydantic Logfire — Uncomplicated Observability
Pydantic, known for its powerful open-source tools, introduces Logfire, an observability platform designed with the same philosophy: delivering powerful capabilities while maintaining ease of use. Logfire is a tool developed for comprehensive observability into Python applications, helping developers gain significant insights into application behavior.
Key Features of Logfire
Logfire distinguishes itself through several core features:
- Simplicity Meets Power: The Logfire dashboard is intuitively simple yet robust, ensuring it is easily adoptable by entire engineering teams. It serves both individual developers and larger teams by providing a straightforward interface without sacrificing its powerful abilities.
- Python-centric Analytics: Tailored for Python applications, Logfire presents detailed insights, from Python object displays to event-loop telemetry, and profiles Python code and database queries for unparalleled visibility.
- SQL Querying: Logfire supports querying data using standard SQL. This feature offers developers control over data examination using a familiar language, and it integrates seamlessly with existing business intelligence (BI) tools and database libraries.
- OpenTelemetry Integration: As a thoughtful wrapper around OpenTelemetry, Logfire allows developers to leverage pre-existing tools and infrastructure. It supports a wide array of Python packages and is extensible to most programming languages.
- Pydantic Compatibility: Logfire enhances Pydantic model functionality with built-in analytics, granting insights into the data processed by these models.
For more detailed information, Logfire's documentation provides extensive resources.
Getting Started with Logfire
Installation
Installing Logfire is straightforward via Python’s package manager, pip:
pip install logfire
More installation details can be found here.
Authentication
Authenticate your installation with a simple command:
logfire auth
Further details on authentication are available here.
Manual Tracing and Integration
Logfire supports both manual logging and integration with popular Python packages.
Manual Tracing
Here's a basic example of manual tracing:
import logfire
from datetime import date
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)
Discover more about manual tracing here.
Integration with FastAPI
Avoid manual instrumentation by integrating Logfire with various packages like FastAPI as shown below:
import logfire
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
logfire.configure()
logfire.instrument_fastapi(app)
class User(BaseModel):
name: str
country_code: str
@app.post('/')
async def add_user(user: User):
return {'message': f'{user.name} added'}
Learn more about FastAPI integration here.
Engaging with the Community
Logfire invites developers to contribute to its Software Development Kit (SDK) and documentation. Those interested can follow the contribution guide.
Additionally, the team encourages reporting any security vulnerabilities by reviewing the security policy.
Conclusion
Overall, Logfire represents a comprehensive solution for developers seeking to gain insights into their Python applications. It provides a powerful suite of tools while maintaining simplicity and ease of integration, enhancing productivity and observability for Python-centric development teams.