Skip to content

fix: avoid int64 overflow in milli-quantity conversion for large metric values#7534

Merged
JorTurFer merged 3 commits into
kedacore:mainfrom
MunemHashmi:fix/metrics-api-large-value-overflow
Mar 12, 2026
Merged

fix: avoid int64 overflow in milli-quantity conversion for large metric values#7534
JorTurFer merged 3 commits into
kedacore:mainfrom
MunemHashmi:fix/metrics-api-large-value-overflow

Conversation

@MunemHashmi

@MunemHashmi MunemHashmi commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Fix int64 overflow in GetMetricTargetMili() and GenerateMetricInMili() that caused very large metric values (>~9.2×10¹⁵) to be reported as zero.

Root Cause

Both functions computed int64(value * 1000) to create milli-quantities. When value * 1000 exceeds math.MaxInt64 (~9.22×10¹⁸), the conversion silently overflows, producing a negative number that gets reported as zero by the HPA.

Fix

Extract a shared quantityFromFloat64() helper that:

  1. Guards against NaN and Inf inputs (treats them as zero to prevent resource.MustParse panics)
  2. Uses resource.MustParse(fmt.Sprintf("%.3f", value)) instead of int64(value * 1000) + resource.NewMilliQuantity(), leveraging resource.Quantity's arbitrary precision to avoid int64 overflow

Checklist

Fixes #7441

…ic values

Values exceeding ~9.2e15 caused int64 overflow when multiplied by 1000
in GetMetricTargetMili() and GenerateMetricInMili(), resulting in metrics
being reported as zero. Use resource.MustParse with string formatting
instead to handle arbitrarily large float64 values.

Signed-off-by: Munem Hashmi <[email protected]>
@MunemHashmi MunemHashmi requested a review from a team as a code owner March 10, 2026 02:46
@github-actions

Copy link
Copy Markdown

Thank you for your contribution! 🙏

Please understand that we will do our best to review your PR and give you feedback as soon as possible, but please bear with us if it takes a little longer as expected.

While you are waiting, make sure to:

  • Add an entry in our changelog in alphabetical order and link related issue
  • Update the documentation, if needed
  • Add unit & e2e tests for your changes
  • GitHub checks are passing
  • Is the DCO check failing? Here is how you can fix DCO issues

Once the initial tests are successful, a KEDA member will ensure that the e2e tests are run. Once the e2e tests have been successfully completed, the PR may be merged at a later date. Please be patient.

Learn more about our contribution guide.

@keda-automation keda-automation requested a review from a team March 10, 2026 02:47
@snyk-io

snyk-io Bot commented Mar 10, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@rickbrouwer

Copy link
Copy Markdown
Member

What will happen with this new code when we get a NaN or Inf as result like #7475?

…version

MustParse panics on "NaN" or "+Inf" strings. Add a guard in
quantityFromFloat64 that treats NaN/Inf as zero, preventing operator
crashes from malformed metric API responses.

Signed-off-by: Munem Hashmi <[email protected]>
@MunemHashmi

Copy link
Copy Markdown
Contributor Author

Good catch @rickbrouwer! The previous code would have had undefined behavior for NaN/Inf (silent int64 cast), but with resource.MustParse it would panic on those inputs.

I've pushed a follow-up commit that extracts a quantityFromFloat64 helper which guards against NaN and Inf by treating them as zero. This prevents operator crashes from malformed metric API responses. Added test coverage for both cases as well.

@rickbrouwer

rickbrouwer commented Mar 11, 2026

Copy link
Copy Markdown
Member

/run-e2e
Update: You can check the progress here

@rickbrouwer

rickbrouwer commented Mar 11, 2026

Copy link
Copy Markdown
Member

/run-e2e
Update: You can check the progress here

@rickbrouwer

rickbrouwer commented Mar 11, 2026

Copy link
Copy Markdown
Member

/run-e2e github_runner*
Update: You can check the progress here

@JorTurFer JorTurFer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch!

Comment thread pkg/scalers/scaler_test.go Outdated
…Type variation

The metric type only affects which struct field receives the quantity,
not the conversion logic. Use a single metric type to keep the test
focused on value conversion.

Signed-off-by: Munem Hashmi <[email protected]>
@keda-automation keda-automation requested a review from a team March 11, 2026 23:15
@rickbrouwer

rickbrouwer commented Mar 12, 2026

Copy link
Copy Markdown
Member

/run-e2e internal
Update: You can check the progress here

@rickbrouwer rickbrouwer enabled auto-merge (squash) March 12, 2026 07:20
@JorTurFer JorTurFer disabled auto-merge March 12, 2026 07:55
@JorTurFer JorTurFer enabled auto-merge (squash) March 12, 2026 07:55
@JorTurFer JorTurFer merged commit edf694c into kedacore:main Mar 12, 2026
25 checks passed
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.

Averaged Value for Keda Metrics-API is Zero even though It's Large Value

3 participants