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 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()