2024-03-11 14:34:34 +00:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-08-26 16:05:54 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2024-03-11 14:34:34 +00:00
|
|
|
|
|
|
|
package exemplar // import "go.opentelemetry.io/otel/sdk/metric/internal/exemplar"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-26 16:05:54 +00:00
|
|
|
"slices"
|
2024-03-11 14:34:34 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Histogram returns a [Reservoir] that samples the last measurement that falls
|
|
|
|
// within a histogram bucket. The histogram bucket upper-boundaries are define
|
|
|
|
// by bounds.
|
|
|
|
//
|
|
|
|
// The passed bounds will be sorted by this function.
|
2024-08-26 16:05:54 +00:00
|
|
|
func Histogram(bounds []float64) Reservoir {
|
|
|
|
slices.Sort(bounds)
|
|
|
|
return &histRes{
|
2024-03-11 14:34:34 +00:00
|
|
|
bounds: bounds,
|
2024-08-26 16:05:54 +00:00
|
|
|
storage: newStorage(len(bounds) + 1),
|
2024-03-11 14:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
type histRes struct {
|
|
|
|
*storage
|
2024-03-11 14:34:34 +00:00
|
|
|
|
|
|
|
// bounds are bucket bounds in ascending order.
|
|
|
|
bounds []float64
|
|
|
|
}
|
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
func (r *histRes) Offer(ctx context.Context, t time.Time, v Value, a []attribute.KeyValue) {
|
|
|
|
var x float64
|
|
|
|
switch v.Type() {
|
|
|
|
case Int64ValueType:
|
|
|
|
x = float64(v.Int64())
|
|
|
|
case Float64ValueType:
|
|
|
|
x = v.Float64()
|
|
|
|
default:
|
|
|
|
panic("unknown value type")
|
|
|
|
}
|
|
|
|
r.store[sort.SearchFloat64s(r.bounds, x)] = newMeasurement(ctx, t, v, a)
|
2024-03-11 14:34:34 +00:00
|
|
|
}
|