| 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 |
"time" |
| 9 |
|
| 10 |
"go.opentelemetry.io/otel/attribute" |
| 11 |
"go.opentelemetry.io/otel/sdk/internal/x" |
| 12 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 13 |
) |
| 14 |
|
| 15 |
// lastValuePoint is timestamped measurement data. |
| 16 |
type lastValuePoint[N int64 | float64] struct { |
| 17 |
attrs attribute.Set |
| 18 |
value atomicN[N] |
| 19 |
res FilteredExemplarReservoir[N] |
| 20 |
startTime time.Time |
| 21 |
} |
| 22 |
|
| 23 |
// lastValueMap summarizes a set of measurements as the last one made. |
| 24 |
type lastValueMap[N int64 | float64] struct { |
| 25 |
newRes func(attribute.Set) FilteredExemplarReservoir[N] |
| 26 |
values limitedSyncMap |
| 27 |
} |
| 28 |
|
| 29 |
func (s *lastValueMap[N]) measure( |
| 30 |
ctx context.Context, |
| 31 |
value N, |
| 32 |
fltrAttr attribute.Set, |
| 33 |
droppedAttr []attribute.KeyValue, |
| 34 |
) { |
| 35 |
lv := s.values.LoadOrStoreAttr(fltrAttr, func(attr attribute.Set) any { |
| 36 |
p := &lastValuePoint[N]{ |
| 37 |
res: s.newRes(attr), |
| 38 |
attrs: attr, |
| 39 |
startTime: now(), |
| 40 |
} |
| 41 |
p.value.Store(value) |
| 42 |
return p |
| 43 |
}).(*lastValuePoint[N]) |
| 44 |
|
| 45 |
lv.value.Store(value) |
| 46 |
lv.res.Offer(ctx, value, droppedAttr) |
| 47 |
} |
| 48 |
|
| 49 |
func newDeltaLastValue[N int64 | float64]( |
| 50 |
limit int, |
| 51 |
r func(attribute.Set) FilteredExemplarReservoir[N], |
| 52 |
) *deltaLastValue[N] { |
| 53 |
return &deltaLastValue[N]{ |
| 54 |
newRes: r, |
| 55 |
start: now(), |
| 56 |
hotColdValMap: [2]lastValueMap[N]{ |
| 57 |
{ |
| 58 |
values: limitedSyncMap{aggLimit: limit}, |
| 59 |
newRes: r, |
| 60 |
}, |
| 61 |
{ |
| 62 |
values: limitedSyncMap{aggLimit: limit}, |
| 63 |
newRes: r, |
| 64 |
}, |
| 65 |
}, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// deltaLastValue summarizes a set of measurements as the last one made. |
| 70 |
type deltaLastValue[N int64 | float64] struct { |
| 71 |
newRes func(attribute.Set) FilteredExemplarReservoir[N] |
| 72 |
start time.Time |
| 73 |
|
| 74 |
hcwg hotColdWaitGroup |
| 75 |
hotColdValMap [2]lastValueMap[N] |
| 76 |
} |
| 77 |
|
| 78 |
func (s *deltaLastValue[N]) measure( |
| 79 |
ctx context.Context, |
| 80 |
value N, |
| 81 |
fltrAttr attribute.Set, |
| 82 |
droppedAttr []attribute.KeyValue, |
| 83 |
) { |
| 84 |
hotIdx := s.hcwg.start() |
| 85 |
defer s.hcwg.done(hotIdx) |
| 86 |
s.hotColdValMap[hotIdx].measure(ctx, value, fltrAttr, droppedAttr) |
| 87 |
} |
| 88 |
|
| 89 |
func (s *deltaLastValue[N]) collect( |
| 90 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 91 |
) int { |
| 92 |
t := now() |
| 93 |
n := s.copyAndClearDpts(dest, t) |
| 94 |
// Update start time for delta temporality. |
| 95 |
s.start = t |
| 96 |
return n |
| 97 |
} |
| 98 |
|
| 99 |
// copyAndClearDpts copies the lastValuePoints held by s into dest. The number of lastValuePoints |
| 100 |
// copied is returned. |
| 101 |
func (s *deltaLastValue[N]) copyAndClearDpts( |
| 102 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 103 |
t time.Time, |
| 104 |
) int { |
| 105 |
// Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of |
| 106 |
// the DataPoints is missed (better luck next time). |
| 107 |
gData, _ := (*dest).(metricdata.Gauge[N]) |
| 108 |
// delta always clears values on collection |
| 109 |
readIdx := s.hcwg.swapHotAndWait() |
| 110 |
// The len will not change while we iterate over values, since we waited |
| 111 |
// for all writes to finish to the cold values and len. |
| 112 |
n := s.hotColdValMap[readIdx].values.Len() |
| 113 |
dPts := reset(gData.DataPoints, n, n) |
| 114 |
|
| 115 |
var i int |
| 116 |
s.hotColdValMap[readIdx].values.Range(func(_, value any) bool { |
| 117 |
v := value.(*lastValuePoint[N]) |
| 118 |
dPts[i].Attributes = v.attrs |
| 119 |
dPts[i].StartTime = s.start |
| 120 |
dPts[i].Time = t |
| 121 |
dPts[i].Value = v.value.Load() |
| 122 |
collectExemplars[N](&dPts[i].Exemplars, v.res.Collect) |
| 123 |
i++ |
| 124 |
return true |
| 125 |
}) |
| 126 |
gData.DataPoints = dPts |
| 127 |
// Do not report stale values. |
| 128 |
s.hotColdValMap[readIdx].values.Clear() |
| 129 |
*dest = gData |
| 130 |
return i |
| 131 |
} |
| 132 |
|
| 133 |
// cumulativeLastValue summarizes a set of measurements as the last one made. |
| 134 |
type cumulativeLastValue[N int64 | float64] struct { |
| 135 |
lastValueMap[N] |
| 136 |
start time.Time |
| 137 |
} |
| 138 |
|
| 139 |
func newCumulativeLastValue[N int64 | float64]( |
| 140 |
limit int, |
| 141 |
r func(attribute.Set) FilteredExemplarReservoir[N], |
| 142 |
) *cumulativeLastValue[N] { |
| 143 |
return &cumulativeLastValue[N]{ |
| 144 |
lastValueMap: lastValueMap[N]{ |
| 145 |
values: limitedSyncMap{aggLimit: limit}, |
| 146 |
newRes: r, |
| 147 |
}, |
| 148 |
start: now(), |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
func (s *cumulativeLastValue[N]) collect( |
| 153 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 154 |
) int { |
| 155 |
t := now() |
| 156 |
// Ignore if dest is not a metricdata.Gauge. The chance for memory reuse of |
| 157 |
// the lastValuePoints is missed (better luck next time). |
| 158 |
gData, _ := (*dest).(metricdata.Gauge[N]) |
| 159 |
|
| 160 |
// Values are being concurrently written while we iterate, so only use the |
| 161 |
// current length for capacity. |
| 162 |
dPts := reset(gData.DataPoints, 0, s.values.Len()) |
| 163 |
|
| 164 |
perSeriesStartTimeEnabled := x.PerSeriesStartTimestamps.Enabled() |
| 165 |
|
| 166 |
var i int |
| 167 |
s.values.Range(func(_, value any) bool { |
| 168 |
v := value.(*lastValuePoint[N]) |
| 169 |
|
| 170 |
startTime := s.start |
| 171 |
if perSeriesStartTimeEnabled { |
| 172 |
startTime = v.startTime |
| 173 |
} |
| 174 |
newPt := metricdata.DataPoint[N]{ |
| 175 |
Attributes: v.attrs, |
| 176 |
StartTime: startTime, |
| 177 |
Time: t, |
| 178 |
Value: v.value.Load(), |
| 179 |
} |
| 180 |
collectExemplars[N](&newPt.Exemplars, v.res.Collect) |
| 181 |
dPts = append(dPts, newPt) |
| 182 |
i++ |
| 183 |
return true |
| 184 |
}) |
| 185 |
gData.DataPoints = dPts |
| 186 |
// TODO (#3006): This will use an unbounded amount of memory if there |
| 187 |
// are unbounded number of attribute sets being aggregated. Attribute |
| 188 |
// sets that become "stale" need to be forgotten so this will not |
| 189 |
// overload the system. |
| 190 |
*dest = gData |
| 191 |
|
| 192 |
return i |
| 193 |
} |
| 194 |
|
| 195 |
// newPrecomputedLastValue returns an aggregator that summarizes a set of |
| 196 |
// observations as the last one made. |
| 197 |
func newPrecomputedLastValue[N int64 | float64]( |
| 198 |
limit int, |
| 199 |
r func(attribute.Set) FilteredExemplarReservoir[N], |
| 200 |
) *precomputedLastValue[N] { |
| 201 |
return &precomputedLastValue[N]{deltaLastValue: newDeltaLastValue[N](limit, r)} |
| 202 |
} |
| 203 |
|
| 204 |
// precomputedLastValue summarizes a set of observations as the last one made. |
| 205 |
type precomputedLastValue[N int64 | float64] struct { |
| 206 |
*deltaLastValue[N] |
| 207 |
} |
| 208 |
|
| 209 |
func (s *precomputedLastValue[N]) delta( |
| 210 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 211 |
) int { |
| 212 |
return s.collect(dest) |
| 213 |
} |
| 214 |
|
| 215 |
func (s *precomputedLastValue[N]) cumulative( |
| 216 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 217 |
) int { |
| 218 |
// Do not reset the start time. |
| 219 |
return s.copyAndClearDpts(dest, now()) |
| 220 |
} |