Logfire: Uncomplicated Observability for Python Applications

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

 

Installing LogFire

Installing LogFire

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

Shell

 

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

 

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

 

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

 

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

 

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.

Source:
https://dzone.com/articles/logfire-uncomplicated-observability-for-python-app