Do not emit a leading zero in floatToGoString exponents >= 10#1190
Open
sean-kim05 wants to merge 1 commit into
Open
Do not emit a leading zero in floatToGoString exponents >= 10#1190sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
floatToGoString reproduces Go's strconv.FormatFloat(f, 'g', -1, 64),
which pads a float's exponent to a minimum of two digits. The exponent
was formatted as 'e+0{dot - 1}', which hard-codes a single leading zero
and only produces the right width while dot - 1 is a single digit. Once
the exponent reaches two digits (values >= 1e10, whose repr is still
plain decimal) it over-pads, e.g. 1e10 became '1e+010' instead of Go's
'1e+10'. That affects any emitted sample value or le/quantile bucket
boundary at or above 1e10.
Use '{dot - 1:02d}' so the exponent is zero-padded to a minimum of two
digits and not beyond, matching Go.
Add tests/test_utils.py covering both the previously-correct single-digit
exponents and the two-digit exponents that regressed.
Signed-off-by: Sean Kim <skim8705@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
floatToGoStringis meant to mirror Go'sstrconv.FormatFloat(f, 'g', -1, 64), which pads the exponent to a minimum of two digits (1e+06,1e+10,1e+15). The current formatting hard-codes a single0in front of the exponent:That is only correct while
dot - 1 < 10. As soon as the exponent needs two digits, the literal0becomes a spurious third digit:1e61e+061e+061e101e+101e+0101e151e+151e+0151234567890123.01.234567890123e+121.234567890123e+012So any histogram/summary whose
le/quantile boundary is at or above1e10serializes a bucket label that doesn't match the value Go/Prometheus would emit for the same float.Fix: zero-pad the exponent to two digits rather than prepending a literal
0:This keeps the existing two-digit padding for small exponents (
1e+06) and drops the extra zero for large ones (1e+10).Added
tests/test_utils.pycovering both the single-digit (still zero-padded) and two-digit (no leading zero) cases.