Skip to content

Render numeric label and value paths without scientific notation#455

Open
ricardbejarano wants to merge 1 commit into
prometheus-community:masterfrom
bejaratommy:fix-numeric-label-scientific-notation
Open

Render numeric label and value paths without scientific notation#455
ricardbejarano wants to merge 1 commit into
prometheus-community:masterfrom
bejaratommy:fix-numeric-label-scientific-notation

Conversation

@ricardbejarano

Copy link
Copy Markdown

Summary

Fixes #367.

A numeric JSON value used as a label (or metric value) is rendered in scientific notation, e.g. {"id": 1655371} produces id="1.655371e+06" instead of id="1655371".

Root cause

encoding/json unmarshals every JSON number into float64, and k8s.io/client-go/util/jsonpath renders scalar results with fmt.Fprint(..., iface) (i.e. %v), which prints large float64 values in scientific notation.

Fix

For the non-JSON-output extraction path (used for label values, values, and scalar metric values), extractValue now calls FindResults and renders the results itself, formatting float64 leaf values with strconv.FormatFloat(f, 'f', -1, 64). All other kinds keep the previous behavior (containers via json.Marshal, everything else via fmt.Fprint).

Numeric jsonpath filters such as [?(@.x>5)] are unaffected: values remain float64 during evaluation, and only the final leaf-text rendering changes. The JSON-output path (ObjectScrape key extraction) is left untouched to minimize risk.

Tests

Added TestExtractValue covering a large integer, a float with decimals, a small integer, and a string value.

Changelog

Added a [BUGFIX] entry referencing #367.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Force a label value not to be in scientific notation

2 participants