Allow creation of Metric from data structure without __name__#306
Merged
4n4nd merged 2 commits into4n4nd:masterfrom Dec 4, 2025
Merged
Allow creation of Metric from data structure without __name__#3064n4nd merged 2 commits into4n4nd:masterfrom
4n4nd merged 2 commits into4n4nd:masterfrom
Conversation
When applying a function to a time series in a query to Prometheus, the Prometheus server returns a data structure without __name__ key. Apart from that aspect, the time series could still be used to create a Metric object. This PR proposes to set "metric_name" to None when __name__ is missing.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR modifies the Metric class initialization to support creating Metric objects from Prometheus query results that lack a __name__ key, which commonly occurs when applying functions to time series. The change sets metric_name to None when __name__ is absent, rather than raising a KeyError.
Key Changes
- Modified line 57 to use
pop("__name__", None)instead of direct dictionary access - Removed the explicit deletion of
__name__fromlabel_configon the previously existing line (now removed), relying onpop()to handle removal
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
prometheus_api_client/metric.py
Outdated
Comment on lines
57
to
58
| self.metric_name = metric["metric"].pop("__name__", None) | ||
| self.label_config = deepcopy(metric["metric"]) |
There was a problem hiding this comment.
The PR allows metric_name to be None, but there's no test coverage for this new behavior. Consider adding a test case that verifies:
- A Metric can be created from data without a
__name__key - The
metric_nameattribute is correctly set toNone - The
label_configcontains all other labels - Operations like
__eq__and__str__work correctly withNonemetric_name
Example test:
def test_metric_without_name(self):
"""Test creating a Metric from data without __name__ key."""
test_metric = Metric(
metric={
"metric": {"instance": "localhost", "job": "test"},
"value": [1627485628.789, "26.82"],
}
)
self.assertIsNone(test_metric.metric_name)
self.assertEqual(test_metric.label_config, {"instance": "localhost", "job": "test"})Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Owner
|
@copilot open a new pull request to apply changes based on the comments in this thread |
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.
When applying a function to a time series in a query to Prometheus, the Prometheus server returns a data structure without
__name__key. Apart from that aspect, the time series could still be used to create a Metric object.This PR proposes to set "metric_name" to None when
__name__is missing.