Profiling
GAIA includes optional PyInstrument middleware for detailed call stack profiling of FastAPI requests. This provides deep insights into performance bottlenecks and function execution times.Overview
The profiling system uses PyInstrument to provide statistical profiling with:- Call stack visualization: See exactly which functions consume the most time
- Interactive HTML reports: Drill down into performance bottlenecks
- Minimal overhead: Statistical sampling keeps performance impact low
- Production ready: Safe defaults and error resilience
Configuration
Profiling is completely optional and disabled by default. All configuration is done via environment variables.Environment Variables
Add these to your.env
file:
Configuration Options
ENABLE_PROFILING
- Type:
boolean
- Default:
false
- Description: Master switch for profiling functionality. Must be explicitly enabled.
- Security: Disabled by default for safety in all environments.
PROFILING_SAMPLE_RATE
- Type:
float
(0.0 to 1.0) - Default:
0.1
(10%) - Description: Fraction of requests to automatically profile in the background.
- Examples:
0.0
= No automatic profiling (manual only)0.01
= Profile 1% of requests (recommended for high-traffic production)0.1
= Profile 10% of requests (good for development/staging)1.0
= Profile all requests (only for debugging, high overhead)
PROFILING_MAX_DEPTH
- Type:
integer
- Default:
50
- Description: Maximum call stack depth to capture in profiling reports.
- Impact: Higher values provide more detail but increase memory usage and report complexity.
- Recommendations:
- Development:
50-100
(detailed debugging) - Production:
30-50
(focused on main bottlenecks)
- Development:
PROFILING_ASYNC_MODE
- Type:
string
- Default:
"enabled"
- Options:
"enabled"
: Profile async code with thread awareness"disabled"
: Don’t profile async code specifically"strict"
: Strict async profiling (may impact performance)
- Description: Controls how PyInstrument handles async/await code profiling.
Usage
Manual Profiling
Add theprofile
query parameter to any request to get a detailed profiling report:
1
, true
, yes
(case-insensitive)
When manual profiling is requested:
- You receive an HTML profiling report instead of the normal JSON response
- The report shows detailed call stacks, execution times, and bottlenecks
- Save the HTML and open in a browser for interactive analysis
Automatic Sampling
WhenPROFILING_SAMPLE_RATE > 0
, requests are automatically profiled in the background:
- Sampled requests are profiled transparently
- Normal JSON responses are returned to clients
- Profiling results are logged to the application logs
- No impact on client experience or API contracts
Examples
Development Setup
Production Setup
Profiling API Requests
Troubleshooting
Profiling Not Working
- Check that
ENABLE_PROFILING=true
in your.env
file - Verify PyInstrument is installed:
pip install pyinstrument
- Look for profiling logs at application startup
- Try manual profiling first:
?profile=1
High Performance Impact
- Reduce
PROFILING_SAMPLE_RATE
(e.g., from0.1
to0.01
) - Lower
PROFILING_MAX_DEPTH
(e.g., from100
to30
) - Use
PROFILING_ASYNC_MODE=disabled
if async overhead is too high
Note: Profiling adds computational overhead. Use judiciously in production
environments and monitor the performance impact of your chosen sample rate.