System & Client Integrations
Relevant source files
The following files were used as context for generating this wiki page:
Logfire provides an opinionated observability layer built on top of OpenTelemetry, designed to integrate seamlessly with various Python web frameworks, HTTP clients, databases, and system-level metrics. These integrations allow developers to capture traces, metrics, and logs across their entire application stack with minimal manual instrumentation.
The system is designed to be "Python-centric," offering deep visibility into Python-specific behaviors such as event-loop telemetry and rich object display, while maintaining compatibility with the broader OpenTelemetry ecosystem.
Sources: README.md:12-23, mkdocs.yml:104-180
Integration Architecture
Logfire functions as a wrapper around OpenTelemetry, managing the lifecycle of TracerProviders, MeterProviders, and LoggerProviders. When an integration is activated, it typically attaches specific instrumentation to the target library, which then emits telemetry data through Logfire's configured processors and exporters.
The diagram shows how various application components interface with the Logfire SDK via instrumentation to export data to the backend or console.
Sources: logfire/_internal/config.py:539-650, README.md:52-78
Supported Client & System Integrations
Logfire categorizes its integrations into several logical groups. The configuration for these is often handled via specific instrument_* functions or during the initial logfire.configure() call.
Web Frameworks
Logfire supports major Python web frameworks by wrapping their ASGI/WSGI interfaces or using native instrumentation.
- FastAPI / Starlette: Captured via specific instrumentors that track request/response cycles.
- Django: Integrated through middleware and OpenTelemetry Django instrumentation.
- Flask: Supported via WSGI wrapping.
Sources: mkdocs.yml:130-140, pyproject.toml:79-87
HTTP Clients
Instrumentation for HTTP clients ensures that distributed tracing headers are propagated to downstream services.
- HTTPX: Captures outbound requests as spans.
- Requests: Integrated via the
opentelemetry-instrumentation-requestspackage. - AIOHTTP: Supports both client-side and server-side tracing.
Sources: mkdocs.yml:157-160, pyproject.toml:88-90
Databases
Database integrations capture queries, execution times, and connection metadata.
- SQLAlchemy & Psycopg: Standard SQL database instrumentation.
- Asyncpg: For high-performance asynchronous PostgreSQL operations.
- NoSQL: Supports PyMongo and Redis.
Sources: mkdocs.yml:148-156, pyproject.toml:91-100
System & Infrastructure
- System Metrics: Optional collection of CPU, memory, and network metrics via
logfire.instrument_system_metrics(). - AWS Lambda: Specific handler wrapping to ensure spans are flushed before the runtime freezes.
Sources: logfire/_internal/config.py:789-835, mkdocs.yml:179-181
Configuration and Initialization
Integrations are typically initialized after the global logfire.configure() call. This ensures that the underlying OpenTelemetry providers are ready to receive data.
Configuration Options
| Parameter | Type | Description |
|---|---|---|
service_name | str | Name identifying the service in the UI. |
send_to_logfire | bool | str | Whether to export data to the Logfire backend. |
metrics | MetricsOptions | Configuration for metric views and readers. |
scrubbing | ScrubbingOptions | Rules for redacting sensitive data from integrations. |
distributed_tracing | bool | Enables/disables trace context propagation across clients. |
| Sources: logfire/_internal/config.py:276-350 |
AWS Lambda Special Handling
Because AWS Lambda freezes the execution environment after a request, Logfire patches the LambdaRuntimeClient to ensure a force_flush() occurs at the end of every invocation.
This sequence ensures no telemetry data is trapped in the SDK buffer when the Lambda container is paused.
Sources: logfire/_internal/config.py:789-831
The CLI and Auto-Instrumentation
The Logfire CLI provides tools for inspecting the environment and recommending integrations based on installed packages.
Inspection and Recommendations
The command logfire inspect analyzes the current Python environment and suggests opentelemetry-instrumentation-* packages that should be installed to enable auto-tracing for detected libraries.
Sources: logfire/_internal/cli/init.py:117-128, tests/test_cli.py:190-210
Key CLI Commands
logfire run: Runs a script with automatic instrumentation enabled.logfire inspect: Recommends missing instrumentation packages.logfire auth: Authenticates the local client for data export.
Sources: logfire/_internal/cli/init.py:220-250, README.md:46-51
Data Scrubbing in Integrations
System and client integrations often process sensitive data (e.g., SQL queries with literals or HTTP headers). Logfire provides a Scrubber that is integrated into the MainSpanProcessorWrapper. This processor redacts sensitive information before the data is serialized and exported.
Sources: logfire/_internal/config.py:126-128, 478-485
Summary
Logfire's integration system provides a high-level, developer-friendly interface to OpenTelemetry. By combining manual tracing with extensive auto-instrumentation for popular frameworks and clients, it creates a comprehensive observability web. Critical system-level behaviors, such as the unique lifecycle requirements of AWS Lambda, are handled automatically, ensuring data reliability across different execution environments.