Skip to content

promql: fix avg_over_time for single native histogram#18058

Merged
beorn7 merged 2 commits intoprometheus:mainfrom
zenador:fix-avg-over-time-nh
Feb 11, 2026
Merged

promql: fix avg_over_time for single native histogram#18058
beorn7 merged 2 commits intoprometheus:mainfrom
zenador:fix-avg-over-time-nh

Conversation

@zenador
Copy link
Contributor

@zenador zenador commented Feb 10, 2026

When running avg_over_time for a single native histogram, we get infinite count and sum due to a divide by 0 error, which we fix by initialising count to 1 (similar to what we do for float samples).

Commits generated with Copilot.

Which issue(s) does the PR fix:

N/A

Does this PR introduce a user-facing change?

[BUGFIX] promql: fix avg_over_time for a single native histogram

Add regression test for the bug where avg_over_time with a single
histogram sample would produce +Inf count/sum and NaN zero bucket
due to division by zero. The test verifies that both regular
exponential histograms and native histograms with custom buckets
(NHCB) correctly return the histogram unchanged when averaging
a single sample.

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
avg_over_time would produce +Inf count/sum and NaN zero bucket when
averaging a single histogram, because the count variable was initialized
to 0 instead of 1. This caused a division by zero at the end of the
function.

The float version of avg_over_time already correctly initializes count
to 1, this change makes the histogram version consistent with that.

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Thanks for the catch.

@beorn7 beorn7 merged commit b84d7dc into prometheus:main Feb 11, 2026
32 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.

3 participants