Bug 1976: MON fails to collect disk metrics fixed 36/11836/2
authorsritharan <priyadarshini@tataelxsi.co.in>
Mon, 4 Apr 2022 13:46:15 +0000 (13:46 +0000)
committerpalsus <subhankar.pal@aricent.com>
Wed, 20 Apr 2022 04:46:11 +0000 (06:46 +0200)
Change-Id: I696bd5f9ce609cf3069d8733d01719204462f954
Signed-off-by: sritharan <priyadarshini@tataelxsi.co.in>
osm_mon/collector/vnf_collectors/openstack.py
osm_mon/tests/unit/collector/vnf_collectors/test_openstack.py

index ef366f9..aba4788 100644 (file)
@@ -76,11 +76,19 @@ INTERFACE_METRICS = [
     "packets_sent",
 ]
 
+INSTANCE_DISK = [
+    "disk_read_ops",
+    "disk_write_ops",
+    "disk_read_bytes",
+    "disk_write_bytes",
+]
+
 
 class MetricType(Enum):
     INSTANCE = "instance"
     INTERFACE_ALL = "interface_all"
     INTERFACE_ONE = "interface_one"
+    INSTANCEDISK = 'instancedisk'
 
 
 class OpenstackCollector(BaseVimCollector):
@@ -196,7 +204,10 @@ class OpenstackCollector(BaseVimCollector):
 
     def _get_metric_type(self, metric_name: str) -> MetricType:
         if metric_name not in INTERFACE_METRICS:
-            return MetricType.INSTANCE
+            if metric_name not in INSTANCE_DISK:
+                return MetricType.INSTANCE
+            else:
+                return MetricType.INSTANCEDISK
         else:
             return MetricType.INTERFACE_ALL
 
@@ -228,6 +239,9 @@ class GnocchiBackend(OpenstackBackend):
         elif metric_type == MetricType.INSTANCE:
             return self._collect_instance_metric(metric_name, resource_id)
 
+        elif metric_type == MetricType.INSTANCEDISK:
+            return self._collect_instance_disk_metric(metric_name, resource_id)
+
         else:
             raise Exception("Unknown metric type %s" % metric_type.value)
 
@@ -256,6 +270,25 @@ class GnocchiBackend(OpenstackBackend):
                 )
         return total_measure
 
+    def _collect_instance_disk_metric(self, openstack_metric_name, resource_id):
+        value = None
+        instances = self.client.resource.search(
+            resource_type='instance_disk',
+            query={'=': {'instance_id': resource_id}},
+        )
+        for instance in instances:
+            try:
+                measures = self.client.metric.get_measures(
+                    openstack_metric_name, resource_id=instance['id'], limit=1
+                )
+                if measures:
+                    value = measures[-1][2]
+
+            except gnocchiclient.exceptions.NotFound as e:
+                log.debug("No metric %s found for instance disk %s: %s", openstack_metric_name,
+                          instance['id'], e)
+        return value
+
     def _collect_instance_metric(self, openstack_metric_name, resource_id):
         value = None
         try:
index 991d7f9..0fc5e99 100644 (file)
@@ -198,3 +198,50 @@ class CollectorTest(TestCase):
         mock_gnocchi_client.metric.get_measures.assert_any_call(
             "packets_received", resource_id="test_id_2", limit=1
         )
+
+    @mock.patch.object(GnocchiBackend, "_build_neutron_client")
+    @mock.patch.object(GnocchiBackend, "_build_gnocchi_client")
+    def test_collect_instance_disk_metric(
+        self, build_gnocchi_client, build_neutron_client
+    ):
+        mock_gnocchi_client = mock.Mock()
+        mock_gnocchi_client.resource.search.return_value = [
+            {"id": "test_id"},
+        ]
+        mock_gnocchi_client.metric.get_measures.return_value = [
+            (
+                datetime.datetime(
+                    2019,
+                    4,
+                    12,
+                    15,
+                    43,
+                    tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
+                ),
+                60.0,
+                0.0225808,
+            ),
+            (
+                datetime.datetime(
+                    2019,
+                    4,
+                    12,
+                    15,
+                    44,
+                    tzinfo=datetime.timezone(datetime.timedelta(0), "+00:00"),
+                ),
+                60.0,
+                230848,
+            ),
+        ]
+
+        build_gnocchi_client.return_value = mock_gnocchi_client
+
+        backend = GnocchiBackend({"_id": "test_uuid"})
+        value = backend._collect_instance_disk_metric(
+            "disk_read_bytes", "test_resource_id"
+        )
+        self.assertEqual(value, 230848)
+        mock_gnocchi_client.metric.get_measures.assert_any_call(
+            "disk_read_bytes", resource_id="test_id", limit=1
+        )