Control profiling with sampling rate and error thresholds
mainYou can reduce overhead in production by using profiler_sample_rate (a float between 0.0 and 1.0). You can also ensure critical failures are always captured by setting always_profile_errors=True, which forces profiling on 5xx responses even if they fall outside the sampling rate or slow-request threshold.
# Profile only 10% of requests
app.add_middleware(
PyInstrumentProfilerMiddleware,
server_app=app,
profiler_sample_rate=0.1,
)
# Profile nothing normally, but always profile 5xx errors
app.add_middleware(
PyInstrumentProfilerMiddleware,
server_app=app,
profiler_sample_rate=0.0,
always_profile_errors=True,
)