Merge "use hashmap to quickly find matching resource def"

This commit is contained in:
Zuul
2018-01-15 10:30:04 +00:00
committed by Gerrit Code Review
2 changed files with 13 additions and 16 deletions

View File

@@ -104,12 +104,6 @@ class ResourcesDefinition(object):
return value
return [value]
def metric_match(self, metric_name):
for t in self.cfg['metrics']:
if fnmatch.fnmatch(metric_name, t):
return True
return False
def support_events(self):
for e in ["event_create", "event_delete", "event_update"]:
if e in self.cfg:
@@ -195,6 +189,8 @@ class GnocchiPublisher(publisher.ConfigPublisherBase):
[conf.dispatcher_gnocchi.archive_policy])[-1]
self.resources_definition = self._load_resources_definitions(
conf, archive_policy, resources_definition_file)
self.metric_map = dict((metric, rd) for rd in self.resources_definition
for metric in rd.metrics)
timeout = options.get('timeout',
[conf.dispatcher_gnocchi.request_timeout])[-1]
@@ -271,9 +267,11 @@ class GnocchiPublisher(publisher.ConfigPublisherBase):
return self._gnocchi_project_id
def _is_swift_account_sample(self, sample):
return bool([rd for rd in self.resources_definition
if rd.cfg['resource_type'] == 'swift_account'
and rd.metric_match(sample.name)])
try:
return (self.metric_map[sample.name].cfg['resource_type']
== 'swift_account')
except KeyError:
return False
def _is_gnocchi_activity(self, sample):
return (self.filter_project and self.gnocchi_project_id and (
@@ -284,11 +282,6 @@ class GnocchiPublisher(publisher.ConfigPublisherBase):
self._is_swift_account_sample(sample))
))
def _get_resource_definition_from_metric(self, metric_name):
for rd in self.resources_definition:
if rd.metric_match(metric_name):
return rd
def _get_resource_definition_from_event(self, event_type):
for rd in self.resources_definition:
operation = rd.event_match(event_type)
@@ -320,7 +313,7 @@ class GnocchiPublisher(publisher.ConfigPublisherBase):
stats['metrics'] += 1
samples = list(samples)
rd = self._get_resource_definition_from_metric(metric_name)
rd = self.metric_map.get(metric_name)
if rd is None:
if metric_name not in self._already_logged_metric_names:
LOG.warning("metric %s is not handled by Gnocchi" %

View File

@@ -201,7 +201,11 @@ class PublisherTest(base.BaseTestCase):
resource, "low", plugin_manager)
operation = rd.event_match("image.delete")
self.assertEqual('delete', operation)
self.assertEqual(True, rd.metric_match('image'))
def test_metric_match(self):
pub = gnocchi.GnocchiPublisher(self.conf.conf,
netutils.urlsplit("gnocchi://"))
self.assertIn('image.size', pub.metric_map['image.size'].metrics)
@mock.patch('ceilometer.publisher.gnocchi.LOG')
def test_broken_config_load(self, mylog):