Support for wildcard in pipeline

This patch will support for wildcard in pipeline.yaml so we
can exclude things like !storage.objects.*

This patch is dedicated to Fei Long Wang who triggered my thought on the
subject and proposed a first implementation.

docImpact
Fixes bug 1209128

Change-Id: I09a805ea7c9313b45152672a63981d5d4c263601
This commit is contained in:
Julien Danjou 2013-08-26 07:31:27 +08:00
parent 056e883373
commit 2363058cd3
2 changed files with 52 additions and 7 deletions

View File

@ -16,6 +16,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import fnmatch
import itertools
import os
import operator
@ -241,14 +242,27 @@ class Pipeline(object):
def support_meter(self, meter_name):
meter_name = self._variable_meter_name(meter_name)
if ('!' + meter_name) in self.meters:
return False
if '*' in self.meters:
return True
elif self.meters[0][0] == '!':
return not ('!' + meter_name) in self.meters
# Special case: if we only have negation, we suppose the default it
# allow
if all(meter.startswith('!') for meter in self.meters):
default = True
else:
return meter_name in self.meters
default = False
# Support wildcard like storage.* and !disk.*
# Start with negation, we consider that the order is deny, allow
if any(fnmatch.fnmatch(meter_name, meter[1:])
for meter in self.meters
if meter[0] == '!'):
return False
if any(fnmatch.fnmatch(meter_name, meter)
for meter in self.meters
if meter[0] != '!'):
return True
return default
def flush(self, ctxt):
"""Flush data after all samples have been injected to pipeline."""

View File

@ -325,6 +325,37 @@ class TestPipeline(base.TestCase):
self.assertTrue(pipeline_manager.pipelines[0].support_meter('b'))
self.assertFalse(pipeline_manager.pipelines[0].support_meter('c'))
def test_wildcard_and_excluded_wildcard_counters(self):
counter_cfg = ['*', '!disk.*']
self.pipeline_cfg[0]['counters'] = counter_cfg
pipeline_manager = pipeline.PipelineManager(self.pipeline_cfg,
self.transformer_manager)
self.assertFalse(pipeline_manager.pipelines[0].
support_meter('disk.read.bytes'))
self.assertTrue(pipeline_manager.pipelines[0].support_meter('cpu'))
def test_included_counter_and_wildcard_counters(self):
counter_cfg = ['cpu', 'disk.*']
self.pipeline_cfg[0]['counters'] = counter_cfg
pipeline_manager = pipeline.PipelineManager(self.pipeline_cfg,
self.transformer_manager)
self.assertTrue(pipeline_manager.pipelines[0].
support_meter('disk.read.bytes'))
self.assertTrue(pipeline_manager.pipelines[0].support_meter('cpu'))
self.assertFalse(pipeline_manager.pipelines[0].
support_meter('instance'))
def test_excluded_counter_and_excluded_wildcard_counters(self):
counter_cfg = ['!cpu', '!disk.*']
self.pipeline_cfg[0]['counters'] = counter_cfg
pipeline_manager = pipeline.PipelineManager(self.pipeline_cfg,
self.transformer_manager)
self.assertFalse(pipeline_manager.pipelines[0].
support_meter('disk.read.bytes'))
self.assertFalse(pipeline_manager.pipelines[0].support_meter('cpu'))
self.assertTrue(pipeline_manager.pipelines[0].
support_meter('instance'))
def test_multiple_pipeline(self):
self.pipeline_cfg.append({
'name': 'second_pipeline',