Allow apostrophes in dimension names and values

Change-Id: Iec6a2b4190bdcc24978e9f5921a68cd00fd7834e
This commit is contained in:
Ryan Brandt 2015-08-27 13:22:02 -06:00
parent 908310e7c8
commit e7eabee61d
2 changed files with 39 additions and 5 deletions

View File

@ -17,8 +17,9 @@ log = logging.getLogger(__name__)
# submitted for the timestamp passed into the flush() function.
RECENT_POINT_THRESHOLD_DEFAULT = 3600
invalid_chars = "<>={}(),'\"\\\\;&"
restricted_chars = re.compile('[' + invalid_chars + ']')
invalid_chars = "<>={}(),\"\\\\;&"
restricted_dimension_chars = re.compile('[' + invalid_chars + ']')
restricted_name_chars = re.compile('[' + invalid_chars + ' ' + ']')
class InvalidMetricName(Exception):
@ -157,7 +158,7 @@ class MetricsAggregator(object):
if len(k) > 255 or len(k) < 1:
log.error("invalid length for dimension key {0}: {1} -> {2}".format(k, name, dimensions))
raise InvalidDimensionKey
if restricted_chars.search(k) or re.match('^_', k):
if restricted_dimension_chars.search(k) or re.match('^_', k):
log.error("invalid characters in dimension key {0}: {1} -> {2}".format(k, name, dimensions))
raise InvalidDimensionKey
@ -169,7 +170,7 @@ class MetricsAggregator(object):
log.error("invalid length dimension value {0} for key {1}: {2} -> {3}".format(v, k, name,
dimensions))
raise InvalidDimensionValue
if restricted_chars.search(v):
if restricted_dimension_chars.search(v):
log.error("invalid characters in dimension value {0} for key {1}: {2} -> {3}".format(v, k, name,
dimensions))
raise InvalidDimensionValue
@ -180,7 +181,7 @@ class MetricsAggregator(object):
if len(name) > 255 or len(name) < 1:
log.error("invalid length for metric name: {0} -> {1}".format(name, dimensions))
raise InvalidMetricName
if restricted_chars.search(name):
if restricted_name_chars.search(name):
log.error("invalid characters in metric name: {0} -> {1}".format(name, dimensions))
raise InvalidMetricName

View File

@ -3,6 +3,14 @@ import unittest
import monasca_agent.common.aggregator as aggregator
import monasca_agent.common.metrics as metrics_pkg
# a few valid characters to test
valid_name_chars = ".'_-"
invalid_name_chars = " <>={}(),\"\\\\;&"
# a few valid characters to test
valid_dimension_chars = " .'_-"
invalid_dimension_chars = "<>={}(),\"\\\\;&"
class TestMetricsAggregator(unittest.TestCase):
def setUp(self):
@ -190,3 +198,28 @@ class TestMetricsAggregator(unittest.TestCase):
dimensions=dimensions,
value_meta=value_meta,
exception=aggregator.InvalidValue)
def testValidNameChars(self):
for c in valid_name_chars:
self.submit_metric('test{}counter'.format(c), 2,
dimensions={"test-key": "test-value"})
def testInvalidNameChars(self):
for c in invalid_name_chars:
self.submit_metric('test{}counter'.format(c), 2,
dimensions={"test-key": "test-value"},
exception=aggregator.InvalidMetricName)
def testValidDimensionChars(self):
for c in valid_dimension_chars:
self.submit_metric('test-counter', 2,
dimensions={"test{}key".format(c): "test{}value".format(c)})
def testInvalidDimensionChars(self):
for c in invalid_dimension_chars:
self.submit_metric('test-counter', 2,
dimensions={'test{}key'.format(c): 'test-value'},
exception=aggregator.InvalidDimensionKey)
self.submit_metric('test-counter', 2,
dimensions={'test-key': 'test{}value'.format(c)},
exception=aggregator.InvalidDimensionValue)