Remove use of granularity and just retrieve last metric collected in OpenStack plugin 13/7613/4
authorBenjamin Diaz <bdiaz@whitestack.com>
Wed, 5 Jun 2019 14:39:07 +0000 (11:39 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Tue, 11 Jun 2019 17:49:01 +0000 (14:49 -0300)
Change-Id: I8e52cda7bffb069a76b3834964100399a1c5746b
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
docker/Dockerfile
osm_mon/collector/vnf_collectors/openstack.py
osm_mon/core/mon.yaml
osm_mon/tests/unit/collector/vnf_collectors/test_openstack.py

index 5271205..27cb519 100644 (file)
@@ -44,7 +44,6 @@ ENV OSMMON_DATABASE_DRIVER mongo
 ENV OSMMON_DATABASE_URI mongodb://mongo:27017
 
 ENV OSMMON_SQL_DATABASE_URI sqlite:///mon_sqlite.db
-ENV OSMMON_OPENSTACK_DEFAULT_GRANULARITY 300
 ENV OSMMON_GLOBAL_REQUEST_TIMEOUT 10
 ENV OSMMON_GLOBAL_LOGLEVEL INFO
 ENV OSMMON_VCA_HOST localhost
index 209448c..b2ecef0 100644 (file)
@@ -19,8 +19,6 @@
 # For those usages not covered by the Apache License, Version 2.0 please
 # contact: bdiaz@whitestack.com or glavado@whitestack.com
 ##
-import datetime
-import json
 import logging
 from enum import Enum
 from typing import List
@@ -80,14 +78,6 @@ class OpenstackCollector(BaseVimCollector):
         vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name)
         return vdur['vim-id']
 
-    def _get_granularity(self, vim_account_id: str):
-        creds = CollectorUtils.get_credentials(vim_account_id)
-        vim_config = json.loads(creds.config)
-        if 'granularity' in vim_config:
-            return int(vim_config['granularity'])
-        else:
-            return int(self.conf.get('openstack', 'default_granularity'))
-
     def collect(self, vnfr: dict) -> List[Metric]:
         nsr_id = vnfr['nsr-id-ref']
         vnf_member_index = vnfr['member-vnf-index-ref']
@@ -133,8 +123,7 @@ class OpenstackCollector(BaseVimCollector):
             ceilometer.client.capabilities.get()
             return ceilometer
         except EndpointNotFound:
-            granularity = self._get_granularity(vim_account_id)
-            gnocchi = GnocchiBackend(vim_account_id, granularity)
+            gnocchi = GnocchiBackend(vim_account_id)
             gnocchi.client.status.get()
             return gnocchi
 
@@ -166,10 +155,9 @@ class OpenstackBackend:
 
 class GnocchiBackend(OpenstackBackend):
 
-    def __init__(self, vim_account_id: str, granularity: int):
+    def __init__(self, vim_account_id: str):
         self.client = self._build_gnocchi_client(vim_account_id)
         self.neutron = self._build_neutron_client(vim_account_id)
-        self.granularity = granularity
 
     def _build_gnocchi_client(self, vim_account_id: str) -> gnocchi_client.Client:
         sess = OpenstackBackend.get_session(vim_account_id)
@@ -193,8 +181,6 @@ class GnocchiBackend(OpenstackBackend):
             raise Exception('Unknown metric type %s' % metric_type.value)
 
     def _collect_interface_one_metric(self, metric_name, resource_id, interface_name):
-        delta = 10 * self.granularity
-        start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
         ports = self.neutron.list_ports(name=interface_name, device_id=resource_id)
         if not ports or not ports['ports']:
             raise Exception(
@@ -205,23 +191,19 @@ class GnocchiBackend(OpenstackBackend):
         interfaces = self.client.resource.search(resource_type='instance_network_interface',
                                                  query={'=': {'name': tap_name}})
         measures = self.client.metric.get_measures(metric_name,
-                                                   start=start_date,
                                                    resource_id=interfaces[0]['id'],
-                                                   granularity=self.granularity)
+                                                   limit=1)
         return measures[-1][2] if measures else None
 
     def _collect_interface_all_metric(self, openstack_metric_name, resource_id):
-        delta = 10 * self.granularity
-        start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
         total_measure = None
         interfaces = self.client.resource.search(resource_type='instance_network_interface',
                                                  query={'=': {'instance_id': resource_id}})
         for interface in interfaces:
             try:
                 measures = self.client.metric.get_measures(openstack_metric_name,
-                                                           start=start_date,
                                                            resource_id=interface['id'],
-                                                           granularity=self.granularity)
+                                                           limit=1)
                 if measures:
                     if not total_measure:
                         total_measure = 0.0
@@ -233,14 +215,11 @@ class GnocchiBackend(OpenstackBackend):
         return total_measure
 
     def _collect_instance_metric(self, openstack_metric_name, resource_id):
-        delta = 10 * self.granularity
-        start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
         value = None
         try:
             measures = self.client.metric.get_measures(openstack_metric_name,
-                                                       start=start_date,
                                                        resource_id=resource_id,
-                                                       granularity=self.granularity)
+                                                       limit=1)
             if measures:
                 value = measures[-1][2]
         except gnocchiclient.exceptions.NotFound as e:
index 19e8f3a..c094dda 100644 (file)
@@ -53,6 +53,3 @@ vca:
   host: localhost
   secret: secret
   user: admin
-
-openstack:
-  default_granularity: 300
\ No newline at end of file
index 0d813a2..2949dc6 100644 (file)
@@ -49,11 +49,12 @@ class CollectorTest(TestCase):
                                                                                        '+00:00')), 60.0, 0.0333070363)]
         build_gnocchi_client.return_value = mock_gnocchi_client
 
-        backend = GnocchiBackend('test_uuid', 60)
+        backend = GnocchiBackend('test_uuid')
         value = backend._collect_instance_metric('cpu_utilization', 'test_resource_id')
         self.assertEqual(value, 0.0333070363)
-        mock_gnocchi_client.metric.get_measures.assert_called_once_with('cpu_utilization', granularity=60,
-                                                                        resource_id='test_resource_id', start=mock.ANY)
+        mock_gnocchi_client.metric.get_measures.assert_called_once_with('cpu_utilization',
+                                                                        limit=1,
+                                                                        resource_id='test_resource_id')
 
     @mock.patch.object(GnocchiBackend, '_build_neutron_client')
     @mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
@@ -86,11 +87,11 @@ class CollectorTest(TestCase):
         build_gnocchi_client.return_value = mock_gnocchi_client
         build_neutron_client.return_value = mock_neutron_client
 
-        backend = GnocchiBackend('test_uuid', 60)
+        backend = GnocchiBackend('test_uuid')
         value = backend._collect_interface_one_metric('packets_received', 'test_resource_id', 'eth0')
         self.assertEqual(value, 0.0333070363)
-        mock_gnocchi_client.metric.get_measures.assert_called_once_with('packets_received', granularity=60,
-                                                                        resource_id='test_id', start=mock.ANY)
+        mock_gnocchi_client.metric.get_measures.assert_called_once_with('packets_received', resource_id='test_id',
+                                                                        limit=1)
         mock_neutron_client.list_ports.assert_called_once_with(device_id='test_resource_id', name='eth0')
 
     @mock.patch.object(GnocchiBackend, '_build_neutron_client')
@@ -109,10 +110,10 @@ class CollectorTest(TestCase):
 
         build_gnocchi_client.return_value = mock_gnocchi_client
 
-        backend = GnocchiBackend('test_uuid', 60)
+        backend = GnocchiBackend('test_uuid')
         value = backend._collect_interface_all_metric('packets_received', 'test_resource_id')
         self.assertEqual(value, 0.0666140726)
-        mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', granularity=60,
-                                                                resource_id='test_id_1', start=mock.ANY)
-        mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', granularity=60,
-                                                                resource_id='test_id_2', start=mock.ANY)
+        mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_1',
+                                                                limit=1)
+        mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_2',
+                                                                limit=1)