2023-11-20 15:43:55 +00:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-08-26 16:05:54 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2023-11-20 15:43:55 +00:00
|
|
|
|
|
|
|
package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-03-11 14:34:34 +00:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/internal/exemplar"
|
2023-11-20 15:43:55 +00:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
|
|
)
|
|
|
|
|
|
|
|
// datapoint is timestamped measurement data.
|
|
|
|
type datapoint[N int64 | float64] struct {
|
2024-08-26 16:05:54 +00:00
|
|
|
attrs attribute.Set
|
|
|
|
value N
|
|
|
|
res exemplar.FilteredReservoir[N]
|
2023-11-20 15:43:55 +00:00
|
|
|
}
|
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
func newLastValue[N int64 | float64](limit int, r func() exemplar.FilteredReservoir[N]) *lastValue[N] {
|
2024-03-11 14:34:34 +00:00
|
|
|
return &lastValue[N]{
|
|
|
|
newRes: r,
|
|
|
|
limit: newLimiter[datapoint[N]](limit),
|
2024-08-26 16:05:54 +00:00
|
|
|
values: make(map[attribute.Distinct]datapoint[N]),
|
|
|
|
start: now(),
|
2024-03-11 14:34:34 +00:00
|
|
|
}
|
2023-11-20 15:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lastValue summarizes a set of measurements as the last one made.
|
|
|
|
type lastValue[N int64 | float64] struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
newRes func() exemplar.FilteredReservoir[N]
|
2024-03-11 14:34:34 +00:00
|
|
|
limit limiter[datapoint[N]]
|
2024-08-26 16:05:54 +00:00
|
|
|
values map[attribute.Distinct]datapoint[N]
|
|
|
|
start time.Time
|
2023-11-20 15:43:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 14:34:34 +00:00
|
|
|
func (s *lastValue[N]) measure(ctx context.Context, value N, fltrAttr attribute.Set, droppedAttr []attribute.KeyValue) {
|
2023-11-20 15:43:55 +00:00
|
|
|
s.Lock()
|
2024-03-11 14:34:34 +00:00
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
attr := s.limit.Attributes(fltrAttr, s.values)
|
2024-08-26 16:05:54 +00:00
|
|
|
d, ok := s.values[attr.Equivalent()]
|
2024-03-11 14:34:34 +00:00
|
|
|
if !ok {
|
|
|
|
d.res = s.newRes()
|
|
|
|
}
|
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
d.attrs = attr
|
2024-03-11 14:34:34 +00:00
|
|
|
d.value = value
|
2024-08-26 16:05:54 +00:00
|
|
|
d.res.Offer(ctx, value, droppedAttr)
|
2024-03-11 14:34:34 +00:00
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
s.values[attr.Equivalent()] = d
|
2023-11-20 15:43:55 +00:00
|
|
|
}
|
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
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])
|
|
|
|
|
2023-11-20 15:43:55 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2024-08-26 16:05:54 +00:00
|
|
|
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 {
|
2023-11-20 15:43:55 +00:00
|
|
|
n := len(s.values)
|
|
|
|
*dest = reset(*dest, n, n)
|
|
|
|
|
|
|
|
var i int
|
2024-08-26 16:05:54 +00:00
|
|
|
for _, v := range s.values {
|
|
|
|
(*dest)[i].Attributes = v.attrs
|
|
|
|
(*dest)[i].StartTime = s.start
|
|
|
|
(*dest)[i].Time = t
|
2023-11-20 15:43:55 +00:00
|
|
|
(*dest)[i].Value = v.value
|
2024-08-26 16:05:54 +00:00
|
|
|
collectExemplars(&(*dest)[i].Exemplars, v.res.Collect)
|
2023-11-20 15:43:55 +00:00
|
|
|
i++
|
|
|
|
}
|
2024-08-26 16:05:54 +00:00
|
|
|
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() exemplar.FilteredReservoir[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
|
2023-11-20 15:43:55 +00:00
|
|
|
}
|