Instrumented Libraries¶
Auto-instrumented libraries for distributed tracing.
Overview¶
Logfire automatically instruments common libraries:
# src/infrastructure/otel/logfire.py
def _instrument_libraries() -> None:
logfire.instrument_django(
excluded_urls=".*/v1/health",
is_sql_commentor_enabled=True,
)
logfire.instrument_celery(propagate_trace_context=True)
logfire.instrument_requests()
logfire.instrument_psycopg(
enable_commenter=True,
commenter_options=CommenterOptions(
db_driver=True,
dbapi_level=True,
),
)
logfire.instrument_httpx()
logfire.instrument_redis()
logfire.instrument_pydantic()
Django Instrumentation¶
Features¶
- HTTP request/response tracing
- URL routing information
- Request headers and body
- Response status and timing
Excluded URLs¶
Health check endpoints are excluded to reduce noise:
SQL Commenter¶
Adds trace context to SQL queries:
Celery Instrumentation¶
Features¶
- Task execution tracing
- Task arguments and results
- Retry tracking
- Queue information
Context Propagation¶
propagate_trace_context=True links task traces to parent spans:
PostgreSQL (psycopg) Instrumentation¶
logfire.instrument_psycopg(
enable_commenter=True,
commenter_options=CommenterOptions(
db_driver=True,
dbapi_level=True,
),
)
Features¶
- Query execution timing
- Query text (sanitized)
- Connection information
- Error tracking
SQL Comments¶
Adds metadata to queries:
Requests Instrumentation¶
Features¶
- Outbound HTTP request tracing
- URL, method, headers
- Response status and timing
- Error tracking
Example Trace¶
HTTPX Instrumentation¶
Features¶
- Async HTTP client tracing
- Same capabilities as requests
- Connection pooling visibility
Redis Instrumentation¶
Features¶
- Redis command tracing
- Key operations
- Timing information
- Error tracking
Example Trace¶
Pydantic Instrumentation¶
Features¶
- Validation timing
- Validation errors
- Model serialization
What Gets Traced¶
Automatic Traces¶
| Library | Traced Operations |
|---|---|
| Django | HTTP requests, responses |
| Celery | Task execution |
| psycopg | SQL queries |
| requests | HTTP client calls |
| httpx | Async HTTP calls |
| Redis | Cache operations |
| Pydantic | Validation |
Sample Trace¶
[HTTP] POST /v1/users/
├── [Pydantic] Validate CreateUserSchema
├── [DB] SELECT * FROM users WHERE email = 'test@example.com'
├── [DB] INSERT INTO users (email, username, ...) VALUES (...)
├── [Redis] SET session:abc123 "..." EX 86400
└── [HTTP] 200 OK (45ms)
Custom Instrumentation¶
Add manual spans for application-specific operations:
Disabling Instrumentation¶
To disable specific instrumentation, don't call it:
def _instrument_libraries() -> None:
logfire.instrument_django(...)
logfire.instrument_celery(...)
# logfire.instrument_redis() # Disabled
Performance Impact¶
Instrumentation adds minimal overhead:
- ~1-5ms per request for basic tracing
- SQL commenter adds ~0.1ms per query
- Background span export (non-blocking)
Related Topics¶
- Logfire — Logfire setup
- Sensitive Data Scrubbing — Data protection
- Logging — Console logging