Skip to content

Conversation

@zzstoatzz
Copy link
Collaborator

@zzstoatzz zzstoatzz commented Nov 6, 2025

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:

PREFECT_LOGFIRE_SAMPLING_HEAD_RATE=0.1              # 10% of traces
PREFECT_LOGFIRE_SAMPLING_LEVEL_THRESHOLD=warn       # Always log warn+
PREFECT_LOGFIRE_SAMPLING_DURATION_THRESHOLD=5.0     # Always log >5s spans
PREFECT_LOGFIRE_SAMPLING_BACKGROUND_RATE=0.01       # 1% tail sampling

Implementation

Boolean Environment Variable Fix

Fixes #19360 where PREFECT_LOGFIRE_ENABLED=0 doesn'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's TypeAdapter(bool) to properly parse boolean environment variables.

OpenAPI Schema Fix

Excludes /admin/settings endpoint from the OpenAPI schema to prevent Settings type pollution.

Problem

The /admin/settings endpoint returns the full prefect.settings.Settings object, 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 to ui-v2/src/api/prefect.ts every time someone modified a file matching src/prefect/server/api/*, polluting PRs with unrelated changes.

Solution

  • Added include_in_schema=False to the /admin/settings endpoint (src/prefect/server/api/admin.py:15)
  • Updated test_server_routes_match_openapi_schema to allow this exclusion
  • The UI doesn't need these type definitions since it just displays settings as raw JSON

Verification

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_schema
  • Verify logfire sampling configuration via environment variables
  • Verify PREFECT_LOGFIRE_ENABLED=0 properly disables logfire
  • Verify pre-commit hooks generate stable TypeScript schema

Closes #19360
Supersedes #19362

@zzstoatzz zzstoatzz force-pushed the nate/logfire-sampling-and-bool-fix branch from 08bfe64 to 33de4f8 Compare November 6, 2025 21:19
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 6, 2025

CodSpeed Performance Report

Merging #19372 will not alter performance

Comparing nate/logfire-sampling-and-bool-fix (a284483) with main (abcb29c)

Summary

✅ 2 untouched

@zzstoatzz zzstoatzz force-pushed the nate/logfire-sampling-and-bool-fix branch from e17e071 to 33de4f8 Compare November 6, 2025 21:24
…e /admin/settings from OpenAPI schema

**Logfire sampling configuration:**

introduces configurable logfire sampling via environment variables to reduce
span budget consumption for deployments with high traffic.

environment variables (all optional with sane defaults):
- PREFECT_LOGFIRE_SAMPLING_HEAD_RATE: 0.1 (10% of traces)
- PREFECT_LOGFIRE_SAMPLING_LEVEL_THRESHOLD: "warn"
- PREFECT_LOGFIRE_SAMPLING_DURATION_THRESHOLD: 5.0 seconds
- PREFECT_LOGFIRE_SAMPLING_BACKGROUND_RATE: 0.01 (1% tail sampling)

centralizes logfire configuration in prefect._internal.observability module
to avoid duplication and ensure consistent behavior.

fixes #19360 where PREFECT_LOGFIRE_ENABLED=0 doesn't properly disable logfire.
the fix uses Pydantic's TypeAdapter for proper boolean parsing that handles
"0", "false", "False", etc.

supersedes #19362

**OpenAPI schema fix:**

excludes /admin/settings endpoint from OpenAPI schema to prevent Settings type
pollution. the endpoint returns the full prefect.settings.Settings object,
which was causing FastAPI to include the entire Settings model tree
(APISettings, CLISettings, etc.) in the generated schema.

this was causing the pre-commit hook to regenerate ui-v2/src/api/prefect.ts
with 2000+ lines of spurious Settings type definitions every time someone
modified a file matching src/prefect/server/api/.*, polluting PRs with
unrelated changes.

the UI doesn't need these types since it just displays settings as raw JSON.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@zzstoatzz zzstoatzz force-pushed the nate/logfire-sampling-and-bool-fix branch from a1c560c to a284483 Compare November 6, 2025 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PREFECT_LOGFIRE_ENABLED env var check is not properly handling false values

2 participants