| 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 |
type sumValue[N int64 | float64] struct { |
| 16 |
n atomicCounter[N] |
| 17 |
res FilteredExemplarReservoir[N] |
| 18 |
attrs attribute.Set |
| 19 |
startTime time.Time |
| 20 |
} |
| 21 |
|
| 22 |
type sumValueMap[N int64 | float64] struct { |
| 23 |
values limitedSyncMap |
| 24 |
newRes func(attribute.Set) FilteredExemplarReservoir[N] |
| 25 |
} |
| 26 |
|
| 27 |
func (s *sumValueMap[N]) measure( |
| 28 |
ctx context.Context, |
| 29 |
value N, |
| 30 |
fltrAttr attribute.Set, |
| 31 |
droppedAttr []attribute.KeyValue, |
| 32 |
) { |
| 33 |
sv := s.values.LoadOrStoreAttr(fltrAttr, func(attr attribute.Set) any { |
| 34 |
return &sumValue[N]{ |
| 35 |
res: s.newRes(attr), |
| 36 |
attrs: attr, |
| 37 |
startTime: now(), |
| 38 |
} |
| 39 |
}).(*sumValue[N]) |
| 40 |
sv.n.add(value) |
| 41 |
// It is possible for collection to race with measurement and observe the |
| 42 |
// exemplar in the batch of metrics after the add() for cumulative sums. |
| 43 |
// This is an accepted tradeoff to avoid locking during measurement. |
| 44 |
sv.res.Offer(ctx, value, droppedAttr) |
| 45 |
} |
| 46 |
|
| 47 |
// newDeltaSum returns an aggregator that summarizes a set of measurements as |
| 48 |
// their arithmetic sum. Each sum is scoped by attributes and the aggregation |
| 49 |
// cycle the measurements were made in. |
| 50 |
func newDeltaSum[N int64 | float64]( |
| 51 |
monotonic bool, |
| 52 |
limit int, |
| 53 |
r func(attribute.Set) FilteredExemplarReservoir[N], |
| 54 |
) *deltaSum[N] { |
| 55 |
return &deltaSum[N]{ |
| 56 |
monotonic: monotonic, |
| 57 |
start: now(), |
| 58 |
hotColdValMap: [2]sumValueMap[N]{ |
| 59 |
{ |
| 60 |
values: limitedSyncMap{aggLimit: limit}, |
| 61 |
newRes: r, |
| 62 |
}, |
| 63 |
{ |
| 64 |
values: limitedSyncMap{aggLimit: limit}, |
| 65 |
newRes: r, |
| 66 |
}, |
| 67 |
}, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// deltaSum is the storage for sums which resets every collection interval. |
| 72 |
type deltaSum[N int64 | float64] struct { |
| 73 |
monotonic bool |
| 74 |
start time.Time |
| 75 |
|
| 76 |
hcwg hotColdWaitGroup |
| 77 |
hotColdValMap [2]sumValueMap[N] |
| 78 |
} |
| 79 |
|
| 80 |
func (s *deltaSum[N]) measure(ctx context.Context, value N, fltrAttr attribute.Set, droppedAttr []attribute.KeyValue) { |
| 81 |
hotIdx := s.hcwg.start() |
| 82 |
defer s.hcwg.done(hotIdx) |
| 83 |
s.hotColdValMap[hotIdx].measure(ctx, value, fltrAttr, droppedAttr) |
| 84 |
} |
| 85 |
|
| 86 |
func (s *deltaSum[N]) collect( |
| 87 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 88 |
) int { |
| 89 |
t := now() |
| 90 |
|
| 91 |
// If *dest is not a metricdata.Sum, memory reuse is missed. In that case, |
| 92 |
// use the zero-value sData and hope for better alignment next cycle. |
| 93 |
sData, _ := (*dest).(metricdata.Sum[N]) |
| 94 |
sData.Temporality = metricdata.DeltaTemporality |
| 95 |
sData.IsMonotonic = s.monotonic |
| 96 |
|
| 97 |
// delta always clears values on collection |
| 98 |
readIdx := s.hcwg.swapHotAndWait() |
| 99 |
// The len will not change while we iterate over values, since we waited |
| 100 |
// for all writes to finish to the cold values and len. |
| 101 |
n := s.hotColdValMap[readIdx].values.Len() |
| 102 |
dPts := reset(sData.DataPoints, n, n) |
| 103 |
|
| 104 |
var i int |
| 105 |
s.hotColdValMap[readIdx].values.Range(func(_, value any) bool { |
| 106 |
val := value.(*sumValue[N]) |
| 107 |
collectExemplars(&dPts[i].Exemplars, val.res.Collect) |
| 108 |
dPts[i].Attributes = val.attrs |
| 109 |
dPts[i].StartTime = s.start |
| 110 |
dPts[i].Time = t |
| 111 |
dPts[i].Value = val.n.load() |
| 112 |
i++ |
| 113 |
return true |
| 114 |
}) |
| 115 |
s.hotColdValMap[readIdx].values.Clear() |
| 116 |
// The delta collection cycle resets. |
| 117 |
s.start = t |
| 118 |
|
| 119 |
sData.DataPoints = dPts |
| 120 |
*dest = sData |
| 121 |
|
| 122 |
return i |
| 123 |
} |
| 124 |
|
| 125 |
// newCumulativeSum returns an aggregator that summarizes a set of measurements |
| 126 |
// as their arithmetic sum. Each sum is scoped by attributes and the |
| 127 |
// aggregation cycle the measurements were made in. |
| 128 |
func newCumulativeSum[N int64 | float64]( |
| 129 |
monotonic bool, |
| 130 |
limit int, |
| 131 |
r func(attribute.Set) FilteredExemplarReservoir[N], |
| 132 |
) *cumulativeSum[N] { |
| 133 |
return &cumulativeSum[N]{ |
| 134 |
monotonic: monotonic, |
| 135 |
start: now(), |
| 136 |
sumValueMap: sumValueMap[N]{ |
| 137 |
values: limitedSyncMap{aggLimit: limit}, |
| 138 |
newRes: r, |
| 139 |
}, |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// deltaSum is the storage for sums which never reset. |
| 144 |
type cumulativeSum[N int64 | float64] struct { |
| 145 |
monotonic bool |
| 146 |
start time.Time |
| 147 |
|
| 148 |
sumValueMap[N] |
| 149 |
} |
| 150 |
|
| 151 |
func (s *cumulativeSum[N]) collect( |
| 152 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 153 |
) int { |
| 154 |
t := now() |
| 155 |
|
| 156 |
// If *dest is not a metricdata.Sum, memory reuse is missed. In that case, |
| 157 |
// use the zero-value sData and hope for better alignment next cycle. |
| 158 |
sData, _ := (*dest).(metricdata.Sum[N]) |
| 159 |
sData.Temporality = metricdata.CumulativeTemporality |
| 160 |
sData.IsMonotonic = s.monotonic |
| 161 |
|
| 162 |
// Values are being concurrently written while we iterate, so only use the |
| 163 |
// current length for capacity. |
| 164 |
dPts := reset(sData.DataPoints, 0, s.values.Len()) |
| 165 |
|
| 166 |
perSeriesStartTimeEnabled := x.PerSeriesStartTimestamps.Enabled() |
| 167 |
|
| 168 |
var i int |
| 169 |
s.values.Range(func(_, value any) bool { |
| 170 |
val := value.(*sumValue[N]) |
| 171 |
|
| 172 |
startTime := s.start |
| 173 |
if perSeriesStartTimeEnabled { |
| 174 |
startTime = val.startTime |
| 175 |
} |
| 176 |
newPt := metricdata.DataPoint[N]{ |
| 177 |
Attributes: val.attrs, |
| 178 |
StartTime: startTime, |
| 179 |
Time: t, |
| 180 |
Value: val.n.load(), |
| 181 |
} |
| 182 |
collectExemplars(&newPt.Exemplars, val.res.Collect) |
| 183 |
dPts = append(dPts, newPt) |
| 184 |
// TODO (#3006): This will use an unbounded amount of memory if there |
| 185 |
// are unbounded number of attribute sets being aggregated. Attribute |
| 186 |
// sets that become "stale" need to be forgotten so this will not |
| 187 |
// overload the system. |
| 188 |
i++ |
| 189 |
return true |
| 190 |
}) |
| 191 |
|
| 192 |
sData.DataPoints = dPts |
| 193 |
*dest = sData |
| 194 |
|
| 195 |
return i |
| 196 |
} |
| 197 |
|
| 198 |
// newPrecomputedSum returns an aggregator that summarizes a set of |
| 199 |
// observations as their arithmetic sum. Each sum is scoped by attributes and |
| 200 |
// the aggregation cycle the measurements were made in. |
| 201 |
func newPrecomputedSum[N int64 | float64]( |
| 202 |
monotonic bool, |
| 203 |
limit int, |
| 204 |
r func(attribute.Set) FilteredExemplarReservoir[N], |
| 205 |
) *precomputedSum[N] { |
| 206 |
return &precomputedSum[N]{ |
| 207 |
deltaSum: newDeltaSum(monotonic, limit, r), |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
// precomputedSum summarizes a set of observations as their arithmetic sum. |
| 212 |
type precomputedSum[N int64 | float64] struct { |
| 213 |
*deltaSum[N] |
| 214 |
|
| 215 |
reported map[any]N |
| 216 |
} |
| 217 |
|
| 218 |
func (s *precomputedSum[N]) delta( |
| 219 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 220 |
) int { |
| 221 |
t := now() |
| 222 |
newReported := make(map[any]N) |
| 223 |
|
| 224 |
// If *dest is not a metricdata.Sum, memory reuse is missed. In that case, |
| 225 |
// use the zero-value sData and hope for better alignment next cycle. |
| 226 |
sData, _ := (*dest).(metricdata.Sum[N]) |
| 227 |
sData.Temporality = metricdata.DeltaTemporality |
| 228 |
sData.IsMonotonic = s.monotonic |
| 229 |
|
| 230 |
// delta always clears values on collection |
| 231 |
readIdx := s.hcwg.swapHotAndWait() |
| 232 |
// The len will not change while we iterate over values, since we waited |
| 233 |
// for all writes to finish to the cold values and len. |
| 234 |
n := s.hotColdValMap[readIdx].values.Len() |
| 235 |
dPts := reset(sData.DataPoints, n, n) |
| 236 |
|
| 237 |
var i int |
| 238 |
s.hotColdValMap[readIdx].values.Range(func(key, value any) bool { |
| 239 |
val := value.(*sumValue[N]) |
| 240 |
n := val.n.load() |
| 241 |
|
| 242 |
delta := n - s.reported[key] |
| 243 |
collectExemplars(&dPts[i].Exemplars, val.res.Collect) |
| 244 |
dPts[i].Attributes = val.attrs |
| 245 |
dPts[i].StartTime = s.start |
| 246 |
dPts[i].Time = t |
| 247 |
dPts[i].Value = delta |
| 248 |
newReported[key] = n |
| 249 |
i++ |
| 250 |
return true |
| 251 |
}) |
| 252 |
s.hotColdValMap[readIdx].values.Clear() |
| 253 |
s.reported = newReported |
| 254 |
// The delta collection cycle resets. |
| 255 |
s.start = t |
| 256 |
|
| 257 |
sData.DataPoints = dPts |
| 258 |
*dest = sData |
| 259 |
|
| 260 |
return i |
| 261 |
} |
| 262 |
|
| 263 |
func (s *precomputedSum[N]) cumulative( |
| 264 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 265 |
) int { |
| 266 |
t := now() |
| 267 |
|
| 268 |
// If *dest is not a metricdata.Sum, memory reuse is missed. In that case, |
| 269 |
// use the zero-value sData and hope for better alignment next cycle. |
| 270 |
sData, _ := (*dest).(metricdata.Sum[N]) |
| 271 |
sData.Temporality = metricdata.CumulativeTemporality |
| 272 |
sData.IsMonotonic = s.monotonic |
| 273 |
|
| 274 |
// cumulative precomputed always clears values on collection |
| 275 |
readIdx := s.hcwg.swapHotAndWait() |
| 276 |
// The len will not change while we iterate over values, since we waited |
| 277 |
// for all writes to finish to the cold values and len. |
| 278 |
n := s.hotColdValMap[readIdx].values.Len() |
| 279 |
dPts := reset(sData.DataPoints, n, n) |
| 280 |
|
| 281 |
var i int |
| 282 |
s.hotColdValMap[readIdx].values.Range(func(_, value any) bool { |
| 283 |
val := value.(*sumValue[N]) |
| 284 |
collectExemplars(&dPts[i].Exemplars, val.res.Collect) |
| 285 |
dPts[i].Attributes = val.attrs |
| 286 |
dPts[i].StartTime = s.start |
| 287 |
dPts[i].Time = t |
| 288 |
dPts[i].Value = val.n.load() |
| 289 |
i++ |
| 290 |
return true |
| 291 |
}) |
| 292 |
s.hotColdValMap[readIdx].values.Clear() |
| 293 |
|
| 294 |
sData.DataPoints = dPts |
| 295 |
*dest = sData |
| 296 |
|
| 297 |
return i |
| 298 |
} |