Debian Codegraph @

sdk/metric/pipeline_registry_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package metric // import "go.opentelemetry.io/otel/sdk/metric"
5
6 import (
7 "sync/atomic"
8 "testing"
9
10 "github.com/go-logr/logr"
11 "github.com/go-logr/logr/testr"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14
15 "go.opentelemetry.io/otel"
16 "go.opentelemetry.io/otel/attribute"
17 "go.opentelemetry.io/otel/sdk/metric/exemplar"
18 "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
19 "go.opentelemetry.io/otel/sdk/metric/metricdata"
20 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
21 "go.opentelemetry.io/otel/sdk/resource"
22 )
23
24 var defaultView = NewView(Instrument{Name: "*"}, Stream{})
25
26 type invalidAggregation struct{}
27
28 func (invalidAggregation) copy() Aggregation {
29 return invalidAggregation{}
30 }
31
32 func (invalidAggregation) err() error {
33 return nil
34 }
35
36 func requireN[N int64 | float64](
37 t *testing.T,
38 n int,
39 m []aggregate.Measure[N],
40 comps []aggregate.ComputeAggregation,
41 err error,
42 ) {
43 t.Helper()
44 assert.NoError(t, err)
45 require.Len(t, m, n)
46 require.Len(t, comps, n)
47 }
48
49 func assertSum[N int64 | float64](
50 n int,
51 temp metricdata.Temporality,
52 mono bool,
53 v [2]N,
54 ) func(*testing.T, []aggregate.Measure[N], []aggregate.ComputeAggregation, error) {
55 return func(t *testing.T, meas []aggregate.Measure[N], comps []aggregate.ComputeAggregation, err error) {
56 t.Helper()
57 requireN[N](t, n, meas, comps, err)
58
59 for m := range n {
60 t.Logf("input/output number: %d", m)
61 in, out := meas[m], comps[m]
62 in(t.Context(), 1, *attribute.EmptySet())
63
64 var got metricdata.Aggregation
65 assert.Equal(t, 1, out(&got), "1 data-point expected")
66 metricdatatest.AssertAggregationsEqual(t, metricdata.Sum[N]{
67 Temporality: temp,
68 IsMonotonic: mono,
69 DataPoints: []metricdata.DataPoint[N]{{Value: v[0]}},
70 }, got, metricdatatest.IgnoreTimestamp())
71
72 in(t.Context(), 3, *attribute.EmptySet())
73
74 assert.Equal(t, 1, out(&got), "1 data-point expected")
75 metricdatatest.AssertAggregationsEqual(t, metricdata.Sum[N]{
76 Temporality: temp,
77 IsMonotonic: mono,
78 DataPoints: []metricdata.DataPoint[N]{{Value: v[1]}},
79 }, got, metricdatatest.IgnoreTimestamp())
80 }
81 }
82 }
83
84 func assertHist[N int64 | float64](
85 temp metricdata.Temporality,
86 ) func(*testing.T, []aggregate.Measure[N], []aggregate.ComputeAggregation, error) {
87 return func(t *testing.T, meas []aggregate.Measure[N], comps []aggregate.ComputeAggregation, err error) {
88 t.Helper()
89 requireN[N](t, 1, meas, comps, err)
90
91 in, out := meas[0], comps[0]
92 in(t.Context(), 1, *attribute.EmptySet())
93
94 var got metricdata.Aggregation
95 assert.Equal(t, 1, out(&got), "1 data-point expected")
96 buckets := []uint64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
97 n := 1
98 metricdatatest.AssertAggregationsEqual(t, metricdata.Histogram[N]{
99 Temporality: temp,
100 DataPoints: []metricdata.HistogramDataPoint[N]{{
101 Count: uint64(n),
102 Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000},
103 BucketCounts: buckets,
104 Min: metricdata.NewExtrema[N](1),
105 Max: metricdata.NewExtrema[N](1),
106 Sum: N(n),
107 }},
108 }, got, metricdatatest.IgnoreTimestamp())
109
110 in(t.Context(), 1, *attribute.EmptySet())
111
112 if temp == metricdata.CumulativeTemporality {
113 buckets[1] = 2
114 n = 2
115 }
116 assert.Equal(t, 1, out(&got), "1 data-point expected")
117 metricdatatest.AssertAggregationsEqual(t, metricdata.Histogram[N]{
118 Temporality: temp,
119 DataPoints: []metricdata.HistogramDataPoint[N]{{
120 Count: uint64(n),
121 Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000},
122 BucketCounts: buckets,
123 Min: metricdata.NewExtrema[N](1),
124 Max: metricdata.NewExtrema[N](1),
125 Sum: N(n),
126 }},
127 }, got, metricdatatest.IgnoreTimestamp())
128 }
129 }
130
131 func assertLastValue[N int64 | float64](
132 t *testing.T,
133 meas []aggregate.Measure[N],
134 comps []aggregate.ComputeAggregation,
135 err error,
136 ) {
137 t.Helper()
138 requireN[N](t, 1, meas, comps, err)
139
140 in, out := meas[0], comps[0]
141 in(t.Context(), 10, *attribute.EmptySet())
142 in(t.Context(), 1, *attribute.EmptySet())
143
144 var got metricdata.Aggregation
145 assert.Equal(t, 1, out(&got), "1 data-point expected")
146 metricdatatest.AssertAggregationsEqual(t, metricdata.Gauge[N]{
147 DataPoints: []metricdata.DataPoint[N]{{Value: 1}},
148 }, got, metricdatatest.IgnoreTimestamp())
149 }
150
151 func testCreateAggregators[N int64 | float64](t *testing.T) {
152 changeAggView := NewView(
153 Instrument{Name: "foo"},
154 Stream{Aggregation: AggregationExplicitBucketHistogram{
155 Boundaries: []float64{0, 100},
156 NoMinMax: true,
157 }},
158 )
159 renameView := NewView(Instrument{Name: "foo"}, Stream{Name: "bar"})
160 invalidAggView := NewView(
161 Instrument{Name: "foo"},
162 Stream{Aggregation: invalidAggregation{}},
163 )
164
165 instruments := []Instrument{
166 InstrumentKind(0): {Name: "foo", Kind: InstrumentKind(0)}, // Unknown kind
167 InstrumentKindCounter: {Name: "foo", Kind: InstrumentKindCounter},
168 InstrumentKindUpDownCounter: {Name: "foo", Kind: InstrumentKindUpDownCounter},
169 InstrumentKindHistogram: {Name: "foo", Kind: InstrumentKindHistogram},
170 InstrumentKindGauge: {Name: "foo", Kind: InstrumentKindGauge},
171 InstrumentKindObservableCounter: {Name: "foo", Kind: InstrumentKindObservableCounter},
172 InstrumentKindObservableUpDownCounter: {Name: "foo", Kind: InstrumentKindObservableUpDownCounter},
173 InstrumentKindObservableGauge: {Name: "foo", Kind: InstrumentKindObservableGauge},
174 }
175
176 testcases := []struct {
177 name string
178 reader Reader
179 views []View
180 inst Instrument
181 validate func(*testing.T, []aggregate.Measure[N], []aggregate.ComputeAggregation, error)
182 }{
183 {
184 name: "Default/Drop",
185 reader: NewManualReader(
186 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDrop{} }),
187 ),
188 inst: instruments[InstrumentKindCounter],
189 validate: func(t *testing.T, meas []aggregate.Measure[N], comps []aggregate.ComputeAggregation, err error) {
190 t.Helper()
191 assert.NoError(t, err)
192 assert.Empty(t, meas)
193 assert.Empty(t, comps)
194 },
195 },
196 {
197 name: "Default/Delta/Sum/NonMonotonic",
198 reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
199 inst: instruments[InstrumentKindUpDownCounter],
200 validate: assertSum[N](1, metricdata.DeltaTemporality, false, [2]N{1, 3}),
201 },
202 {
203 name: "Default/Delta/ExplicitBucketHistogram",
204 reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
205 inst: instruments[InstrumentKindHistogram],
206 validate: assertHist[N](metricdata.DeltaTemporality),
207 },
208 {
209 name: "Default/Delta/Gauge",
210 reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
211 inst: instruments[InstrumentKindGauge],
212 validate: assertLastValue[N],
213 },
214 {
215 name: "Default/Delta/PrecomputedSum/Monotonic",
216 reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
217 inst: instruments[InstrumentKindObservableCounter],
218 validate: assertSum[N](1, metricdata.DeltaTemporality, true, [2]N{1, 2}),
219 },
220 {
221 name: "Default/Delta/PrecomputedSum/NonMonotonic",
222 reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
223 inst: instruments[InstrumentKindObservableUpDownCounter],
224 validate: assertSum[N](1, metricdata.DeltaTemporality, false, [2]N{1, 2}),
225 },
226 {
227 name: "Default/Delta/Gauge",
228 reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
229 inst: instruments[InstrumentKindObservableGauge],
230 validate: assertLastValue[N],
231 },
232 {
233 name: "Default/Delta/Sum/Monotonic",
234 reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
235 inst: instruments[InstrumentKindCounter],
236 validate: assertSum[N](1, metricdata.DeltaTemporality, true, [2]N{1, 3}),
237 },
238 {
239 name: "Default/Cumulative/Sum/NonMonotonic",
240 reader: NewManualReader(),
241 inst: instruments[InstrumentKindUpDownCounter],
242 validate: assertSum[N](1, metricdata.CumulativeTemporality, false, [2]N{1, 4}),
243 },
244 {
245 name: "Default/Cumulative/ExplicitBucketHistogram",
246 reader: NewManualReader(),
247 inst: instruments[InstrumentKindHistogram],
248 validate: assertHist[N](metricdata.CumulativeTemporality),
249 },
250 {
251 name: "Default/Cumulative/Gauge",
252 reader: NewManualReader(),
253 inst: instruments[InstrumentKindGauge],
254 validate: assertLastValue[N],
255 },
256 {
257 name: "Default/Cumulative/PrecomputedSum/Monotonic",
258 reader: NewManualReader(),
259 inst: instruments[InstrumentKindObservableCounter],
260 validate: assertSum[N](1, metricdata.CumulativeTemporality, true, [2]N{1, 3}),
261 },
262 {
263 name: "Default/Cumulative/PrecomputedSum/NonMonotonic",
264 reader: NewManualReader(),
265 inst: instruments[InstrumentKindObservableUpDownCounter],
266 validate: assertSum[N](1, metricdata.CumulativeTemporality, false, [2]N{1, 3}),
267 },
268 {
269 name: "Default/Cumulative/Gauge",
270 reader: NewManualReader(),
271 inst: instruments[InstrumentKindObservableGauge],
272 validate: assertLastValue[N],
273 },
274 {
275 name: "Default/Cumulative/Sum/Monotonic",
276 reader: NewManualReader(),
277 inst: instruments[InstrumentKindCounter],
278 validate: assertSum[N](1, metricdata.CumulativeTemporality, true, [2]N{1, 4}),
279 },
280 {
281 name: "ViewHasPrecedence",
282 reader: NewManualReader(),
283 views: []View{changeAggView},
284 inst: instruments[InstrumentKindCounter],
285 validate: func(t *testing.T, meas []aggregate.Measure[N], comps []aggregate.ComputeAggregation, err error) {
286 t.Helper()
287 requireN[N](t, 1, meas, comps, err)
288
289 in, out := meas[0], comps[0]
290 in(t.Context(), 1, *attribute.EmptySet())
291
292 var got metricdata.Aggregation
293 assert.Equal(t, 1, out(&got), "1 data-point expected")
294 metricdatatest.AssertAggregationsEqual(t, metricdata.Histogram[N]{
295 Temporality: metricdata.CumulativeTemporality,
296 DataPoints: []metricdata.HistogramDataPoint[N]{{
297 Count: 1,
298 Bounds: []float64{0, 100},
299 BucketCounts: []uint64{0, 1, 0},
300 Sum: 1,
301 }},
302 }, got, metricdatatest.IgnoreTimestamp())
303
304 in(t.Context(), 1, *attribute.EmptySet())
305
306 assert.Equal(t, 1, out(&got), "1 data-point expected")
307 metricdatatest.AssertAggregationsEqual(t, metricdata.Histogram[N]{
308 Temporality: metricdata.CumulativeTemporality,
309 DataPoints: []metricdata.HistogramDataPoint[N]{{
310 Count: 2,
311 Bounds: []float64{0, 100},
312 BucketCounts: []uint64{0, 2, 0},
313 Sum: 2,
314 }},
315 }, got, metricdatatest.IgnoreTimestamp())
316 },
317 },
318 {
319 name: "MultipleViews",
320 reader: NewManualReader(),
321 views: []View{defaultView, renameView},
322 inst: instruments[InstrumentKindCounter],
323 validate: assertSum[N](2, metricdata.CumulativeTemporality, true, [2]N{1, 4}),
324 },
325 {
326 name: "Reader/Default/Cumulative/Sum/Monotonic",
327 reader: NewManualReader(
328 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDefault{} }),
329 ),
330 inst: instruments[InstrumentKindCounter],
331 validate: assertSum[N](1, metricdata.CumulativeTemporality, true, [2]N{1, 4}),
332 },
333 {
334 name: "Reader/Default/Cumulative/Sum/NonMonotonic",
335 reader: NewManualReader(
336 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDefault{} }),
337 ),
338 inst: instruments[InstrumentKindUpDownCounter],
339 validate: assertSum[N](1, metricdata.CumulativeTemporality, false, [2]N{1, 4}),
340 },
341 {
342 name: "Reader/Default/Cumulative/ExplicitBucketHistogram",
343 reader: NewManualReader(
344 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDefault{} }),
345 ),
346 inst: instruments[InstrumentKindHistogram],
347 validate: assertHist[N](metricdata.CumulativeTemporality),
348 },
349 {
350 name: "Reader/Default/Cumulative/Gauge",
351 reader: NewManualReader(
352 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDefault{} }),
353 ),
354 inst: instruments[InstrumentKindGauge],
355 validate: assertLastValue[N],
356 },
357 {
358 name: "Reader/Default/Cumulative/PrecomputedSum/Monotonic",
359 reader: NewManualReader(
360 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDefault{} }),
361 ),
362 inst: instruments[InstrumentKindObservableCounter],
363 validate: assertSum[N](1, metricdata.CumulativeTemporality, true, [2]N{1, 3}),
364 },
365 {
366 name: "Reader/Default/Cumulative/PrecomputedSum/NonMonotonic",
367 reader: NewManualReader(
368 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDefault{} }),
369 ),
370 inst: instruments[InstrumentKindObservableUpDownCounter],
371 validate: assertSum[N](1, metricdata.CumulativeTemporality, false, [2]N{1, 3}),
372 },
373 {
374 name: "Reader/Default/Gauge",
375 reader: NewManualReader(
376 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationDefault{} }),
377 ),
378 inst: instruments[InstrumentKindObservableGauge],
379 validate: assertLastValue[N],
380 },
381 {
382 name: "InvalidAggregation",
383 reader: NewManualReader(),
384 views: []View{invalidAggView},
385 inst: instruments[InstrumentKindCounter],
386 validate: func(t *testing.T, _ []aggregate.Measure[N], _ []aggregate.ComputeAggregation, err error) {
387 assert.ErrorIs(t, err, errCreatingAggregators)
388 },
389 },
390 }
391 for _, tt := range testcases {
392 t.Run(tt.name, func(t *testing.T) {
393 var c cache[string, instID]
394 p := newPipeline(nil, tt.reader, tt.views, exemplar.AlwaysOffFilter, 0)
395 i := newInserter[N](p, &c)
396 readerAggregation := i.readerDefaultAggregation(tt.inst.Kind)
397 input, err := i.Instrument(tt.inst, readerAggregation)
398 var comps []aggregate.ComputeAggregation
399 for _, instSyncs := range p.aggregations {
400 for _, i := range instSyncs {
401 comps = append(comps, i.compAgg)
402 }
403 }
404 tt.validate(t, input, comps, err)
405 })
406 }
407 }
408
409 func TestCreateAggregators(t *testing.T) {
410 t.Run("Int64", testCreateAggregators[int64])
411 t.Run("Float64", testCreateAggregators[float64])
412 }
413
414 func testInvalidInstrumentShouldPanic[N int64 | float64]() {
415 var c cache[string, instID]
416 i := newInserter[N](newPipeline(nil, NewManualReader(), []View{defaultView}, exemplar.AlwaysOffFilter, 0), &c)
417 inst := Instrument{
418 Name: "foo",
419 Kind: InstrumentKind(255),
420 }
421 readerAggregation := i.readerDefaultAggregation(inst.Kind)
422 _, _ = i.Instrument(inst, readerAggregation)
423 }
424
425 func TestInvalidInstrumentShouldPanic(t *testing.T) {
426 assert.Panics(t, testInvalidInstrumentShouldPanic[int64])
427 assert.Panics(t, testInvalidInstrumentShouldPanic[float64])
428 }
429
430 func TestPipelinesAggregatorForEachReader(t *testing.T) {
431 r0, r1 := NewManualReader(), NewManualReader()
432 pipes := newPipelines(resource.Empty(), []Reader{r0, r1}, nil, exemplar.AlwaysOffFilter, 0)
433 require.Len(t, pipes, 2, "created pipelines")
434
435 inst := Instrument{Name: "foo", Kind: InstrumentKindCounter}
436 var c cache[string, instID]
437 r := newResolver[int64](pipes, &c)
438 aggs, err := r.Aggregators(inst)
439 require.NoError(t, err, "resolved Aggregators error")
440 require.Len(t, aggs, 2, "instrument aggregators")
441
442 for i, p := range pipes {
443 var aggN int
444 for _, is := range p.aggregations {
445 aggN += len(is)
446 }
447 assert.Equalf(t, 1, aggN, "pipeline %d: number of instrumentSync", i)
448 }
449 }
450
451 func TestPipelineRegistryCreateAggregators(t *testing.T) {
452 renameView := NewView(Instrument{Name: "foo"}, Stream{Name: "bar"})
453 testRdr := NewManualReader()
454 testRdrHistogram := NewManualReader(
455 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationExplicitBucketHistogram{} }),
456 )
457
458 testCases := []struct {
459 name string
460 readers []Reader
461 views []View
462 inst Instrument
463 wantCount int
464 }{
465 {
466 name: "No views have no aggregators",
467 inst: Instrument{Name: "foo"},
468 },
469 {
470 name: "1 reader 1 view gets 1 aggregator",
471 inst: Instrument{Name: "foo"},
472 readers: []Reader{testRdr},
473 wantCount: 1,
474 },
475 {
476 name: "1 reader 2 views gets 2 aggregator",
477 inst: Instrument{Name: "foo"},
478 readers: []Reader{testRdr},
479 views: []View{defaultView, renameView},
480 wantCount: 2,
481 },
482 {
483 name: "2 readers 1 view each gets 2 aggregators",
484 inst: Instrument{Name: "foo"},
485 readers: []Reader{testRdr, testRdrHistogram},
486 wantCount: 2,
487 },
488 {
489 name: "2 reader 2 views each gets 4 aggregators",
490 inst: Instrument{Name: "foo"},
491 readers: []Reader{testRdr, testRdrHistogram},
492 views: []View{defaultView, renameView},
493 wantCount: 4,
494 },
495 {
496 name: "An instrument is duplicated in two views share the same aggregator",
497 inst: Instrument{Name: "foo"},
498 readers: []Reader{testRdr},
499 views: []View{defaultView, defaultView},
500 wantCount: 1,
501 },
502 }
503
504 for _, tt := range testCases {
505 t.Run(tt.name, func(t *testing.T) {
506 p := newPipelines(resource.Empty(), tt.readers, tt.views, exemplar.AlwaysOffFilter, 0)
507 testPipelineRegistryResolveIntAggregators(t, p, tt.wantCount)
508 testPipelineRegistryResolveFloatAggregators(t, p, tt.wantCount)
509 testPipelineRegistryResolveIntHistogramAggregators(t, p, tt.wantCount)
510 testPipelineRegistryResolveFloatHistogramAggregators(t, p, tt.wantCount)
511 })
512 }
513 }
514
515 func testPipelineRegistryResolveIntAggregators(t *testing.T, p pipelines, wantCount int) {
516 inst := Instrument{Name: "foo", Kind: InstrumentKindCounter}
517 var c cache[string, instID]
518 r := newResolver[int64](p, &c)
519 aggs, err := r.Aggregators(inst)
520 assert.NoError(t, err)
521
522 require.Len(t, aggs, wantCount)
523 }
524
525 func testPipelineRegistryResolveFloatAggregators(t *testing.T, p pipelines, wantCount int) {
526 inst := Instrument{Name: "foo", Kind: InstrumentKindCounter}
527 var c cache[string, instID]
528 r := newResolver[float64](p, &c)
529 aggs, err := r.Aggregators(inst)
530 assert.NoError(t, err)
531
532 require.Len(t, aggs, wantCount)
533 }
534
535 func testPipelineRegistryResolveIntHistogramAggregators(t *testing.T, p pipelines, wantCount int) {
536 inst := Instrument{Name: "foo", Kind: InstrumentKindCounter}
537 var c cache[string, instID]
538 r := newResolver[int64](p, &c)
539 aggs, err := r.HistogramAggregators(inst, []float64{1, 2, 3})
540 assert.NoError(t, err)
541
542 require.Len(t, aggs, wantCount)
543 }
544
545 func testPipelineRegistryResolveFloatHistogramAggregators(t *testing.T, p pipelines, wantCount int) {
546 inst := Instrument{Name: "foo", Kind: InstrumentKindCounter}
547 var c cache[string, instID]
548 r := newResolver[float64](p, &c)
549 aggs, err := r.HistogramAggregators(inst, []float64{1, 2, 3})
550 assert.NoError(t, err)
551
552 require.Len(t, aggs, wantCount)
553 }
554
555 func TestPipelineRegistryResource(t *testing.T) {
556 v := NewView(Instrument{Name: "bar"}, Stream{Name: "foo"})
557 readers := []Reader{NewManualReader()}
558 views := []View{defaultView, v}
559 res := resource.NewSchemaless(attribute.String("key", "val"))
560 pipes := newPipelines(res, readers, views, exemplar.AlwaysOffFilter, 0)
561 for _, p := range pipes {
562 assert.True(t, res.Equal(p.resource), "resource not set")
563 }
564 }
565
566 func TestPipelineRegistryCreateAggregatorsIncompatibleInstrument(t *testing.T) {
567 testRdrHistogram := NewManualReader(
568 WithAggregationSelector(func(InstrumentKind) Aggregation { return AggregationSum{} }),
569 )
570
571 readers := []Reader{testRdrHistogram}
572 views := []View{defaultView}
573 p := newPipelines(resource.Empty(), readers, views, exemplar.AlwaysOffFilter, 0)
574 inst := Instrument{Name: "foo", Kind: InstrumentKindObservableGauge}
575
576 var vc cache[string, instID]
577 ri := newResolver[int64](p, &vc)
578 intAggs, err := ri.Aggregators(inst)
579 assert.Error(t, err)
580 assert.Empty(t, intAggs)
581
582 rf := newResolver[float64](p, &vc)
583 floatAggs, err := rf.Aggregators(inst)
584 assert.Error(t, err)
585 assert.Empty(t, floatAggs)
586
587 intAggs, err = ri.HistogramAggregators(inst, []float64{1, 2, 3})
588 assert.Error(t, err)
589 assert.Empty(t, intAggs)
590
591 floatAggs, err = rf.HistogramAggregators(inst, []float64{1, 2, 3})
592 assert.Error(t, err)
593 assert.Empty(t, floatAggs)
594 }
595
596 type logCounter struct {
597 logr.LogSink
598
599 errN atomic.Uint32
600 infoN atomic.Uint32
601 }
602
603 func (l *logCounter) Info(level int, msg string, keysAndValues ...any) {
604 l.infoN.Add(1)
605 l.LogSink.Info(level, msg, keysAndValues...)
606 }
607
608 func (l *logCounter) InfoN() int {
609 return int(l.infoN.Swap(0))
610 }
611
612 func (l *logCounter) Error(err error, msg string, keysAndValues ...any) {
613 l.errN.Add(1)
614 l.LogSink.Error(err, msg, keysAndValues...)
615 }
616
617 func (l *logCounter) ErrorN() int {
618 return int(l.errN.Swap(0))
619 }
620
621 func TestResolveAggregatorsDuplicateErrors(t *testing.T) {
622 tLog := testr.NewWithOptions(t, testr.Options{Verbosity: 6})
623 l := &logCounter{LogSink: tLog.GetSink()}
624 otel.SetLogger(logr.New(l))
625
626 renameView := NewView(Instrument{Name: "bar"}, Stream{Name: "foo"})
627 readers := []Reader{NewManualReader()}
628 views := []View{defaultView, renameView}
629
630 fooInst := Instrument{Name: "foo", Kind: InstrumentKindCounter}
631 barInst := Instrument{Name: "bar", Kind: InstrumentKindCounter}
632
633 p := newPipelines(resource.Empty(), readers, views, exemplar.AlwaysOffFilter, 0)
634
635 var vc cache[string, instID]
636 ri := newResolver[int64](p, &vc)
637 intAggs, err := ri.Aggregators(fooInst)
638 assert.NoError(t, err)
639 assert.Equal(t, 0, l.InfoN(), "no info logging should happen")
640 assert.Len(t, intAggs, 1)
641
642 // The Rename view should produce the same instrument without an error, the
643 // default view should also cause a new aggregator to be returned.
644 intAggs, err = ri.Aggregators(barInst)
645 assert.NoError(t, err)
646 assert.Equal(t, 0, l.InfoN(), "no info logging should happen")
647 assert.Len(t, intAggs, 2)
648
649 // Creating a float foo instrument should log a warning because there is an
650 // int foo instrument.
651 rf := newResolver[float64](p, &vc)
652 floatAggs, err := rf.Aggregators(fooInst)
653 assert.NoError(t, err)
654 assert.Equal(t, 1, l.InfoN(), "instrument conflict not logged")
655 assert.Len(t, floatAggs, 1)
656
657 fooInst = Instrument{Name: "foo-float", Kind: InstrumentKindCounter}
658
659 floatAggs, err = rf.Aggregators(fooInst)
660 assert.NoError(t, err)
661 assert.Equal(t, 0, l.InfoN(), "no info logging should happen")
662 assert.Len(t, floatAggs, 1)
663
664 floatAggs, err = rf.Aggregators(barInst)
665 assert.NoError(t, err)
666 // Both the rename and default view aggregators created above should now
667 // conflict. Therefore, 2 warning messages should be logged.
668 assert.Equal(t, 2, l.InfoN(), "instrument conflicts not logged")
669 assert.Len(t, floatAggs, 2)
670 }
671
672 func TestIsAggregatorCompatible(t *testing.T) {
673 var undefinedInstrument InstrumentKind
674
675 testCases := []struct {
676 name string
677 kind InstrumentKind
678 agg Aggregation
679 want error
680 }{
681 {
682 name: "SyncCounter and Drop",
683 kind: InstrumentKindCounter,
684 agg: AggregationDrop{},
685 },
686 {
687 name: "SyncCounter and LastValue",
688 kind: InstrumentKindCounter,
689 agg: AggregationLastValue{},
690 want: errIncompatibleAggregation,
691 },
692 {
693 name: "SyncCounter and Sum",
694 kind: InstrumentKindCounter,
695 agg: AggregationSum{},
696 },
697 {
698 name: "SyncCounter and ExplicitBucketHistogram",
699 kind: InstrumentKindCounter,
700 agg: AggregationExplicitBucketHistogram{},
701 },
702 {
703 name: "SyncCounter and ExponentialHistogram",
704 kind: InstrumentKindCounter,
705 agg: AggregationBase2ExponentialHistogram{},
706 },
707 {
708 name: "SyncUpDownCounter and Drop",
709 kind: InstrumentKindUpDownCounter,
710 agg: AggregationDrop{},
711 },
712 {
713 name: "SyncUpDownCounter and LastValue",
714 kind: InstrumentKindUpDownCounter,
715 agg: AggregationLastValue{},
716 want: errIncompatibleAggregation,
717 },
718 {
719 name: "SyncUpDownCounter and Sum",
720 kind: InstrumentKindUpDownCounter,
721 agg: AggregationSum{},
722 },
723 {
724 name: "SyncUpDownCounter and ExplicitBucketHistogram",
725 kind: InstrumentKindUpDownCounter,
726 agg: AggregationExplicitBucketHistogram{},
727 },
728 {
729 name: "SyncUpDownCounter and ExponentialHistogram",
730 kind: InstrumentKindUpDownCounter,
731 agg: AggregationBase2ExponentialHistogram{},
732 },
733 {
734 name: "SyncHistogram and Drop",
735 kind: InstrumentKindHistogram,
736 agg: AggregationDrop{},
737 },
738 {
739 name: "SyncHistogram and LastValue",
740 kind: InstrumentKindHistogram,
741 agg: AggregationLastValue{},
742 want: errIncompatibleAggregation,
743 },
744 {
745 name: "SyncHistogram and Sum",
746 kind: InstrumentKindHistogram,
747 agg: AggregationSum{},
748 },
749 {
750 name: "SyncHistogram and ExplicitBucketHistogram",
751 kind: InstrumentKindHistogram,
752 agg: AggregationExplicitBucketHistogram{},
753 },
754 {
755 name: "SyncHistogram and ExponentialHistogram",
756 kind: InstrumentKindHistogram,
757 agg: AggregationBase2ExponentialHistogram{},
758 },
759 {
760 name: "SyncGauge and Drop",
761 kind: InstrumentKindGauge,
762 agg: AggregationDrop{},
763 },
764 {
765 name: "SyncGauge and LastValue",
766 kind: InstrumentKindGauge,
767 agg: AggregationLastValue{},
768 },
769 {
770 name: "SyncGauge and Sum",
771 kind: InstrumentKindGauge,
772 agg: AggregationSum{},
773 want: errIncompatibleAggregation,
774 },
775 {
776 name: "SyncGauge and ExplicitBucketHistogram",
777 kind: InstrumentKindGauge,
778 agg: AggregationExplicitBucketHistogram{},
779 },
780 {
781 name: "SyncGauge and ExponentialHistogram",
782 kind: InstrumentKindGauge,
783 agg: AggregationBase2ExponentialHistogram{},
784 },
785 {
786 name: "ObservableCounter and Drop",
787 kind: InstrumentKindObservableCounter,
788 agg: AggregationDrop{},
789 },
790 {
791 name: "ObservableCounter and LastValue",
792 kind: InstrumentKindObservableCounter,
793 agg: AggregationLastValue{},
794 want: errIncompatibleAggregation,
795 },
796 {
797 name: "ObservableCounter and Sum",
798 kind: InstrumentKindObservableCounter,
799 agg: AggregationSum{},
800 },
801 {
802 name: "ObservableCounter and ExplicitBucketHistogram",
803 kind: InstrumentKindObservableCounter,
804 agg: AggregationExplicitBucketHistogram{},
805 },
806 {
807 name: "ObservableCounter and ExponentialHistogram",
808 kind: InstrumentKindObservableCounter,
809 agg: AggregationBase2ExponentialHistogram{},
810 },
811 {
812 name: "ObservableUpDownCounter and Drop",
813 kind: InstrumentKindObservableUpDownCounter,
814 agg: AggregationDrop{},
815 },
816 {
817 name: "ObservableUpDownCounter and LastValue",
818 kind: InstrumentKindObservableUpDownCounter,
819 agg: AggregationLastValue{},
820 want: errIncompatibleAggregation,
821 },
822 {
823 name: "ObservableUpDownCounter and Sum",
824 kind: InstrumentKindObservableUpDownCounter,
825 agg: AggregationSum{},
826 },
827 {
828 name: "ObservableUpDownCounter and ExplicitBucketHistogram",
829 kind: InstrumentKindObservableUpDownCounter,
830 agg: AggregationExplicitBucketHistogram{},
831 },
832 {
833 name: "ObservableUpDownCounter and ExponentialHistogram",
834 kind: InstrumentKindObservableUpDownCounter,
835 agg: AggregationBase2ExponentialHistogram{},
836 },
837 {
838 name: "ObservableGauge and Drop",
839 kind: InstrumentKindObservableGauge,
840 agg: AggregationDrop{},
841 },
842 {
843 name: "ObservableGauge and LastValue{}",
844 kind: InstrumentKindObservableGauge,
845 agg: AggregationLastValue{},
846 },
847 {
848 name: "ObservableGauge and Sum",
849 kind: InstrumentKindObservableGauge,
850 agg: AggregationSum{},
851 want: errIncompatibleAggregation,
852 },
853 {
854 name: "ObservableGauge and ExplicitBucketHistogram",
855 kind: InstrumentKindObservableGauge,
856 agg: AggregationExplicitBucketHistogram{},
857 },
858 {
859 name: "ObservableGauge and ExponentialHistogram",
860 kind: InstrumentKindObservableGauge,
861 agg: AggregationBase2ExponentialHistogram{},
862 },
863 {
864 name: "unknown kind with Sum should error",
865 kind: undefinedInstrument,
866 agg: AggregationSum{},
867 want: errIncompatibleAggregation,
868 },
869 {
870 name: "unknown kind with LastValue should error",
871 kind: undefinedInstrument,
872 agg: AggregationLastValue{},
873 want: errIncompatibleAggregation,
874 },
875 {
876 name: "unknown kind with Histogram should error",
877 kind: undefinedInstrument,
878 agg: AggregationExplicitBucketHistogram{},
879 want: errIncompatibleAggregation,
880 },
881 {
882 name: "unknown kind with Histogram should error",
883 kind: undefinedInstrument,
884 agg: AggregationBase2ExponentialHistogram{},
885 want: errIncompatibleAggregation,
886 },
887 }
888
889 for _, tt := range testCases {
890 t.Run(tt.name, func(t *testing.T) {
891 err := isAggregatorCompatible(tt.kind, tt.agg)
892 assert.ErrorIs(t, err, tt.want)
893 })
894 }
895 }