Skip to content

prometheus: Fix formatBytes invalid output#854

Open
skools-here wants to merge 1 commit into
headlamp-k8s:mainfrom
skools-here:fix-formatbytes-invalid-output-clean
Open

prometheus: Fix formatBytes invalid output#854
skools-here wants to merge 1 commit into
headlamp-k8s:mainfrom
skools-here:fix-formatbytes-invalid-output-clean

Conversation

@skools-here

Copy link
Copy Markdown

Summary

This PR addresses an issue where the formatBytes utility function produces invalid output strings (such as NaNundefined) when it receives negative, NaN, or non-finite inputs.

Fixes: #853

Problem

In prometheus/src/util.ts, the formatBytes function calculates the logarithm of the input to determine the formatting scale (KB, MB, etc.). However, when the input is:

  • Negative (e.g., -1): Math.log(-1) returns NaN, leading to NaNundefined UI outputs. This case frequently arises briefly during counter resets or delta calculations in Prometheus metrics.
  • NaN / Infinity: The scaling calculation evaluates to NaN or out-of-bounds indices, causing the UI to render NaNundefined.

Solution

  • Input Validation: Added validation in formatBytes to return a graceful fallback of "0.00B" if the input is negative (bytes < 0) or not finite (!Number. isFinite(bytes)). This aligns with the formatting of 0 bytes.
  • Unit Tests: Added test coverage in util.test.ts to verify the handling.

Signed-off-by: skools-here <sajalkulshreshtha9@gmail.com>
@skools-here skools-here force-pushed the fix-formatbytes-invalid-output-clean branch from 068a958 to 41f8d7a Compare June 25, 2026 09:51
@skools-here

Copy link
Copy Markdown
Author

ptal @illume

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.

formatBytes produces invalid UI output (e.g. NaNundefined) on negative, NaN, or non-finite inputs

1 participant