From 99c2721738bb118c86a7ab0ed4935d7a5abe14c5 Mon Sep 17 00:00:00 2001 From: Jelle van der Waa Date: Mon, 26 Aug 2024 13:41:27 +0200 Subject: [PATCH] last fixups --- src/cockpit/channels/pcp.py | 16 ++++++++-------- test/pytest/test_pcp.py | 3 +-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/cockpit/channels/pcp.py b/src/cockpit/channels/pcp.py index 60344a63fdb..de775aeda34 100644 --- a/src/cockpit/channels/pcp.py +++ b/src/cockpit/channels/pcp.py @@ -284,7 +284,7 @@ def convert_metric_description(self, context: 'pmapi.pmContext', metric: JsonObj pm_units = pm_desc.units if units: try: - [pm_units, factor] = context.pmParseUnitsStr(units) + [parsed_units, factor] = context.pmParseUnitsStr(units) except pmapi.pmErr as exc: if exc.errno() == c_api.PM_ERR_NAME: raise ChannelError('not-found', message=f'no such metric: {name}') from None @@ -293,8 +293,8 @@ def convert_metric_description(self, context: 'pmapi.pmContext', metric: JsonObj self.try_convert_unit(context, pm_desc, pm_units) - if units != pm_units or factor != 1.0: - units = pm_units + if units != parsed_units or factor != 1.0: + pm_units = parsed_units return MetricInfo(pmid=pm_ids[0], name=name, @@ -373,8 +373,7 @@ def send_meta(self, results: 'pmapi.pmResult', context: 'pmapi.pmContext') -> No instance_desc = context.pmNameInDom(metric_desc.desc, value.inst) insts.append(instance_desc) - if len(insts) > 0: - desc['instances'] = insts + desc['instances'] = insts metrics.append(desc) @@ -441,9 +440,10 @@ def sample(self, context: 'pmapi.pmContext', archive_batch: int, limit: int, tot self.send_meta(results, context) else: if self.needs_meta_update(results): - # Flush all metrics and send new meta - self.send_updates(fetched) - fetched.clear() + # Flush all metrics and send new meta, but only if there is data + if fetched: + self.send_updates(fetched) + fetched.clear() self.send_meta(results, context) fetched.append(self.parse_fetched_results(context, results)) diff --git a/test/pytest/test_pcp.py b/test/pytest/test_pcp.py index 2e3eaddc053..3cf3bfc84ba 100644 --- a/test/pytest/test_pcp.py +++ b/test/pytest/test_pcp.py @@ -576,7 +576,6 @@ async def test_pcp_disk_metrics(transport, disk_metrics_archive): assert len(metrics) == 9 network_metric = metrics[8] assert network_metric['name'] == 'network.interface.total.bytes' - assert 'instances' not in network_metric _, data = await transport.next_frame() data = json.loads(data) @@ -586,7 +585,7 @@ async def test_pcp_disk_metrics(transport, disk_metrics_archive): meta = json.loads(data) network_metric = metrics[8] assert network_metric['name'] == 'network.interface.total.bytes' - assert 'instances' not in network_metric + assert network_metric['instances'] == [] # Another meta assert_metrics_meta(meta, str(disk_metrics_archive), 1597663540413, 1000)