Debian Codegraph @

sdk/metric/internal/aggregate/aggregate_test.go

raw
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 "strconv"
9 "sync"
10 "sync/atomic"
11 "testing"
12 "time"
13
14 "github.com/stretchr/testify/assert"
15
16 "go.opentelemetry.io/otel/attribute"
17 "go.opentelemetry.io/otel/sdk/metric/metricdata"
18 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
19 )
20
21 var (
22 keyUser = "user"
23 userAlice = attribute.String(keyUser, "Alice")
24 userBob = attribute.String(keyUser, "Bob")
25 userCarol = attribute.String(keyUser, "Carol")
26 userDave = attribute.String(keyUser, "Dave")
27 adminTrue = attribute.Bool("admin", true)
28 adminFalse = attribute.Bool("admin", false)
29
30 alice = attribute.NewSet(userAlice, adminTrue)
31 bob = attribute.NewSet(userBob, adminFalse)
32 carol = attribute.NewSet(userCarol, adminFalse)
33 dave = attribute.NewSet(userDave, adminFalse)
34
35 // Filtered.
36 attrFltr = func(kv attribute.KeyValue) bool {
37 return kv.Key == attribute.Key(keyUser)
38 }
39 fltrAlice = attribute.NewSet(userAlice)
40 fltrBob = attribute.NewSet(userBob)
41
42 // Sat Jan 01 2000 00:00:00 GMT+0000.
43 y2k = time.Unix(946684800, 0)
44 )
45
46 // y2kPlus returns the timestamp at n seconds past Sat Jan 01 2000 00:00:00 GMT+0000.
47 func y2kPlus(n int64) time.Time {
48 d := time.Duration(n) * time.Second
49 return y2k.Add(d)
50 }
51
52 // clock is a test clock. It provides a predictable value for now() that can be
53 // reset.
54 type clock struct {
55 ticks atomic.Int64
56 }
57
58 // Now returns the mocked time starting at y2kPlus(0). Each call to Now will
59 // increment the returned value by one second.
60 func (c *clock) Now() time.Time {
61 old := c.ticks.Add(1) - 1
62 return y2kPlus(old)
63 }
64
65 // Reset resets the clock c to tick from y2kPlus(0).
66 func (c *clock) Reset() { c.ticks.Store(0) }
67
68 // Register registers clock c's Now method as the now var. It returns an
69 // unregister func that should be called to restore the original now value.
70 func (c *clock) Register() (unregister func()) {
71 orig := now
72 now = c.Now
73 return func() { now = orig }
74 }
75
76 func dropExemplars[N int64 | float64](attr attribute.Set) FilteredExemplarReservoir[N] {
77 return dropReservoir[N](attr)
78 }
79
80 func TestBuilderFilter(t *testing.T) {
81 t.Run("Int64", testBuilderFilter[int64]())
82 t.Run("Float64", testBuilderFilter[float64]())
83 }
84
85 func testBuilderFilter[N int64 | float64]() func(t *testing.T) {
86 return func(t *testing.T) {
87 t.Helper()
88
89 value, attr := N(1), alice
90 run := func(b Builder[N], wantF attribute.Set, wantD []attribute.KeyValue) func(*testing.T) {
91 return func(t *testing.T) {
92 t.Helper()
93
94 meas := b.filter(func(_ context.Context, v N, f attribute.Set, d []attribute.KeyValue) {
95 assert.Equal(t, value, v, "measured incorrect value")
96 assert.Equal(t, wantF, f, "measured incorrect filtered attributes")
97 assert.ElementsMatch(t, wantD, d, "measured incorrect dropped attributes")
98 })
99 meas(t.Context(), value, attr)
100 }
101 }
102
103 t.Run("NoFilter", run(Builder[N]{}, attr, nil))
104 t.Run("Filter", run(Builder[N]{Filter: attrFltr}, fltrAlice, []attribute.KeyValue{adminTrue}))
105 }
106 }
107
108 type arg[N int64 | float64] struct {
109 ctx context.Context
110
111 value N
112 attr attribute.Set
113 }
114
115 type output struct {
116 n int
117 agg metricdata.Aggregation
118 }
119
120 type teststep[N int64 | float64] struct {
121 input []arg[N]
122 expect output
123 }
124
125 func test[N int64 | float64](meas Measure[N], comp ComputeAggregation, steps []teststep[N]) func(*testing.T) {
126 return func(t *testing.T) {
127 t.Helper()
128
129 got := new(metricdata.Aggregation)
130 for i, step := range steps {
131 for _, args := range step.input {
132 meas(args.ctx, args.value, args.attr)
133 }
134
135 t.Logf("step: %d", i)
136 assert.Equal(t, step.expect.n, comp(got), "incorrect data size")
137 metricdatatest.AssertAggregationsEqual(t, step.expect.agg, *got)
138 }
139 }
140 }
141
142 func getConcurrentVals[N int64 | float64]() []N {
143 // Keep length of v in sync with concurrentNumRecords
144 // and expectedConcurrentSum.
145 switch any(*new(N)).(type) {
146 case float64:
147 v := []float64{2.5, 6.1, 4.4, 10.0, 22.0, -3.5, -6.5, 3.0, -6.0}
148 return any(v).([]N)
149 default:
150 v := []int64{2, 6, 4, 10, 22, -3, -6, 3, -6}
151 return any(v).([]N)
152 }
153 }
154
155 const (
156 concurrentValsSum = 32
157 concurrentNumGoroutines = 10
158 concurrentNumRecords = 90 // Multiple of 9 (length of values sequences)
159 expectedConcurrentCount = uint64(concurrentNumGoroutines * concurrentNumRecords)
160 )
161
162 func expectedConcurrentSum[N int64 | float64]() N {
163 return N(int64(concurrentNumGoroutines) * int64(concurrentNumRecords/9) * concurrentValsSum)
164 }
165
166 // testAggregationConcurrentSafe provides a unified stress test for all generic aggregators
167 // by generating high contention, cardinality limit overflow, and validating exact results.
168 func testAggregationConcurrentSafe[N int64 | float64](
169 meas Measure[N],
170 comp ComputeAggregation,
171 validate func(t *testing.T, aggs []metricdata.Aggregation),
172 ) func(*testing.T) {
173 return func(t *testing.T) {
174 t.Helper()
175
176 ctx := t.Context()
177 var wg sync.WaitGroup
178
179 // Use 10 different attribute sets to force overflow on the AggregationLimit
180 // which is typically set to 3.
181 attrs := make([]attribute.Set, concurrentNumGoroutines)
182 for i := range attrs {
183 attrs[i] = attribute.NewSet(attribute.String(keyUser, strconv.Itoa(i)))
184 }
185
186 vals := getConcurrentVals[N]()
187
188 wg.Add(concurrentNumGoroutines)
189 for i := range concurrentNumGoroutines {
190 go func(id int) {
191 defer wg.Done()
192 // Each goroutine records to a distinct attribute set
193 attr := attrs[id]
194
195 for j := range concurrentNumRecords {
196 meas(ctx, vals[j%len(vals)], attr)
197 }
198 }(i)
199 }
200
201 var results []metricdata.Aggregation
202
203 // Run computation concurrently with measurements to stress hot/cold swaps
204 wg.Go(func() {
205 for range concurrentNumRecords {
206 got := new(metricdata.Aggregation)
207 comp(got)
208 results = append(results, *got)
209 }
210 })
211
212 wg.Wait()
213
214 // Final flush to get final values
215 got := new(metricdata.Aggregation)
216 comp(got)
217 results = append(results, *got)
218
219 validate(t, results)
220 }
221 }
222
223 func assertSumEqual[N int64 | float64](t *testing.T, expected, actual N) {
224 if _, ok := any(*new(N)).(float64); ok {
225 assert.InDelta(t, float64(expected), float64(actual), 0.0001)
226 } else {
227 assert.Equal(t, expected, actual)
228 }
229 }
230
231 func benchmarkAggregate[N int64 | float64](factory func() (Measure[N], ComputeAggregation)) func(*testing.B) {
232 counts := []int{1, 10, 100}
233 return func(b *testing.B) {
234 for _, n := range counts {
235 b.Run(strconv.Itoa(n), func(b *testing.B) {
236 benchmarkAggregateN(b, factory, n)
237 })
238 }
239 }
240 }
241
242 var bmarkRes metricdata.Aggregation
243
244 func benchmarkAggregateN[N int64 | float64](b *testing.B, factory func() (Measure[N], ComputeAggregation), count int) {
245 ctx := b.Context()
246 attrs := make([]attribute.Set, count)
247 for i := range attrs {
248 attrs[i] = attribute.NewSet(attribute.Int("value", i))
249 }
250
251 b.Run("Measure", func(b *testing.B) {
252 got := &bmarkRes
253 meas, comp := factory()
254 b.ReportAllocs()
255 b.ResetTimer()
256
257 for n := 0; n < b.N; n++ {
258 for _, attr := range attrs {
259 meas(ctx, 1, attr)
260 }
261 }
262
263 comp(got)
264 })
265
266 b.Run("ComputeAggregation", func(b *testing.B) {
267 comps := make([]ComputeAggregation, b.N)
268 for n := range comps {
269 meas, comp := factory()
270 for _, attr := range attrs {
271 meas(ctx, 1, attr)
272 }
273 comps[n] = comp
274 }
275
276 got := &bmarkRes
277 b.ReportAllocs()
278 b.ResetTimer()
279
280 for n := 0; n < b.N; n++ {
281 comps[n](got)
282 }
283 })
284 }