Add validation for 'nan' and infinity

Influx doesn't support these as metric values

Change-Id: Id6e9aaa3a21ac20d366e9a88d82ad153210d612b
This commit is contained in:
Ryan Brandt 2017-01-03 12:26:16 -07:00
parent 37e3a29713
commit 4679a9fd31
2 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,4 @@
# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
# (C) Copyright 2016-2017 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.
@ -209,7 +209,7 @@ class TestMetricValidation(unittest.TestCase):
"invalid characters in dimension key",
metric_validator.validate, metric)
def test_invalid_value(self):
def test_invalid_value_type(self):
metric = {"name": "test_metric_name",
"dimensions": {"key1": "value1",
"key2": "value2"},
@ -220,6 +220,20 @@ class TestMetricValidation(unittest.TestCase):
"invalid value type",
metric_validator.validate, metric)
def test_invalid_value(self):
metric = {"name": "test_metric_name",
"dimensions": {"key1": "value1",
"key2": "value2"},
"timestamp": 1405630174123,
"value": None}
for value in ('nan', 'inf', '-inf'):
metric['value'] = float(value)
self.assertRaisesRegexp(
metric_validator.InvalidValue,
value,
metric_validator.validate, metric)
def test_valid_name_chars(self):
for c in valid_name_chars:
metric = {"name": 'test{}counter'.format(c),

View File

@ -1,4 +1,4 @@
# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
# (C) Copyright 2016-2017 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.
@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import re
import ujson
@ -146,6 +147,9 @@ def validate_value(value):
msg = "invalid value type: {0} is not a number type for metric".\
format(value)
raise InvalidValue(msg)
if math.isnan(value) or math.isinf(value):
msg = "invalid value: {0} is not a valid value for metric".format(value)
raise InvalidValue(msg)
def validate_timestamp(timestamp):