Skip to content

Commit

Permalink
Metrics Summary: fix panic edge case (#2738)
Browse files Browse the repository at this point in the history
* fix panic edge case

* changelog
  • Loading branch information
mdisibio authored Aug 1, 2023
1 parent d21e0bb commit 729a26f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [FEATURE] New encoding vParquet3 with support for dedicated attribute columns (@mapno, @stoewer) [#2649](https://github.com/grafana/tempo/pull/2649)
* [ENHANCEMENT] Assert ingestion rate limits as early as possible [#2640](https://github.com/grafana/tempo/pull/2703) (@mghildiy)
* [ENHANCEMENT] Add several metrics-generator fields to user-configurable overrides [#2711](https://github.com/grafana/tempo/pull/2711) (@kvrhdn)
* [BUGFIX] Fix panic in metrics summary api [#2738](https://github.com/grafana/tempo/pull/2738) (@mdisibio)

## v2.2.0 / 2023-07-31

Expand Down
10 changes: 8 additions & 2 deletions pkg/traceqlmetrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"github.com/pkg/errors"
)

const maxBuckets = 64

type LatencyHistogram struct {
buckets [64]int // Exponential buckets, powers of 2
buckets [maxBuckets]int // Exponential buckets, powers of 2
}

func New(buckets [64]int) *LatencyHistogram {
func New(buckets [maxBuckets]int) *LatencyHistogram {
return &LatencyHistogram{buckets: buckets}
}

Expand All @@ -26,6 +28,10 @@ func (m *LatencyHistogram) Record(durationNanos uint64) {
if durationNanos >= 2 {
bucket = int(math.Ceil(math.Log2(float64(durationNanos))))
}
if bucket >= maxBuckets {
bucket = maxBuckets - 1
}

m.buckets[bucket]++
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/traceqlmetrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package traceqlmetrics

import (
"context"
"math"
"testing"

"github.com/grafana/tempo/pkg/traceql"
Expand Down Expand Up @@ -39,6 +40,12 @@ func TestPercentile(t *testing.T) {
p: 1.0,
value: uint64(1),
},
{
name: "edge case max bucket",
durations: []uint64{math.MaxUint64},
p: 1.0,
value: uint64(1 << (maxBuckets - 1)),
},
{
name: "edge case empty",
durations: []uint64{},
Expand Down

0 comments on commit 729a26f

Please # to comment.