From 3dcd00ad334b51a10a36df080cb2b1cf59d63ce3 Mon Sep 17 00:00:00 2001
From: Jaromir Wysoglad <jwysogla@redhat.com>
Date: Wed, 10 Apr 2024 02:13:13 -0400
Subject: [PATCH] Sort column order in cli output

Change-Id: Ie2db01d02483cf6767d5d54159ebf203e20dd2c5
---
 observabilityclient/tests/unit/test_utils.py | 67 ++++++++++++++++++++
 observabilityclient/utils/metric_utils.py    |  1 +
 2 files changed, 68 insertions(+)

diff --git a/observabilityclient/tests/unit/test_utils.py b/observabilityclient/tests/unit/test_utils.py
index 2eabf43..d9d734d 100644
--- a/observabilityclient/tests/unit/test_utils.py
+++ b/observabilityclient/tests/unit/test_utils.py
@@ -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)
diff --git a/observabilityclient/utils/metric_utils.py b/observabilityclient/utils/metric_utils.py
index ea75de0..ba70b9a 100644
--- a/observabilityclient/utils/metric_utils.py
+++ b/observabilityclient/utils/metric_utils.py
@@ -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: