Skip to content

Commit

Permalink
Fix failed metric names.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dashmage committed Dec 1, 2023
1 parent ae32c80 commit 76ae3b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
12 changes: 8 additions & 4 deletions prometheus_hardware_exporter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions tests/unit/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,21 +1294,34 @@ def test_redfish_create_smart_storage_health_metric_payload(self):
)

def test_collector_fetch_failed(self):
class NoCollectorSuffix(MegaRAIDCollector):
"""Dummy class whose name doesn't end with "Collector".
Inherits from an existing collector for required methods to test suffix removal.
"""

pass

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"},
),
(
NoCollectorSuffix,
"nocollectorsuffix_collector_failed",
{"collector": "nocollectorsuffix"},
),
]:
collector = collector_cls(Mock())
Expand Down

0 comments on commit 76ae3b7

Please sign in to comment.