From 6e2eeeedcc9611b3fff7e9597ba620707f0c3d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Fortin?= Date: Thu, 2 Oct 2025 09:30:36 -0400 Subject: [PATCH 1/2] Allow creation of Metric from datastructure without __name__ 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. --- prometheus_api_client/metric.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/prometheus_api_client/metric.py b/prometheus_api_client/metric.py index 3ff971e..0ecb899 100644 --- a/prometheus_api_client/metric.py +++ b/prometheus_api_client/metric.py @@ -54,10 +54,9 @@ def __init__(self, metric, oldest_data_datetime=None): self.metric_values = metric.metric_values self.oldest_data_datetime = oldest_data_datetime else: - self.metric_name = metric["metric"]["__name__"] + self.metric_name = metric["metric"].pop("__name__", None) self.label_config = deepcopy(metric["metric"]) self.oldest_data_datetime = oldest_data_datetime - del self.label_config["__name__"] # if it is a single value metric change key name if "value" in metric: From 0a45ee4053b5dacb6e9f6df570f9e82d19847635 Mon Sep 17 00:00:00 2001 From: Anand Sanmukhani Date: Wed, 3 Dec 2025 21:40:26 -0600 Subject: [PATCH 2/2] Update prometheus_api_client/metric.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- prometheus_api_client/metric.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/prometheus_api_client/metric.py b/prometheus_api_client/metric.py index 0ecb899..e02832b 100644 --- a/prometheus_api_client/metric.py +++ b/prometheus_api_client/metric.py @@ -54,8 +54,10 @@ def __init__(self, metric, oldest_data_datetime=None): self.metric_values = metric.metric_values self.oldest_data_datetime = oldest_data_datetime else: - self.metric_name = metric["metric"].pop("__name__", None) + self.metric_name = metric["metric"].get("__name__", None) self.label_config = deepcopy(metric["metric"]) + if "__name__" in self.label_config: + del self.label_config["__name__"] self.oldest_data_datetime = oldest_data_datetime # if it is a single value metric change key name