feat(litestar): default prometheus group_path=True to bound cardinality#144
Merged
Merged
Conversation
Litestar's PrometheusConfig defaults group_path=False, so the `path` metric label records the raw URL. Parameterized routes then mint one series per distinct value and grow the registry unbounded, causing memory growth (see litestar-org/litestar#4891). Add LitestarConfig.prometheus_group_path (default True) so the label uses the route template. The instrument merges {"group_path": <field>, **prometheus_additional_params} so the dict still overrides the field without a kwarg collision, keeping the existing group_path-via-additional_params workaround working. FastAPI is unaffected: prometheus_fastapi_instrumentator already labels by route template. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lesnik512
added a commit
that referenced
this pull request
Jul 1, 2026
Notes for the Litestar Prometheus group_path=True default (PR #144). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Litestar's
PrometheusConfigdefaultsgroup_path=False, so thepathmetric label records the raw URL. Any route with path parameters then mints one time series per distinct value, growing the registry without bound. In production this shows up as steadily climbing memory / OOM on the scraping Prometheus. It hits users silently because it is the default.Reproduced (isolated processes so the global
prometheus_clientregistry doesn't leak series between runs), 5 requests to one parameterized route:Change
LitestarConfig.prometheus_group_path: bool = True— thepathlabel uses the route template (/users/{id}), bounding cardinality.LitestarPrometheusInstrument.bootstrapmerges{"group_path": <field>, **prometheus_additional_params}. Precedence:prometheus_additional_params["group_path"]>prometheus_group_path> Litestar's ownFalse. The merge (not a plaingroup_path=kwarg) avoids a "multiple values for keyword argument" crash for anyone already passinggroup_pathvia the dict — the documented workaround keeps working.prometheus_fastapi_instrumentatoralready labels by route template.Compatibility
Behavior-changing default: the
pathlabel now holds the route template instead of the raw URL. Dashboards keyed on raw paths will shift. Revert per-app withprometheus_group_path=False.Tests
TDD (red before green), unique route prefixes per test to avoid global-registry contamination:
prometheus_group_path=False-> raw path recordedprometheus_additional_params={"group_path": False}overrides theTruedefaultjust lint-ciclean (ruff +ty+ planning validator); full suite 204 passed, 100% coverage.Docs
architecture/instruments.md— new "Prometheus path-label cardinality (Litestar)" subsection.docs/integrations/litestar.md— user-facing Prometheus section.Upstream
Filed litestar-org/litestar#4891 proposing a warning (2.x) and default flip (next major). This PR gives lite-bootstrap users the safe default now, regardless of upstream.
🤖 Generated with Claude Code