Sort column order in cli output

Change-Id: Ie2db01d02483cf6767d5d54159ebf203e20dd2c5
This commit is contained in:
Jaromir Wysoglad 2024-04-10 02:13:13 -04:00
parent ed042e2b16
commit 3dcd00ad33
2 changed files with 68 additions and 0 deletions

View File

@ -128,3 +128,70 @@ class Metrics2ColsTest(testtools.TestCase):
ret = metric_utils.metrics2cols(input_metrics)
self.assertEqual(expected, ret)
def test_metrics2cols_column_ordering(self):
metric = {
'value': [
1234567,
5
],
'metric': {
'a_label1': 'value1',
'b_label2': 'value2',
}
}
input_metrics = [prometheus_client.PrometheusMetric(metric)]
expected = (['a_label1', 'b_label2', 'value'],
[['value1', 'value2', 5]])
ret = metric_utils.metrics2cols(input_metrics)
self.assertEqual(expected, ret)
metric = {
'value': [
1234567,
5
],
'metric': {
'b_label1': 'value1',
'a_label2': 'value2',
}
}
input_metrics = [prometheus_client.PrometheusMetric(metric)]
expected = (['a_label2', 'b_label1', 'value'],
[['value2', 'value1', 5]])
ret = metric_utils.metrics2cols(input_metrics)
self.assertEqual(expected, ret)
metric1 = {
'value': [
1234567,
5
],
'metric': {
'b_label1': 'value1',
'a_label2': 'value2',
}
}
metric2 = {
'value': [
1234567,
5
],
'metric': {
'b_label1': 'value1',
'a_label2': 'value2',
'd_label3': 'value3',
'c_label4': 'value4',
}
}
input_metrics = [prometheus_client.PrometheusMetric(metric1),
prometheus_client.PrometheusMetric(metric2)]
expected = (['a_label2', 'b_label1', 'c_label4', 'd_label3', 'value'],
[['value2', 'value1', '', '', 5],
['value2', 'value1', 'value4', 'value3', 5]]
)
ret = metric_utils.metrics2cols(input_metrics)
self.assertEqual(expected, ret)

View File

@ -100,6 +100,7 @@ def format_labels(d: dict) -> str:
def metrics2cols(m):
# get all label keys
cols = list(set().union(*(d.labels.keys() for d in m)))
cols.sort()
cols.append("value")
fields = []
for metric in m: