| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package exemplar // import "go.opentelemetry.io/otel/sdk/metric/exemplar" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"slices" |
| 9 |
"sort" |
| 10 |
"time" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
"go.opentelemetry.io/otel/sdk/metric/internal/reservoir" |
| 14 |
) |
| 15 |
|
| 16 |
// HistogramReservoirProvider is a provider of [HistogramReservoir]. |
| 17 |
func HistogramReservoirProvider(bounds []float64) ReservoirProvider { |
| 18 |
cp := slices.Clone(bounds) |
| 19 |
slices.Sort(cp) |
| 20 |
return func(attribute.Set) Reservoir { |
| 21 |
return NewHistogramReservoir(cp) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
// NewHistogramReservoir returns a [HistogramReservoir] that samples the last |
| 26 |
// measurement that falls within a histogram bucket. The histogram bucket |
| 27 |
// upper-boundaries are define by bounds. |
| 28 |
// |
| 29 |
// The passed bounds must be sorted before calling this function. |
| 30 |
func NewHistogramReservoir(bounds []float64) *HistogramReservoir { |
| 31 |
return &HistogramReservoir{ |
| 32 |
bounds: bounds, |
| 33 |
storage: newStorage(len(bounds) + 1), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
var _ Reservoir = &HistogramReservoir{} |
| 38 |
|
| 39 |
// HistogramReservoir is a [Reservoir] that samples the last measurement that |
| 40 |
// falls within a histogram bucket. The histogram bucket upper-boundaries are |
| 41 |
// define by bounds. |
| 42 |
type HistogramReservoir struct { |
| 43 |
reservoir.ConcurrentSafe |
| 44 |
*storage |
| 45 |
|
| 46 |
// bounds are bucket bounds in ascending order. |
| 47 |
bounds []float64 |
| 48 |
} |
| 49 |
|
| 50 |
// Offer accepts the parameters associated with a measurement. The |
| 51 |
// parameters will be stored as an exemplar if the Reservoir decides to |
| 52 |
// sample the measurement. |
| 53 |
// |
| 54 |
// The passed ctx needs to contain any baggage or span that were active |
| 55 |
// when the measurement was made. This information may be used by the |
| 56 |
// Reservoir in making a sampling decision. |
| 57 |
// |
| 58 |
// The time t is the time when the measurement was made. The v and a |
| 59 |
// parameters are the value and dropped (filtered) attributes of the |
| 60 |
// measurement respectively. |
| 61 |
func (r *HistogramReservoir) Offer(ctx context.Context, t time.Time, v Value, a []attribute.KeyValue) { |
| 62 |
var n float64 |
| 63 |
switch v.Type() { |
| 64 |
case Int64ValueType: |
| 65 |
n = float64(v.Int64()) |
| 66 |
case Float64ValueType: |
| 67 |
n = v.Float64() |
| 68 |
default: |
| 69 |
panic("unknown value type") |
| 70 |
} |
| 71 |
|
| 72 |
idx := sort.SearchFloat64s(r.bounds, n) |
| 73 |
|
| 74 |
r.store(ctx, idx, t, v, a) |
| 75 |
} |
| 76 |
|
| 77 |
// Collect returns all the held exemplars. |
| 78 |
// |
| 79 |
// The Reservoir state is preserved after this call. |
| 80 |
func (r *HistogramReservoir) Collect(dest *[]Exemplar) { |
| 81 |
r.storage.Collect(dest) |
| 82 |
} |