Skip to content
Snippets Groups Projects
Commit 6b0d2e15 authored by Jan David Mol's avatar Jan David Mol
Browse files

Merge branch 'L2SS-1759-fix-metrics' into 'master'

L2SS-1759: Fix for attributes that return None and then break the prometheus_client

Closes L2SS-1759

See merge request !857
parents 3fb045b5 3e773635
No related branches found
No related tags found
1 merge request!857L2SS-1759: Fix for attributes that return None and then break the prometheus_client
......@@ -112,6 +112,10 @@ class AttributePoller:
try:
value = await self._read_attribute(attr_name)
# check if value could be read
if value is None:
continue
# update metric, if any
if attr_data["metric"]:
attr_data["metric"].set_value(value)
......
......@@ -159,6 +159,9 @@ class AttributeMetric:
def set_value(self, value: object):
"""A new value for the attribute is known. Feed it to the metric."""
if value is None:
raise ValueError(f"Invalid value for metric: {value}")
# set it, this class will take care of the default labels
if self.metric_class == Enum:
self._enum_value(value, self.static_label_values)
......
......@@ -563,6 +563,27 @@ class TestMetrics(base.TestCase):
metrics = parse_prometheus_response(proxy.prometheus_response())
self.assertIn("ds_foo", metrics)
def test_set_value_none(self):
"""Test behaviour of setting None as a value in a metric."""
@device_metrics()
class test_device(TestMetrics.TestDevice):
FOO = attribute(
doc="This is FOO",
dtype=str,
fget=lambda obj: None,
fset=lambda obj, value: None,
)
@command()
def test(device):
# writing None should fail
with self.assertRaises(ValueError):
device.metrics["FOO"].set_value(None)
with DeviceTestContext(test_device, process=False) as proxy:
proxy.test()
def test_track_value_modification(self):
"""Test whether metrics update if the attribute's value is accessed."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment