Move the metrics update to get_metrics

It is not necessary to update the metrics when get every metric in
one compute monitor plugin, just update once when get metrics for
the monitor plugin.

Change-Id: Ib665188964de8318f6d94343891666c674111c8c
This commit is contained in:
Shuangtai Tian 2014-02-18 11:35:28 +08:00
parent 6dab229484
commit b4bce92c31
2 changed files with 15 additions and 18 deletions

View File

@ -83,28 +83,20 @@ class ResourceMonitorBase(object):
which means a timestamp should be added into the returned value.
That is, a tuple (value, timestamp) is returned.
The timestamp is not the time when the function is called but probably
when the value the function returns was retrieved.
Actually the value is retrieved by the internal method
_update_data(). Because we don't allow _update_data() is called
so frequently. So, the value is read from the cache which was got in
the last call sometimes.
The timestamp is the time when we update the value in the _data.
If users want to use this decorator, they need to implement class
method _update_data() and variable _data.
If users hope to define how the timestamp is got by themselves,
they should not use this decorator in their own classes.
"""
def wrapper(cls, **kwargs):
cls._update_data()
return func(cls, **kwargs), cls._data.get("timestamp", None)
return wrapper
def _update_data(self):
"""Method to update the metrics data.
Each subclass should implement this method to update metrics.
It will be called in the decorator add_timestamp.
Each subclass can implement this method to update metrics
into _data. It will be called in get_metrics.
"""
pass
@ -129,6 +121,7 @@ class ResourceMonitorBase(object):
:returns: a list to tell the current metrics
"""
data = []
self._update_data()
for name, func in self.metric_map.iteritems():
ret = func(self, **kwargs)
data.append(self._populate(name, ret[0], ret[1]))

View File

@ -20,15 +20,18 @@ from nova import test
class FakeResourceMonitor(monitors.ResourceMonitorBase):
def get_metric_names(self):
return ["foo.metric1", "foo.metric2"]
def _update_data(self):
self._data['foo.metric1'] = '1000'
self._data['foo.metric2'] = '99.999'
self._data['timestamp'] = '123'
def get_metrics(self):
data = []
data.append(self._populate('foo.metric1', '1000'))
data.append(self._populate('foo.metric2', '99.999'))
@monitors.ResourceMonitorBase.add_timestamp
def _get_foo_metric1(self, **kwargs):
return self._data.get("foo.metric1")
return data
@monitors.ResourceMonitorBase.add_timestamp
def _get_foo_metric2(self, **kwargs):
return self._data.get("foo.metric2")
class FakeMonitorClass1(monitors.ResourceMonitorBase):
@ -96,6 +99,7 @@ class ResourceMonitorBaseTestCase(test.TestCase):
metrics = {}
for metric in metrics_raw:
self.assertIn(metric['name'], names)
self.assertEqual(metric["timestamp"], '123')
metrics[metric['name']] = metric['value']
self.assertEqual(metrics["foo.metric1"], '1000')