Move metric validation from agent to monasca common
This case other repos can use this code to validate metrics instead of writing their own. Change-Id: If402441cd1ec80c4b81c125eea678d01b4687d90
This commit is contained in:
parent
1ad8b00ae2
commit
a62f3cdd19
0
monasca_common/tests/validation/__init__.py
Normal file
0
monasca_common/tests/validation/__init__.py
Normal file
369
monasca_common/tests/validation/test_metric_validation.py
Normal file
369
monasca_common/tests/validation/test_metric_validation.py
Normal file
@ -0,0 +1,369 @@
|
|||||||
|
# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
# implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import monasca_common.validation.metrics as metric_validator
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
# 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 TestMetricValidation(unittest.TestCase):
|
||||||
|
def test_valid_single_metric(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
metric_validator.validate(metric)
|
||||||
|
|
||||||
|
def test_valid_metrics(self):
|
||||||
|
metrics = [
|
||||||
|
{"name": "name1",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 1.0},
|
||||||
|
{"name": "name2",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"value_meta": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 2.0}
|
||||||
|
]
|
||||||
|
metric_validator.validate(metrics)
|
||||||
|
|
||||||
|
def test_valid_metric_unicode_dimension_value(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"dimensions": {unichr(2440): 'B', 'B': 'C', 'D': 'E'},
|
||||||
|
"value": 5}
|
||||||
|
metric_validator.validate(metric)
|
||||||
|
|
||||||
|
def test_valid_metric_unicode_dimension_key(self):
|
||||||
|
metric = {"name": 'test_metric_name',
|
||||||
|
"dimensions": {'A': 'B', 'B': unichr(920), 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
metric_validator.validate(metric)
|
||||||
|
|
||||||
|
def test_valid_metric_unicode_metric_name(self):
|
||||||
|
metric = {"name": unichr(6021),
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
metric_validator.validate(metric)
|
||||||
|
|
||||||
|
def test_invalid_metric_name(self):
|
||||||
|
metric = {'name': "TooLarge" * 255,
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidMetricName,
|
||||||
|
"invalid length for metric name",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_metric_name_empty(self):
|
||||||
|
metric = {"name": "",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidMetricName,
|
||||||
|
"invalid length for metric name",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_metric_name_non_str(self):
|
||||||
|
metric = {"name": 133,
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidMetricName,
|
||||||
|
"invalid metric name type",
|
||||||
|
metric_validator.validate,
|
||||||
|
metric)
|
||||||
|
|
||||||
|
def test_invalid_metric_restricted_characters(self):
|
||||||
|
metric = {"name": '"Foo"',
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidMetricName,
|
||||||
|
"invalid characters in metric name",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_empty_key(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A': 'B', '': 'C', 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionKey,
|
||||||
|
"invalid length for dimension key",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_empty_value(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A': 'B', 'B': 'C', 'D': ''},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionValue,
|
||||||
|
"invalid length for dimension value",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_non_str_key(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A': 'B', 4: 'C', 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionKey,
|
||||||
|
"invalid dimension key type",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_non_str_value(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A': 13.3, 'B': 'C', 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionValue,
|
||||||
|
"invalid dimension value type",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_key_length(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A'*256: 'B', 'B': 'C', 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionKey,
|
||||||
|
"invalid length for dimension key",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_value_length(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A': 'B', 'B': 'C'*256, 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionValue,
|
||||||
|
"invalid length for dimension value",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_key_restricted_characters(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A': 'B', 'B': 'C', '(D)': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionKey,
|
||||||
|
"invalid characters in dimension key",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_value_restricted_characters(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'A': 'B;', 'B': 'C', 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionValue,
|
||||||
|
"invalid characters in dimension value",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_key_leading_underscore(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {'_A': 'B', 'B': 'C', 'D': 'E'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionKey,
|
||||||
|
"invalid characters in dimension key",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_value(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": "value"}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidValue,
|
||||||
|
"invalid value type",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_valid_name_chars(self):
|
||||||
|
for c in valid_name_chars:
|
||||||
|
metric = {"name": 'test{}counter'.format(c),
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
metric_validator.validate(metric)
|
||||||
|
|
||||||
|
def test_invalid_name_chars(self):
|
||||||
|
for c in invalid_name_chars:
|
||||||
|
metric = {"name": 'test{}counter'.format(c),
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidMetricName,
|
||||||
|
"invalid characters in metric name",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_valid_dimension_chars(self):
|
||||||
|
for c in valid_dimension_chars:
|
||||||
|
metric = {"name": "test_name",
|
||||||
|
"dimensions":
|
||||||
|
{"test{}key".format(c): "test{}value".format(c)},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
metric_validator.validate(metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_key_chars(self):
|
||||||
|
for c in invalid_dimension_chars:
|
||||||
|
metric = {"name": "test_name",
|
||||||
|
"dimensions": {'test{}key'.format(c): 'test-value'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionKey,
|
||||||
|
"invalid characters in dimension key",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_dimension_value_chars(self):
|
||||||
|
for c in invalid_dimension_chars:
|
||||||
|
metric = {"name": "test_name",
|
||||||
|
"dimensions": {'test-key': 'test{}value'.format(c)},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidDimensionValue,
|
||||||
|
"invalid characters in dimension value",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_too_many_value_meta(self):
|
||||||
|
value_meta = {}
|
||||||
|
for i in range(0, 17):
|
||||||
|
value_meta['key{}'.format(i)] = 'value{}'.format(i)
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"value_meta": value_meta,
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidValueMeta,
|
||||||
|
"Too many valueMeta entries",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_empty_value_meta_key(self):
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"value_meta": {'': 'BBB'},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidValueMeta,
|
||||||
|
"valueMeta name cannot be empty",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_too_long_value_meta_key(self):
|
||||||
|
key = "K"
|
||||||
|
for i in range(0, metric_validator.VALUE_META_NAME_MAX_LENGTH):
|
||||||
|
key = "{}{}".format(key, "1")
|
||||||
|
value_meta = {key: 'BBB'}
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"value_meta": value_meta,
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidValueMeta,
|
||||||
|
"valueMeta name too long",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_too_large_value_meta(self):
|
||||||
|
value_meta_value = ""
|
||||||
|
num_value_meta = 10
|
||||||
|
for i in range(0, metric_validator.VALUE_META_VALUE_MAX_LENGTH / num_value_meta):
|
||||||
|
value_meta_value = '{}{}'.format(value_meta_value, '1')
|
||||||
|
value_meta = {}
|
||||||
|
for i in range(0, num_value_meta):
|
||||||
|
value_meta['key{}'.format(i)] = value_meta_value
|
||||||
|
metric = {"name": "test_metric_name",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"value_meta": value_meta,
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidValueMeta,
|
||||||
|
"Unable to serialize valueMeta into JSON",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_invalid_timestamp(self):
|
||||||
|
metric = {'name': 'test_metric_name',
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": "invalid_timestamp",
|
||||||
|
"value": 5}
|
||||||
|
self.assertRaisesRegexp(
|
||||||
|
metric_validator.InvalidTimeStamp,
|
||||||
|
"invalid timestamp type",
|
||||||
|
metric_validator.validate, metric)
|
||||||
|
|
||||||
|
def test_valid_metrics_by_components(self):
|
||||||
|
metrics = [
|
||||||
|
{"name": "name1",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 1.0},
|
||||||
|
{"name": "name2",
|
||||||
|
"dimensions": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"value_meta": {"key1": "value1",
|
||||||
|
"key2": "value2"},
|
||||||
|
"timestamp": 1405630174123,
|
||||||
|
"value": 2.0}
|
||||||
|
]
|
||||||
|
for i in xrange(len(metrics)):
|
||||||
|
metric_validator.validate_name(metrics[i]['name'])
|
||||||
|
metric_validator.validate_value(metrics[i]['value'])
|
||||||
|
metric_validator.validate_timestamp(metrics[i]['timestamp'])
|
||||||
|
if 'dimensions' in metrics[i]:
|
||||||
|
metric_validator.validate_dimensions(metrics[i]['dimensions'])
|
||||||
|
if 'value_meta' in metrics[i]:
|
||||||
|
metric_validator.validate_value_meta(metrics[i]['value_meta'])
|
0
monasca_common/validation/__init__.py
Normal file
0
monasca_common/validation/__init__.py
Normal file
155
monasca_common/validation/metrics.py
Normal file
155
monasca_common/validation/metrics.py
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
# implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import re
|
||||||
|
import ujson
|
||||||
|
|
||||||
|
# This is used to ensure that metrics with a timestamp older than
|
||||||
|
# RECENT_POINT_THRESHOLD_DEFAULT seconds (or the value passed in to
|
||||||
|
# the MetricsAggregator constructor) get discarded rather than being
|
||||||
|
# input into the incorrect bucket. Currently, the MetricsAggregator
|
||||||
|
# does not support submitting values for the past, and all values get
|
||||||
|
# submitted for the timestamp passed into the flush() function.
|
||||||
|
RECENT_POINT_THRESHOLD_DEFAULT = 3600
|
||||||
|
VALUE_META_MAX_NUMBER = 16
|
||||||
|
VALUE_META_VALUE_MAX_LENGTH = 2048
|
||||||
|
VALUE_META_NAME_MAX_LENGTH = 255
|
||||||
|
|
||||||
|
INVALID_CHARS = "<>={}(),\"\\\\;&"
|
||||||
|
RESTRICTED_DIMENSION_CHARS = re.compile('[' + INVALID_CHARS + ']')
|
||||||
|
RESTRICTED_NAME_CHARS = re.compile('[' + INVALID_CHARS + ' ' + ']')
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidMetricName(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidDimensionKey(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidDimensionValue(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidValue(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidValueMeta(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidTimeStamp(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def validate(metrics):
|
||||||
|
if isinstance(metrics, list):
|
||||||
|
for metric in metrics:
|
||||||
|
validate_metric(metric)
|
||||||
|
else:
|
||||||
|
validate_metric(metrics)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_metric(metric):
|
||||||
|
validate_name(metric['name'])
|
||||||
|
validate_value(metric['value'])
|
||||||
|
validate_timestamp(metric['timestamp'])
|
||||||
|
if "dimensions" in metric:
|
||||||
|
validate_dimensions(metric['dimensions'])
|
||||||
|
if "value_meta" in metric:
|
||||||
|
validate_value_meta(metric['value_meta'])
|
||||||
|
|
||||||
|
|
||||||
|
def validate_value_meta(value_meta):
|
||||||
|
if len(value_meta) > VALUE_META_MAX_NUMBER:
|
||||||
|
msg = "Too many valueMeta entries {0}, limit is {1}: valueMeta {2}".\
|
||||||
|
format(len(value_meta), VALUE_META_MAX_NUMBER, value_meta)
|
||||||
|
raise InvalidValueMeta(msg)
|
||||||
|
for key, value in value_meta.iteritems():
|
||||||
|
if not key:
|
||||||
|
raise InvalidValueMeta("valueMeta name cannot be empty: key={}, "
|
||||||
|
"value={}".format(key, value))
|
||||||
|
if len(key) > VALUE_META_NAME_MAX_LENGTH:
|
||||||
|
msg = "valueMeta name too long: {0} must be {1} characters or " \
|
||||||
|
"less".format(key, VALUE_META_NAME_MAX_LENGTH)
|
||||||
|
raise InvalidValueMeta(msg)
|
||||||
|
|
||||||
|
try:
|
||||||
|
value_meta_json = ujson.dumps(value_meta)
|
||||||
|
if len(value_meta_json) > VALUE_META_VALUE_MAX_LENGTH:
|
||||||
|
msg = "valueMeta name value combinations must be {0} characters " \
|
||||||
|
"or less: valueMeta {1}".format(VALUE_META_VALUE_MAX_LENGTH,
|
||||||
|
value_meta)
|
||||||
|
raise InvalidValueMeta(msg)
|
||||||
|
except Exception:
|
||||||
|
raise InvalidValueMeta("Unable to serialize valueMeta into JSON")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_dimensions(dimensions):
|
||||||
|
for k, v in dimensions.iteritems():
|
||||||
|
if not isinstance(k, (str, unicode)):
|
||||||
|
msg = "invalid dimension key type: " \
|
||||||
|
"{0} in {1} is not a string type".format(k, dimensions)
|
||||||
|
raise InvalidDimensionKey(msg)
|
||||||
|
if len(k) > 255 or len(k) < 1:
|
||||||
|
msg = "invalid length for dimension key {0}: {1}".\
|
||||||
|
format(k, dimensions)
|
||||||
|
raise InvalidDimensionKey(msg)
|
||||||
|
if RESTRICTED_DIMENSION_CHARS.search(k) or re.match('^_', k):
|
||||||
|
msg = "invalid characters in dimension key {0}: {1}".\
|
||||||
|
format(k, dimensions)
|
||||||
|
raise InvalidDimensionKey(msg)
|
||||||
|
|
||||||
|
if not isinstance(v, (str, unicode)):
|
||||||
|
msg = "invalid dimension value type: {0} for key {1} must be a " \
|
||||||
|
"string: {2}".format(v, k, dimensions)
|
||||||
|
raise InvalidDimensionValue(msg)
|
||||||
|
if len(v) > 255 or len(v) < 1:
|
||||||
|
msg = "invalid length for dimension value {0} in key {1}: {2}".\
|
||||||
|
format(v, k, dimensions)
|
||||||
|
raise InvalidDimensionValue(msg)
|
||||||
|
if RESTRICTED_DIMENSION_CHARS.search(v):
|
||||||
|
msg = "invalid characters in dimension value {0} for key {1}: " \
|
||||||
|
"{2}".format(v, k, dimensions)
|
||||||
|
raise InvalidDimensionValue(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_name(name):
|
||||||
|
if not isinstance(name, (str, unicode)):
|
||||||
|
msg = "invalid metric name type: {0} is not a string type ".format(
|
||||||
|
name)
|
||||||
|
raise InvalidMetricName(msg)
|
||||||
|
if len(name) > 255 or len(name) < 1:
|
||||||
|
msg = "invalid length for metric name: {0}".format(name)
|
||||||
|
raise InvalidMetricName(msg)
|
||||||
|
if RESTRICTED_NAME_CHARS.search(name):
|
||||||
|
msg = "invalid characters in metric name: {0}".format(name)
|
||||||
|
raise InvalidMetricName(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_value(value):
|
||||||
|
if not isinstance(value, (int, long, float)):
|
||||||
|
msg = "invalid value type: {0} is not a number type for metric".\
|
||||||
|
format(value)
|
||||||
|
raise InvalidValue(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_timestamp(timestamp):
|
||||||
|
if not isinstance(timestamp, (int, float)):
|
||||||
|
msg = "invalid timestamp type: {0} is not a number type for " \
|
||||||
|
"metric".format(timestamp)
|
||||||
|
raise InvalidTimeStamp(msg)
|
Loading…
Reference in New Issue
Block a user