Fix Prom exporter resource_name parsing

This changes the code to follow the original code in sg-core.
The resource_name label value in sg-core is being overwritten
when there is a "display name" and "name". It shouldn't be
duplicated.

It also removes the index variable, which is there in the
original implementation mostly to do this overwrite. I
don't think it's needed in this Python implementation.

Related sg-core code: https://github.com/infrawatch/sg-core/blob/master/plugins/handler/ceilometer-metrics/main.go#L269

Change-Id: I72971d51fa37b43e348605af9f6bb52cfa651369
This commit is contained in:
Jaromir Wysoglad 2024-10-15 10:16:27 -04:00
parent f680d44b2b
commit 1d56f8e072
2 changed files with 8 additions and 17 deletions

View File

@ -75,48 +75,40 @@ def _gen_labels(sample):
labels['keys'].append("type")
labels['values'].append(ctype)
index = 3
if (sample.get('counter_name', '') != '' and
sample.get('counter_name') is not None):
labels['keys'].append("counter")
labels['values'].append(sample['counter_name'])
index += 1
if (sample.get('project_id', '') != '' and
sample.get('project_id') is not None):
labels['keys'].append("project")
labels['values'].append(sample['project_id'])
index += 1
if (sample.get('project_name', '') != '' and
sample.get('project_name') is not None):
labels['keys'].append("project_name")
labels['values'].append(sample['project_name'])
index += 1
if (sample.get('user_id', '') != '' and
sample.get('user_id') is not None):
labels['keys'].append("user")
labels['values'].append(sample['user_id'])
index += 1
if (sample.get('user_name', '') != '' and
sample.get('user_name') is not None):
labels['keys'].append("user_name")
labels['values'].append(sample['user_name'])
index += 1
if (sample.get('counter_unit', '') != '' and
sample.get('counter_unit') is not None):
labels['keys'].append("unit")
labels['values'].append(sample['counter_unit'])
index += 1
if (sample.get('resource_id', '') != '' and
sample.get('resource_id') is not None):
labels['keys'].append("resource")
labels['values'].append(sample['resource_id'])
index += 1
if (sample.get('resource_metadata', '') != '' and
sample.get('resource_metadata') is not None):
@ -125,17 +117,16 @@ def _gen_labels(sample):
if (resource_metadata.get('host', '') != ''):
labels['keys'].append("vm_instance")
labels['values'].append(resource_metadata['host'])
index += 1
if (resource_metadata.get('display_name', '') != ''):
labels['keys'].append("resource_name")
labels['values'].append(resource_metadata['display_name'])
if (resource_metadata.get('name', '') != ''):
labels['keys'].append("resource_name")
if (labels['values'][index] if index < len(labels['values'])
else '' != ''):
labels['values'].append(labels['values'][index] + ":" +
if labels['keys'][-1] != 'resource_name':
labels['keys'].append("resource_name")
if len(labels['keys']) == len(labels['values']):
labels['values'][-1] = (labels['values'][-1] + ":" +
resource_metadata['name'])
else:
labels['values'].append(resource_metadata['name'])

View File

@ -227,14 +227,14 @@ class TestPromExporter(base.BaseTestCase):
slabels1['keys'] = ['disk', 'publisher', 'type', 'counter',
'project', 'user', 'unit', 'resource',
'vm_instance', 'resource_name',
'resource_name', 'server_group']
'server_group']
slabels1['values'] = ['read', 'ceilometer', 'device',
'disk.device.read.latency',
'd965489b7f894cbda89cd2e25bfd85a0',
'6e7d71415cd5401cbe103829c9c5dec2',
'ns',
'e536fff6-b20d-4aa5-ac2f-d15ac8b3af63-vda',
'e0d297f5df3b62ec73c8d42b', 'myserver',
'e0d297f5df3b62ec73c8d42b',
'myserver:instance-00000002', 'none']
label1 = prom_exporter._gen_labels(self.test_data[0])
self.assertDictEqual(label1, slabels1)
@ -243,14 +243,14 @@ class TestPromExporter(base.BaseTestCase):
slabels2['keys'] = ['memory', 'publisher', 'type', 'counter',
'project', 'user', 'unit', 'resource',
'vm_instance', 'resource_name',
'resource_name', 'server_group']
'server_group']
slabels2['values'] = ['e536fff6-b20d-4aa5-ac2f-d15ac8b3af63',
'ceilometer', 'usage', 'memory.usage',
'd965489b7f894cbda89cd2e25bfd85a0',
'6e7d71415cd5401cbe103829c9c5dec2', 'MB',
'e536fff6-b20d-4aa5-ac2f-d15ac8b3af63',
'e0d297f5df3b62ec73c8d42b',
'myserver', 'myserver:instance-00000002', 'none']
'myserver:instance-00000002', 'none']
label2 = prom_exporter._gen_labels(self.test_data[1])
self.assertDictEqual(label2, slabels2)