Skip to content

Commit 4eb42ab

Browse files
committed
Add the Histogram Reservoir impl
1 parent dcfec0c commit 4eb42ab

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

sdk/metric/internal/exemplar/hist.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar"
16+
17+
import (
18+
"context"
19+
"sort"
20+
"time"
21+
22+
"go.opentelemetry.io/otel/attribute"
23+
)
24+
25+
// Histogram returns a [Reservoir] that samples the last measurement that falls
26+
// within a histogram bucket. The histogram bucket upper-boundaries are define
27+
// by bounds.
28+
//
29+
// The passed bounds will be sorted by this function.
30+
func Histogram[N int64 | float64](bounds []float64) Reservoir[N] {
31+
sort.Float64s(bounds)
32+
return &histRes[N]{
33+
bounds: bounds,
34+
storage: newStorage[N](len(bounds) + 1),
35+
}
36+
}
37+
38+
type histRes[N int64 | float64] struct {
39+
*storage[N]
40+
41+
// bounds are bucket bounds in ascending order.
42+
bounds []float64
43+
}
44+
45+
func (r *histRes[N]) Offer(ctx context.Context, t time.Time, n N, a []attribute.KeyValue) {
46+
r.store[sort.SearchFloat64s(r.bounds, float64(n))] = newMeasurement(ctx, t, n, a)
47+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exemplar
16+
17+
import "testing"
18+
19+
func TestHist(t *testing.T) {
20+
bounds := []float64{0, 100}
21+
t.Run("Int64", ReservoirTest[int64](func(int) (Reservoir[int64], int) {
22+
return Histogram[int64](bounds), len(bounds)
23+
}))
24+
25+
t.Run("Float64", ReservoirTest[float64](func(int) (Reservoir[float64], int) {
26+
return Histogram[float64](bounds), len(bounds)
27+
}))
28+
}

0 commit comments

Comments
 (0)