Add logfire sampling configuration and fix boolean env var handling #19372
+485
−105
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Logfire Sampling Configuration
This PR introduces configurable logfire sampling to reduce span budget consumption for high-traffic Prefect server deployments.
Configuration
All settings are optional with sane defaults:
Implementation
src/prefect/_internal/observability.pyto centralize logfire configurationserver.pyandconfigurations.pyBaseSettingsfor configuration with environment variablesTypeAdapterfor proper boolean parsing (fixes PREFECT_LOGFIRE_ENABLED env var check is not properly handling false values #19360)Boolean Environment Variable Fix
Fixes #19360 where
PREFECT_LOGFIRE_ENABLED=0doesn't properly disable logfire.Previously,
os.getenv("PREFECT_LOGFIRE_ENABLED")returned a truthy string for any non-empty value, including "0", "false", etc. Now uses Pydantic'sTypeAdapter(bool)to properly parse boolean environment variables.OpenAPI Schema Fix
Excludes
/admin/settingsendpoint from the OpenAPI schema to prevent Settings type pollution.Problem
The
/admin/settingsendpoint returns the fullprefect.settings.Settingsobject, causing FastAPI to include the entire Settings model tree (APISettings, CLISettings, ServerSettings, LoggingSettings, etc.) in the generated OpenAPI schema. This resulted in 2000+ lines of spurious Settings type definitions being added toui-v2/src/api/prefect.tsevery time someone modified a file matchingsrc/prefect/server/api/*, polluting PRs with unrelated changes.Solution
include_in_schema=Falseto the/admin/settingsendpoint (src/prefect/server/api/admin.py:15)test_server_routes_match_openapi_schemato allow this exclusionVerification
After this change, the pre-commit hook's OpenAPI sync generates a stable schema without the Settings types.
Test Plan
uv run pytest tests/client/test_client_routes.py::test_server_routes_match_openapi_schemaPREFECT_LOGFIRE_ENABLED=0properly disables logfireCloses #19360
Supersedes #19362