Render numeric label and value paths without scientific notation#455
Open
ricardbejarano wants to merge 1 commit into
Open
Conversation
JSON numbers unmarshal to float64, and k8s.io/client-go jsonpath prints them with %v, so large integers used as label or metric values render in scientific notation (e.g. "1.655371e+06" instead of "1655371"). For the non-JSON-output path, render results ourselves and format float64 values with strconv.FormatFloat(f, 'f', -1, 64). Numeric jsonpath filters are unaffected since values remain float64 during evaluation. Fixes prometheus-community#367 Signed-off-by: bejaratommy <tommy@bejara.net>
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.
Summary
Fixes #367.
A numeric JSON value used as a label (or metric value) is rendered in scientific notation, e.g.
{"id": 1655371}producesid="1.655371e+06"instead ofid="1655371".Root cause
encoding/jsonunmarshals every JSON number intofloat64, andk8s.io/client-go/util/jsonpathrenders scalar results withfmt.Fprint(..., iface)(i.e.%v), which prints largefloat64values in scientific notation.Fix
For the non-JSON-output extraction path (used for label values,
values, and scalar metric values),extractValuenow callsFindResultsand renders the results itself, formattingfloat64leaf values withstrconv.FormatFloat(f, 'f', -1, 64). All other kinds keep the previous behavior (containers viajson.Marshal, everything else viafmt.Fprint).Numeric jsonpath filters such as
[?(@.x>5)]are unaffected: values remainfloat64during evaluation, and only the final leaf-text rendering changes. The JSON-output path (ObjectScrapekey extraction) is left untouched to minimize risk.Tests
Added
TestExtractValuecovering a large integer, a float with decimals, a small integer, and a string value.Changelog
Added a
[BUGFIX]entry referencing #367.