DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Modern Full-Stack Python Applications: MVC Architecture Meets Enterprise-Ready Python
  • Checking TLS and SSL Versions of Applications in JavaScript, Python, and Other Programming Languages
  • Top 10 Python Applications Transforming the Real World
  • Deploying Python and Java Applications to Kubernetes With Korifi

Trending

  • DZone's Article Submission Guidelines
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Mastering Fluent Bit: Beginners' Guide for Contributing to Our CNCF Project Website
  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Monitoring and Observability
  4. Logfire: Uncomplicated Observability for Python Applications

Logfire: Uncomplicated Observability for Python Applications

Learn about Logfire, an observability platform designed to provide developers with powerful insights into their Python applications.

By 
Vidyasagar (Sarath Chandra) Machupalli FBCS user avatar
Vidyasagar (Sarath Chandra) Machupalli FBCS
DZone Core CORE ·
Feb. 11, 25 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

In 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

  1. Log into Logfire.  
  2. Follow the prompts to create your account. This account will help you to organize your projects. 
  3. From your organization, click New project to create your first project.

Projects

Create a new 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.

Shell
 
pip install logfire

Installing LogFire

Installing LogFire

Step 2. Authenticate with the Logfire service. This will open a browser and ask for logfire login credentials. 

Shell
 
logfire auth

Authentication with LogFire

Authentication with LogFire

3. Configure Logfire in your application. Once the package is installed, you need to use the below commands to configure logfire.

Python
 
import logfire
logfire.configure()


You can follow the steps as part of your project setup:

Project setup

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:

Python
 
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)

Logfire project showing spans

Logfire project showing spans

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:

Python
 
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.

OpenAI API helps AI enthusiasts access large language models (LLMs) like GPT-3 via an API request. This helps developers to integrate cutting-edge AI capabilities into their applications, enabling tasks like text generation, image creation, code writing, and more through simple API calls.

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:

Python
 
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.

Logfire showing the quota exceeded error information

Logfire showing the quota exceeded error information

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.

Observability applications Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • Building Modern Full-Stack Python Applications: MVC Architecture Meets Enterprise-Ready Python
  • Checking TLS and SSL Versions of Applications in JavaScript, Python, and Other Programming Languages
  • Top 10 Python Applications Transforming the Real World
  • Deploying Python and Java Applications to Kubernetes With Korifi

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook