|
| 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 | +} |
0 commit comments