-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
feat(prometheus): support wildcard metric name in prometheus_metrics_… #34622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ademicho123
wants to merge
3
commits into
BerriAI:litellm_internal_staging
Choose a base branch
from
ademicho123:feat/prometheus-wildcard-label-filter-clean
base: litellm_internal_staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−9
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
161 changes: 161 additions & 0 deletions
161
tests/test_litellm/integrations/test_prometheus_wildcard_labels.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import sys | ||
| from typing import get_args | ||
|
|
||
| import pytest | ||
|
|
||
| sys.path.insert(0, "../../../..") | ||
|
|
||
| import litellm | ||
| from litellm.integrations.prometheus import PrometheusLogger | ||
| from litellm.types.integrations.prometheus import ( | ||
| DEFINED_PROMETHEUS_METRICS, | ||
| PROMETHEUS_METRICS_WILDCARD, | ||
| PrometheusMetricsConfig, | ||
| ) | ||
|
|
||
|
|
||
| def _bare_logger() -> PrometheusLogger: | ||
| """A PrometheusLogger instance with __init__ skipped, for testing the | ||
| config-parsing helpers in isolation without re-registering ~70 real | ||
| metrics against prometheus_client's global registry on every test.""" | ||
| return PrometheusLogger.__new__(PrometheusLogger) | ||
|
|
||
|
|
||
| def test_wildcard_sets_default_labels_for_every_metric(): | ||
| logger = _bare_logger() | ||
| configs = [ | ||
| PrometheusMetricsConfig( | ||
| group="defaults", | ||
| metrics=[PROMETHEUS_METRICS_WILDCARD], | ||
| include_labels=["hashed_api_key", "team"], | ||
| ) | ||
| ] | ||
|
|
||
| label_filters = logger._build_label_filters(configs) | ||
|
|
||
| assert set(label_filters.keys()) == set(get_args(DEFINED_PROMETHEUS_METRICS)) | ||
| for labels in label_filters.values(): | ||
| assert labels == ["hashed_api_key", "team"] | ||
|
|
||
|
|
||
| def test_named_group_overrides_wildcard_for_that_metric(): | ||
| logger = _bare_logger() | ||
| configs = [ | ||
| PrometheusMetricsConfig( | ||
| group="defaults", | ||
| metrics=[PROMETHEUS_METRICS_WILDCARD], | ||
| include_labels=["hashed_api_key", "team"], | ||
| ), | ||
| PrometheusMetricsConfig( | ||
| group="spend_needs_more", | ||
| metrics=["litellm_spend_metric"], | ||
| include_labels=["hashed_api_key", "team", "end_user"], | ||
| ), | ||
| ] | ||
|
|
||
| label_filters = logger._build_label_filters(configs) | ||
|
|
||
| assert label_filters["litellm_spend_metric"] == [ | ||
| "hashed_api_key", | ||
| "team", | ||
| "end_user", | ||
| ] | ||
| assert label_filters["litellm_total_tokens_metric"] == ["hashed_api_key", "team"] | ||
|
|
||
|
|
||
| def test_wildcard_group_does_not_disable_other_metrics(monkeypatch): | ||
| """A wildcard-only config should leave every metric enabled - it's a | ||
| label filter, not an allowlist.""" | ||
| logger = _bare_logger() | ||
| monkeypatch.setattr( | ||
| litellm, | ||
| "prometheus_metrics_config", | ||
| [ | ||
| { | ||
| "group": "defaults", | ||
| "metrics": [PROMETHEUS_METRICS_WILDCARD], | ||
| "include_labels": ["team"], | ||
| } | ||
| ], | ||
| ) | ||
|
|
||
| logger._parse_prometheus_config() | ||
|
|
||
| assert logger.enabled_metrics == set() | ||
| assert logger._is_metric_enabled("litellm_spend_metric") is True | ||
| assert logger._is_metric_enabled("litellm_mcp_tool_calls_total") is True | ||
|
|
||
|
|
||
| def test_wildcard_combined_with_named_enable_list(monkeypatch): | ||
| """Wildcard for labels + a separate group naming which metrics are | ||
| enabled - the two concerns are independent.""" | ||
| logger = _bare_logger() | ||
| monkeypatch.setattr( | ||
| litellm, | ||
| "prometheus_metrics_config", | ||
| [ | ||
| { | ||
| "group": "enabled", | ||
| "metrics": ["litellm_spend_metric", "litellm_total_tokens_metric"], | ||
| }, | ||
| { | ||
| "group": "defaults", | ||
| "metrics": [PROMETHEUS_METRICS_WILDCARD], | ||
| "include_labels": ["team"], | ||
| }, | ||
| ], | ||
| ) | ||
|
|
||
| label_filters = logger._parse_prometheus_config() | ||
|
|
||
| assert logger.enabled_metrics == { | ||
| "litellm_spend_metric", | ||
| "litellm_total_tokens_metric", | ||
| } | ||
| assert logger._is_metric_enabled("litellm_spend_metric") is True | ||
| assert logger._is_metric_enabled("litellm_cache_hits_metric") is False | ||
| assert label_filters["litellm_spend_metric"] == ["team"] | ||
|
|
||
| def test_wildcard_mixed_with_named_metric_in_same_group_raises(): | ||
| logger = _bare_logger() | ||
| configs = [ | ||
| PrometheusMetricsConfig( | ||
| group="broken", | ||
| metrics=[PROMETHEUS_METRICS_WILDCARD, "litellm_spend_metric"], | ||
| include_labels=["team"], | ||
| ) | ||
| ] | ||
|
|
||
| with pytest.raises(ValueError, match="mixes the wildcard"): | ||
| logger._validate_all_configurations(configs) | ||
|
|
||
|
|
||
| def test_wildcard_with_unknown_label_is_reported(): | ||
| logger = _bare_logger() | ||
| configs = [ | ||
| PrometheusMetricsConfig( | ||
| group="typo", | ||
| metrics=[PROMETHEUS_METRICS_WILDCARD], | ||
| include_labels=["taem"], # typo, should be "team" | ||
| ) | ||
| ] | ||
|
|
||
| results = logger._validate_all_configurations(configs) | ||
|
|
||
| assert results.has_errors | ||
| assert any("taem" in err.message for err in results.label_errors) | ||
|
|
||
|
|
||
| def test_wildcard_with_no_include_labels_is_a_noop(): | ||
| logger = _bare_logger() | ||
| configs = [ | ||
| PrometheusMetricsConfig( | ||
| group="pointless", | ||
| metrics=[PROMETHEUS_METRICS_WILDCARD], | ||
| include_labels=None, | ||
| ) | ||
| ] | ||
|
|
||
| label_filters = logger._build_label_filters(configs) | ||
|
|
||
| assert label_filters == {} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
prometheus_metrics_configcontainsmetrics: ["*"],PrometheusLogger.__init__passes the wildcard through the existing metric-name validator, which rejects it because it is not inDEFINED_PROMETHEUS_METRICSand raisesValueError. The production parser also lacks the wildcard expansion and enablement handling asserted by these tests, so the advertised configuration cannot apply default labels.Knowledge Base Used: Logging & Observability Integrations