promtool: Fix panic on extended tsdb analyze (#13976)

Currently, running promtool tsdb analyze with the --extended flag
will cause an 'index out of range' error if running it
against a block that does not have any native histogram chunks.

This change ensures that promtool won't try to display data that doesn't exist.

Signed-off-by: Will Hegedus <whegedus@linode.com>
This commit is contained in:
Will Hegedus 2024-04-23 21:35:34 -04:00 committed by GitHub
parent 76b0318ed5
commit bd1878700b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -838,6 +838,10 @@ func backfillOpenMetrics(path, outputDir string, humanReadable, quiet bool, maxB
} }
func displayHistogram(dataType string, datas []int, total int) { func displayHistogram(dataType string, datas []int, total int) {
if len(datas) == 0 {
fmt.Printf("%s: N/A\n\n", dataType)
return
}
slices.Sort(datas) slices.Sort(datas)
start, end, step := generateBucket(datas[0], datas[len(datas)-1]) start, end, step := generateBucket(datas[0], datas[len(datas)-1])
sum := 0 sum := 0