| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"sync" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/sdk/metric/exemplar" |
| 13 |
"go.opentelemetry.io/otel/sdk/metric/internal/reservoir" |
| 14 |
) |
| 15 |
|
| 16 |
// FilteredExemplarReservoir wraps a [exemplar.Reservoir] with a filter. |
| 17 |
type FilteredExemplarReservoir[N int64 | float64] interface { |
| 18 |
// Offer accepts the parameters associated with a measurement. The |
| 19 |
// parameters will be stored as an exemplar if the filter decides to |
| 20 |
// sample the measurement. |
| 21 |
// |
| 22 |
// The passed ctx needs to contain any baggage or span that were active |
| 23 |
// when the measurement was made. This information may be used by the |
| 24 |
// Reservoir in making a sampling decision. |
| 25 |
Offer(ctx context.Context, val N, attr []attribute.KeyValue) |
| 26 |
// Collect returns all the held exemplars in the reservoir. |
| 27 |
Collect(dest *[]exemplar.Exemplar) |
| 28 |
} |
| 29 |
|
| 30 |
// filteredExemplarReservoir handles the pre-sampled exemplar of measurements made. |
| 31 |
type filteredExemplarReservoir[N int64 | float64] struct { |
| 32 |
filter exemplar.Filter |
| 33 |
reservoir exemplar.Reservoir |
| 34 |
// The exemplar.Reservoir is not required to be concurrent safe, but |
| 35 |
// implementations can indicate that they are concurrent-safe by embedding |
| 36 |
// reservoir.ConcurrentSafe in order to improve performance. |
| 37 |
reservoirMux sync.Mutex |
| 38 |
concurrentSafe bool |
| 39 |
} |
| 40 |
|
| 41 |
// NewFilteredExemplarReservoir creates a [FilteredExemplarReservoir] which only offers values |
| 42 |
// that are allowed by the filter. |
| 43 |
func NewFilteredExemplarReservoir[N int64 | float64]( |
| 44 |
f exemplar.Filter, |
| 45 |
r exemplar.Reservoir, |
| 46 |
) FilteredExemplarReservoir[N] { |
| 47 |
_, concurrentSafe := r.(reservoir.ConcurrentSafe) |
| 48 |
return &filteredExemplarReservoir[N]{ |
| 49 |
filter: f, |
| 50 |
reservoir: r, |
| 51 |
concurrentSafe: concurrentSafe, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func (f *filteredExemplarReservoir[N]) Offer(ctx context.Context, val N, attr []attribute.KeyValue) { |
| 56 |
if f.filter(ctx) { |
| 57 |
// only record the current time if we are sampling this measurement. |
| 58 |
ts := time.Now() |
| 59 |
if !f.concurrentSafe { |
| 60 |
f.reservoirMux.Lock() |
| 61 |
defer f.reservoirMux.Unlock() |
| 62 |
} |
| 63 |
f.reservoir.Offer(ctx, ts, exemplar.NewValue(val), attr) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func (f *filteredExemplarReservoir[N]) Collect(dest *[]exemplar.Exemplar) { |
| 68 |
if !f.concurrentSafe { |
| 69 |
f.reservoirMux.Lock() |
| 70 |
defer f.reservoirMux.Unlock() |
| 71 |
} |
| 72 |
f.reservoir.Collect(dest) |
| 73 |
} |