From 8a6afe28a2f82287d76c041e442198c4552bde86 Mon Sep 17 00:00:00 2001 From: Ashley James Date: Fri, 1 Dec 2023 18:43:02 +0530 Subject: [PATCH] Fix failed metric names. Previously, the failed metric had "collector" twice in its name. Eg: "ipmidcmicollector_collector_failed" This change aims to remove the duplication. Previous example becomes "ipmidcmi_collector_failed" The label is also changed similarly to reflect the new name. Unit test was also modified to reflect this change. --- prometheus_hardware_exporter/core.py | 12 ++++++++---- tests/unit/test_collector.py | 12 ++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/prometheus_hardware_exporter/core.py b/prometheus_hardware_exporter/core.py index ba9434c..b0b2d9a 100644 --- a/prometheus_hardware_exporter/core.py +++ b/prometheus_hardware_exporter/core.py @@ -96,14 +96,18 @@ def failed_metrics(self) -> Iterable[Metric]: Yields: metrics: the internal metrics """ - name = self.__class__.__name__ + # if `__class_.__name__` is "IpmiDcmiCollector" + # then `name` becomes "ipmidcmi" + suffix = "collector" + class_name = self.__class__.__name__.lower() + name = class_name[: -len(suffix)] if class_name.endswith(suffix) else class_name metric = GaugeMetricFamily( - name=f"{name.lower()}_collector_failed", - documentation=f"{name} Collector failed to fetch metrics", + name=f"{name}_collector_failed", + documentation=f"{name} collector failed to fetch metrics", labels=["collector"], ) metric.add_metric( - labels=[self.__class__.__name__], + labels=[name], value=1, ) yield metric diff --git a/tests/unit/test_collector.py b/tests/unit/test_collector.py index eb421f7..38537f0 100644 --- a/tests/unit/test_collector.py +++ b/tests/unit/test_collector.py @@ -1297,18 +1297,18 @@ def test_collector_fetch_failed(self): for collector_cls, expected_name, expected_labels in [ ( MegaRAIDCollector, - "megaraidcollector_collector_failed", - {"collector": "MegaRAIDCollector"}, + "megaraid_collector_failed", + {"collector": "megaraid"}, ), ( RedfishCollector, - "redfishcollector_collector_failed", - {"collector": "RedfishCollector"}, + "redfish_collector_failed", + {"collector": "redfish"}, ), ( IpmiSensorsCollector, - "ipmisensorscollector_collector_failed", - {"collector": "IpmiSensorsCollector"}, + "ipmisensors_collector_failed", + {"collector": "ipmisensors"}, ), ]: collector = collector_cls(Mock())