From 9e818dbda1627510e9afbadb2eb4596a76cf3bef Mon Sep 17 00:00:00 2001 From: Victor Grigoriu Date: Thu, 3 Jan 2019 17:24:36 +0200 Subject: [PATCH 1/4] Make the format strings compatible with Python 2.6 Signed-off-by: Victor Grigoriu --- prometheus_client/samples.py | 2 +- prometheus_client/utils.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/prometheus_client/samples.py b/prometheus_client/samples.py index 61f63f0a..86ac2270 100644 --- a/prometheus_client/samples.py +++ b/prometheus_client/samples.py @@ -6,7 +6,7 @@ class Timestamp(object): def __init__(self, sec, nsec): if nsec < 0 or nsec >= 1e9: - raise ValueError("Invalid value for nanoseconds in Timestamp: {}".format(nsec)) + raise ValueError("Invalid value for nanoseconds in Timestamp: {0}".format(nsec)) if sec < 0: nsec = -nsec self.sec = int(sec) diff --git a/prometheus_client/utils.py b/prometheus_client/utils.py index 4f0c5108..bd894a1b 100644 --- a/prometheus_client/utils.py +++ b/prometheus_client/utils.py @@ -18,6 +18,6 @@ def floatToGoString(d): # Go switches to exponents sooner than Python. # We only need to care about positive values for le/quantile. if d > 0 and dot > 6: - mantissa = '{}.{}{}'.format(s[0], s[1:dot], s[dot+1:]).rstrip('0.') - return '{}e+0{}'.format(mantissa, dot-1) + mantissa = '{0}.{1}{2}'.format(s[0], s[1:dot], s[dot+1:]).rstrip('0.') + return '{0}e+0{1}'.format(mantissa, dot-1) return s From 896ef8fcb2a48b34a8d154596ae2fa260856754f Mon Sep 17 00:00:00 2001 From: Victor Grigoriu Date: Fri, 4 Jan 2019 13:46:12 +0200 Subject: [PATCH 2/4] Delete test that fails on Python 2.6 Signed-off-by: Victor Grigoriu --- tests/openmetrics/test_parser.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/openmetrics/test_parser.py b/tests/openmetrics/test_parser.py index e53bff19..3c604396 100644 --- a/tests/openmetrics/test_parser.py +++ b/tests/openmetrics/test_parser.py @@ -568,7 +568,6 @@ def test_invalid_input(self): ('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 1\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="9.999999999999999e+22"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), - ('# TYPE a histogram\na_bucket{le="1.5555555555555201e+06"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1e-04"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1e+05"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="+INF"} 0\n# EOF\n'), From a4ec6b3d4d1bebfa23b0df4390b711c3c6e0394d Mon Sep 17 00:00:00 2001 From: Victor Grigoriu Date: Fri, 4 Jan 2019 14:13:22 +0200 Subject: [PATCH 3/4] Limit # of digits in floatToGoString Signed-off-by: Victor Grigoriu --- prometheus_client/utils.py | 2 +- tests/openmetrics/test_parser.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/prometheus_client/utils.py b/prometheus_client/utils.py index bd894a1b..7d731b28 100644 --- a/prometheus_client/utils.py +++ b/prometheus_client/utils.py @@ -18,6 +18,6 @@ def floatToGoString(d): # Go switches to exponents sooner than Python. # We only need to care about positive values for le/quantile. if d > 0 and dot > 6: - mantissa = '{0}.{1}{2}'.format(s[0], s[1:dot], s[dot+1:]).rstrip('0.') + mantissa = '{0}.{1}{2}'.format(s[0], s[1:dot], s[dot+1:16]).rstrip('0.') return '{0}e+0{1}'.format(mantissa, dot-1) return s diff --git a/tests/openmetrics/test_parser.py b/tests/openmetrics/test_parser.py index 3c604396..e53bff19 100644 --- a/tests/openmetrics/test_parser.py +++ b/tests/openmetrics/test_parser.py @@ -568,6 +568,7 @@ def test_invalid_input(self): ('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 1\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="9.999999999999999e+22"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), + ('# TYPE a histogram\na_bucket{le="1.5555555555555201e+06"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1e-04"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1e+05"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="+INF"} 0\n# EOF\n'), From e9093e8a5ac0eabad686f6e28124ce51fcc203f3 Mon Sep 17 00:00:00 2001 From: Victor Grigoriu Date: Fri, 4 Jan 2019 16:44:41 +0200 Subject: [PATCH 4/4] skip failing test on 2.6 Signed-off-by: Victor Grigoriu --- prometheus_client/utils.py | 2 +- tests/openmetrics/test_parser.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/prometheus_client/utils.py b/prometheus_client/utils.py index 7d731b28..bd894a1b 100644 --- a/prometheus_client/utils.py +++ b/prometheus_client/utils.py @@ -18,6 +18,6 @@ def floatToGoString(d): # Go switches to exponents sooner than Python. # We only need to care about positive values for le/quantile. if d > 0 and dot > 6: - mantissa = '{0}.{1}{2}'.format(s[0], s[1:dot], s[dot+1:16]).rstrip('0.') + mantissa = '{0}.{1}{2}'.format(s[0], s[1:dot], s[dot+1:]).rstrip('0.') return '{0}e+0{1}'.format(mantissa, dot-1) return s diff --git a/tests/openmetrics/test_parser.py b/tests/openmetrics/test_parser.py index e53bff19..251077e3 100644 --- a/tests/openmetrics/test_parser.py +++ b/tests/openmetrics/test_parser.py @@ -567,8 +567,6 @@ def test_invalid_input(self): ('# TYPE a histogram\na_count 1\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="+Inf"} 0\na_count 1\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), - ('# TYPE a histogram\na_bucket{le="9.999999999999999e+22"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), - ('# TYPE a histogram\na_bucket{le="1.5555555555555201e+06"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1e-04"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="1e+05"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), ('# TYPE a histogram\na_bucket{le="+INF"} 0\n# EOF\n'), @@ -590,6 +588,16 @@ def test_invalid_input(self): with self.assertRaises(ValueError): list(text_string_to_metric_families(case)) + @unittest.skipIf(sys.version_info < (2, 7), "float repr changed from 2.6 to 2.7") + def test_invalid_float_input(self): + for case in [ + # Bad histograms. + ('# TYPE a histogram\na_bucket{le="9.999999999999999e+22"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), + ('# TYPE a histogram\na_bucket{le="1.5555555555555201e+06"} 0\na_bucket{le="+Inf"} 0\n# EOF\n'), + ]: + with self.assertRaises(ValueError): + list(text_string_to_metric_families(case)) + if __name__ == '__main__': unittest.main()