runtime/metrics: fix panic in readingAllMetric example

medianBucket can return if the total is greater than thresh.
However, if a histogram has no counts, total and thresh
will both be zero and cause panic.

Adding an equal sign to prevent the potential panic.

Fixes #44148

Change-Id: Ifb8a781990f490d142ae7c035b4e01d6a07ae04d
Reviewed-on: https://go-review.googlesource.com/c/go/+/290171
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Changkun Ou 2021-02-07 17:31:12 +01:00 committed by Michael Knyszek
parent ed3e4afa12
commit 1901853098

View File

@ -88,7 +88,7 @@ func medianBucket(h *metrics.Float64Histogram) float64 {
total = 0
for i, count := range h.Counts {
total += count
if total > thresh {
if total >= thresh {
return h.Buckets[i]
}
}