Skip to content

Commit

Permalink
fix: zero-lenght h.upperBounds
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Goncharov <[email protected]>
  • Loading branch information
imorph committed Nov 4, 2024
1 parent aaa3f32 commit e3cc4e4
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,13 +858,16 @@ func (h *histogram) Write(out *dto.Metric) error {
// findBucket returns the index of the bucket for the provided value, or
// len(h.upperBounds) for the +Inf bucket.
func (h *histogram) findBucket(v float64) int {
n := len(h.upperBounds)
if n == 0 {
return 0
}

// Early exit: if v is less than or equal to the first upper bound, return 0
if v <= h.upperBounds[0] {
return 0
}

n := len(h.upperBounds)

// Early exit: if v is greater than the last upper bound, return len(h.upperBounds)
if v > h.upperBounds[n-1] {
return n
Expand Down

0 comments on commit e3cc4e4

Please sign in to comment.