Skip to content

Commit

Permalink
use hashmap to quickly find matching resource def
Browse files Browse the repository at this point in the history
we shouldn't waste our time looping through resources and searching
metrics to find a match. just build hashmap and find the resource
based on metric.

this removes fnmatch functionality because we don't match on
wildcards and the code itself actually requires metric names to be
explicit to create_metrics

Change-Id: I2398247270217759c876ab5a9b60038dad79a9d3
  • Loading branch information
chungg authored and dchavoll committed Aug 15, 2018
1 parent 5ebc250 commit e516e82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
23 changes: 8 additions & 15 deletions ceilometer/publisher/gnocchi.py
Expand Up @@ -104,12 +104,6 @@ def _ensure_list(value):
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:
Expand Down Expand Up @@ -195,6 +189,8 @@ def __init__(self, conf, parsed_url):
[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]
Expand Down Expand Up @@ -271,9 +267,11 @@ def gnocchi_project_id(self):
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 (
Expand All @@ -284,11 +282,6 @@ def _is_gnocchi_activity(self, sample):
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)
Expand Down Expand Up @@ -320,7 +313,7 @@ def publish_samples(self, data):
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" %
Expand Down
6 changes: 5 additions & 1 deletion ceilometer/tests/unit/publisher/test_gnocchi.py
Expand Up @@ -376,7 +376,11 @@ def test_match(self):
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):
Expand Down

0 comments on commit e516e82

Please sign in to comment.