mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-26 03:52:03 +02:00
81b2a33e1b
Resolve https://github.com/open-telemetry/opentelemetry-go/issues/5249 ### Spec > exemplar_reservoir: A functional type that generates an exemplar reservoir a MeterProvider will use when storing exemplars. This functional type needs to be a factory or callback similar to aggregation selection functionality which allows different reservoirs to be chosen by the aggregation. > Users can provide an exemplar_reservoir, but it is up to their discretion. Therefore, the stream configuration parameter needs to be structured to accept an exemplar_reservoir, but MUST NOT obligate a user to provide one. If the user does not provide an exemplar_reservoir value, the MeterProvider MUST apply a [default exemplar reservoir](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#exemplar-defaults). Also, > the reservoir MUST be given the Attributes associated with its timeseries point either at construction so that additional sampling performed by the reservoir has access to all attributes from a measurement in the "offer" method. ### Changes In sdk/metric/exemplar, add: * `exemplar.ReservoirProvider` * `exemplar.HistogramReservoirProvider` * `exemplar.FixedSizeReservoirProvider` In sdk/metric, add: * `metric.ExemplarReservoirProviderSelector` (func Aggregation -> ReservoirProvider) * `metric.DefaultExemplarReservoirProviderSelector` (our default implementation) * `ExemplarReservoirProviderSelector` added to `metric.Stream` Note: the only required public types are `metric.ExemplarReservoirProviderSelector` and `ExemplarReservoirProviderSelector` in `metric.Stream`. Others are for convenience and readability. ### Alternatives considered #### Add ExemplarReservoirProvider directly to metric.Stream, instead of ExemplarReservoirProviderSelector This would mean users can configure a `func() exemplar.Reservoir` instead of a `func(Aggregation) func() exemplar.Reservoir`. I don't think this complies with the statement: `This functional type needs to be a factory or callback similar to aggregation selection functionality which allows different reservoirs to be chosen by the aggregation.`. I'm interpreting "allows different reservoirs to be chosen by the aggregation" as meaning "allows different reservoirs to be chosen **based on the** aggregation", rather than meaning that the aggregation is somehow choosing the reservoir. ### Future work There is some refactoring I plan to do after this to simplify the interaction between the internal/aggregate and exemplar package. I've omitted that from this PR to keep the diff smaller. --------- Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Robert Pająk <pellared@hotmail.com>
162 lines
4.2 KiB
Go
162 lines
4.2 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
)
|
|
|
|
// datapoint is timestamped measurement data.
|
|
type datapoint[N int64 | float64] struct {
|
|
attrs attribute.Set
|
|
value N
|
|
res FilteredExemplarReservoir[N]
|
|
}
|
|
|
|
func newLastValue[N int64 | float64](limit int, r func(attribute.Set) FilteredExemplarReservoir[N]) *lastValue[N] {
|
|
return &lastValue[N]{
|
|
newRes: r,
|
|
limit: newLimiter[datapoint[N]](limit),
|
|
values: make(map[attribute.Distinct]datapoint[N]),
|
|
start: now(),
|
|
}
|
|
}
|
|
|
|
// lastValue summarizes a set of measurements as the last one made.
|
|
type lastValue[N int64 | float64] struct {
|
|
sync.Mutex
|
|
|
|
newRes func(attribute.Set) FilteredExemplarReservoir[N]
|
|
limit limiter[datapoint[N]]
|
|
values map[attribute.Distinct]datapoint[N]
|
|
start time.Time
|
|
}
|
|
|
|
func (s *lastValue[N]) measure(ctx context.Context, value N, fltrAttr attribute.Set, droppedAttr []attribute.KeyValue) {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
attr := s.limit.Attributes(fltrAttr, s.values)
|
|
d, ok := s.values[attr.Equivalent()]
|
|
if !ok {
|
|
d.res = s.newRes(attr)
|
|
}
|
|
|
|
d.attrs = attr
|
|
d.value = value
|
|
d.res.Offer(ctx, value, droppedAttr)
|
|
|
|
s.values[attr.Equivalent()] = d
|
|
}
|
|
|
|
func (s *lastValue[N]) delta(dest *metricdata.Aggregation) int {
|
|
t := now()
|
|
// Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of
|
|
// the DataPoints is missed (better luck next time).
|
|
gData, _ := (*dest).(metricdata.Gauge[N])
|
|
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
n := s.copyDpts(&gData.DataPoints, t)
|
|
// Do not report stale values.
|
|
clear(s.values)
|
|
// Update start time for delta temporality.
|
|
s.start = t
|
|
|
|
*dest = gData
|
|
|
|
return n
|
|
}
|
|
|
|
func (s *lastValue[N]) cumulative(dest *metricdata.Aggregation) int {
|
|
t := now()
|
|
// Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of
|
|
// the DataPoints is missed (better luck next time).
|
|
gData, _ := (*dest).(metricdata.Gauge[N])
|
|
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
n := s.copyDpts(&gData.DataPoints, t)
|
|
// TODO (#3006): This will use an unbounded amount of memory if there
|
|
// are unbounded number of attribute sets being aggregated. Attribute
|
|
// sets that become "stale" need to be forgotten so this will not
|
|
// overload the system.
|
|
*dest = gData
|
|
|
|
return n
|
|
}
|
|
|
|
// copyDpts copies the datapoints held by s into dest. The number of datapoints
|
|
// copied is returned.
|
|
func (s *lastValue[N]) copyDpts(dest *[]metricdata.DataPoint[N], t time.Time) int {
|
|
n := len(s.values)
|
|
*dest = reset(*dest, n, n)
|
|
|
|
var i int
|
|
for _, v := range s.values {
|
|
(*dest)[i].Attributes = v.attrs
|
|
(*dest)[i].StartTime = s.start
|
|
(*dest)[i].Time = t
|
|
(*dest)[i].Value = v.value
|
|
collectExemplars(&(*dest)[i].Exemplars, v.res.Collect)
|
|
i++
|
|
}
|
|
return n
|
|
}
|
|
|
|
// newPrecomputedLastValue returns an aggregator that summarizes a set of
|
|
// observations as the last one made.
|
|
func newPrecomputedLastValue[N int64 | float64](limit int, r func(attribute.Set) FilteredExemplarReservoir[N]) *precomputedLastValue[N] {
|
|
return &precomputedLastValue[N]{lastValue: newLastValue[N](limit, r)}
|
|
}
|
|
|
|
// precomputedLastValue summarizes a set of observations as the last one made.
|
|
type precomputedLastValue[N int64 | float64] struct {
|
|
*lastValue[N]
|
|
}
|
|
|
|
func (s *precomputedLastValue[N]) delta(dest *metricdata.Aggregation) int {
|
|
t := now()
|
|
// Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of
|
|
// the DataPoints is missed (better luck next time).
|
|
gData, _ := (*dest).(metricdata.Gauge[N])
|
|
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
n := s.copyDpts(&gData.DataPoints, t)
|
|
// Do not report stale values.
|
|
clear(s.values)
|
|
// Update start time for delta temporality.
|
|
s.start = t
|
|
|
|
*dest = gData
|
|
|
|
return n
|
|
}
|
|
|
|
func (s *precomputedLastValue[N]) cumulative(dest *metricdata.Aggregation) int {
|
|
t := now()
|
|
// Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of
|
|
// the DataPoints is missed (better luck next time).
|
|
gData, _ := (*dest).(metricdata.Gauge[N])
|
|
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
n := s.copyDpts(&gData.DataPoints, t)
|
|
// Do not report stale values.
|
|
clear(s.values)
|
|
*dest = gData
|
|
|
|
return n
|
|
}
|