diff --git a/prometheus_client/utils.py b/prometheus_client/utils.py index 87b75ca8..52c852ab 100644 --- a/prometheus_client/utils.py +++ b/prometheus_client/utils.py @@ -21,7 +21,7 @@ def floatToGoString(d): # We only need to care about positive values for le/quantile. if d > 0 and dot > 6: mantissa = f'{s[0]}.{s[1:dot]}{s[dot + 1:]}'.rstrip('0.') - return f'{mantissa}e+0{dot - 1}' + return f'{mantissa}e+{dot - 1:02d}' return s diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..50eac33c --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,18 @@ +import unittest + +from prometheus_client.utils import floatToGoString + + +class TestFloatToGoString(unittest.TestCase): + def test_exponent_two_digits_has_no_leading_zero(self): + # floatToGoString mirrors Go's strconv.FormatFloat(f, 'g', -1, 64), + # which pads the exponent to a minimum of two digits. A two-digit + # exponent must not gain a spurious leading zero. + self.assertEqual('1e+10', floatToGoString(1e10)) + self.assertEqual('1e+15', floatToGoString(1e15)) + self.assertEqual('1.234567890123e+12', floatToGoString(1234567890123.0)) + + def test_exponent_one_digit_is_zero_padded(self): + # Single-digit exponents keep the two-digit zero padding. + self.assertEqual('1e+06', floatToGoString(1e6)) + self.assertEqual('1.234567e+06', floatToGoString(1234567.0))