Debian Codegraph @

sdk/metric/view_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 "testing"
8
9 "github.com/go-logr/logr"
10 "github.com/go-logr/logr/funcr"
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/instrumentation"
18 )
19
20 var (
21 schemaURL = "https://opentelemetry.io/schemas/1.37.0"
22 completeIP = Instrument{
23 Name: "foo",
24 Description: "foo desc",
25 Kind: InstrumentKindCounter,
26 Unit: "By",
27 Scope: instrumentation.Scope{
28 Name: "TestNewViewMatch",
29 Version: "v0.1.0",
30 SchemaURL: schemaURL,
31 },
32 }
33 )
34
35 func scope(name, ver, url string) instrumentation.Scope {
36 return instrumentation.Scope{Name: name, Version: ver, SchemaURL: url}
37 }
38
39 func testNewViewMatchName() func(t *testing.T) {
40 tests := []struct {
41 name string
42 criteria string
43 match []string
44 notMatch []string
45 }{
46 {
47 name: "Exact",
48 criteria: "foo",
49 match: []string{"foo"},
50 notMatch: []string{"", "bar", "foobar", "barfoo", "ffooo"},
51 },
52 {
53 name: "Wildcard/*",
54 criteria: "*",
55 match: []string{"", "foo", "foobar", "barfoo", "barfoobaz"},
56 },
57 {
58 name: "Wildcard/Front?",
59 criteria: "?oo",
60 match: []string{"foo", "1oo"},
61 notMatch: []string{"", "bar", "foobar", "barfoo", "barfoobaz"},
62 },
63 {
64 name: "Wildcard/Back?",
65 criteria: "fo?",
66 match: []string{"foo", "fo1"},
67 notMatch: []string{"", "bar", "foobar", "barfoo", "barfoobaz"},
68 },
69 {
70 name: "Wildcard/Front*",
71 criteria: "*foo",
72 match: []string{"foo", "123foo", "barfoo"},
73 notMatch: []string{"", "bar", "foobar", "barfoobaz"},
74 },
75 {
76 name: "Wildcard/Back*",
77 criteria: "foo*",
78 match: []string{"foo", "foo1", "foobar"},
79 notMatch: []string{"", "bar", "barfoo", "barfoobaz"},
80 },
81 {
82 name: "Wildcard/FrontBack*",
83 criteria: "*foo*",
84 match: []string{"foo", "foo1", "1foo", "1foo1", "foobar", "barfoobaz"},
85 notMatch: []string{"", "bar"},
86 },
87 {
88 name: "Wildcard/Front**",
89 criteria: "**foo",
90 match: []string{"foo", "123foo", "barfoo", "afoo"},
91 notMatch: []string{"", "bar", "foobar", "barfoobaz"},
92 },
93 {
94 name: "Wildcard/Back**",
95 criteria: "foo**",
96 match: []string{"foo", "foo1", "fooa", "foobar"},
97 notMatch: []string{"", "bar", "barfoo", "barfoobaz"},
98 },
99 {
100 name: "Wildcard/Front*?",
101 criteria: "*?oo",
102 match: []string{"foo", "123foo", "barfoo", "afoo"},
103 notMatch: []string{"", "fo", "bar", "foobar", "barfoobaz"},
104 },
105 {
106 name: "Wildcard/Back*?",
107 criteria: "fo*?",
108 match: []string{"foo", "foo1", "fooa", "foobar"},
109 notMatch: []string{"", "bar", "barfoo", "barfoobaz"},
110 },
111 {
112 name: "Wildcard/Front?*",
113 criteria: "?*oo",
114 match: []string{"foo", "123foo", "barfoo", "afoo"},
115 notMatch: []string{"", "oo", "fo", "bar", "foobar", "barfoobaz"},
116 },
117 {
118 name: "Wildcard/Back?*",
119 criteria: "fo?*",
120 match: []string{"foo", "foo1", "fooa", "foobar"},
121 notMatch: []string{"", "fo", "bar", "barfoo", "barfoobaz"},
122 },
123 {
124 name: "Wildcard/Middle*",
125 criteria: "f*o",
126 match: []string{"fo", "foo", "fooo", "fo12baro"},
127 notMatch: []string{"", "bar", "barfoo", "barfoobaz"},
128 },
129 {
130 name: "Wildcard/Middle?",
131 criteria: "f?o",
132 match: []string{"foo", "f1o"},
133 notMatch: []string{"", "fo", "fooo", "fo12baro", "bar"},
134 },
135 {
136 name: "Wildcard/MetaCharacters",
137 criteria: "*.+()|[]{}^$-_?",
138 match: []string{"aa.+()|[]{}^$-_b", ".+()|[]{}^$-_b"},
139 notMatch: []string{"", "foo", ".+()|[]{}^$-_"},
140 },
141 }
142
143 return func(t *testing.T) {
144 for _, test := range tests {
145 v := NewView(Instrument{Name: test.criteria}, Stream{})
146 t.Run(test.name, func(t *testing.T) {
147 for _, n := range test.match {
148 _, matches := v(Instrument{Name: n})
149 assert.Truef(t, matches, "%s does not match %s", test.criteria, n)
150 }
151 for _, n := range test.notMatch {
152 _, matches := v(Instrument{Name: n})
153 assert.Falsef(t, matches, "%s matches %s", test.criteria, n)
154 }
155 })
156 }
157 }
158 }
159
160 func TestNewViewMatch(t *testing.T) {
161 // Avoid boilerplate for name match testing.
162 t.Run("Name", testNewViewMatchName())
163
164 tests := []struct {
165 name string
166 criteria Instrument
167 matches []Instrument
168 notMatches []Instrument
169 }{
170 {
171 name: "Empty",
172 notMatches: []Instrument{{}, {Name: "foo"}, completeIP},
173 },
174 {
175 name: "Description",
176 criteria: Instrument{Description: "foo desc"},
177 matches: []Instrument{{Description: "foo desc"}, completeIP},
178 notMatches: []Instrument{{}, {Description: "foo"}, {Description: "desc"}},
179 },
180 {
181 name: "Kind",
182 criteria: Instrument{Kind: InstrumentKindCounter},
183 matches: []Instrument{{Kind: InstrumentKindCounter}, completeIP},
184 notMatches: []Instrument{
185 {},
186 {Kind: InstrumentKindUpDownCounter},
187 {Kind: InstrumentKindHistogram},
188 {Kind: InstrumentKindGauge},
189 {Kind: InstrumentKindObservableCounter},
190 {Kind: InstrumentKindObservableUpDownCounter},
191 {Kind: InstrumentKindObservableGauge},
192 },
193 },
194 {
195 name: "Unit",
196 criteria: Instrument{Unit: "By"},
197 matches: []Instrument{{Unit: "By"}, completeIP},
198 notMatches: []Instrument{
199 {},
200 {Unit: "1"},
201 {Unit: "K"},
202 },
203 },
204 {
205 name: "ScopeName",
206 criteria: Instrument{Scope: scope("TestNewViewMatch", "", "")},
207 matches: []Instrument{
208 {Scope: scope("TestNewViewMatch", "", "")},
209 completeIP,
210 },
211 notMatches: []Instrument{
212 {},
213 {Scope: scope("PrefixTestNewViewMatch", "", "")},
214 {Scope: scope("TestNewViewMatchSuffix", "", "")},
215 {Scope: scope("alt", "", "")},
216 },
217 },
218 {
219 name: "ScopeVersion",
220 criteria: Instrument{Scope: scope("", "v0.1.0", "")},
221 matches: []Instrument{
222 {Scope: scope("", "v0.1.0", "")},
223 completeIP,
224 },
225 notMatches: []Instrument{
226 {},
227 {Scope: scope("", "v0.1.0-RC1", "")},
228 {Scope: scope("", "v0.1.1", "")},
229 },
230 },
231 {
232 name: "ScopeSchemaURL",
233 criteria: Instrument{Scope: scope("", "", schemaURL)},
234 matches: []Instrument{
235 {Scope: scope("", "", schemaURL)},
236 completeIP,
237 },
238 notMatches: []Instrument{
239 {},
240 {Scope: scope("", "", schemaURL+"/path")},
241 {Scope: scope("", "", "https://go.dev")},
242 },
243 },
244 {
245 name: "Scope",
246 criteria: Instrument{Scope: scope("TestNewViewMatch", "v0.1.0", schemaURL)},
247 matches: []Instrument{
248 {Scope: scope("TestNewViewMatch", "v0.1.0", schemaURL)},
249 completeIP,
250 },
251 notMatches: []Instrument{
252 {},
253 {Scope: scope("CompleteMisMatch", "v0.2.0", "https://go.dev")},
254 {Scope: scope("NameMisMatch", "v0.1.0", schemaURL)},
255 },
256 },
257 {
258 name: "Complete",
259 criteria: completeIP,
260 matches: []Instrument{completeIP},
261 notMatches: []Instrument{
262 {},
263 {Name: "foo"},
264 {
265 Name: "Wrong Name",
266 Description: "foo desc",
267 Kind: InstrumentKindCounter,
268 Unit: "By",
269 Scope: scope("TestNewViewMatch", "v0.1.0", schemaURL),
270 },
271 {
272 Name: "foo",
273 Description: "Wrong Description",
274 Kind: InstrumentKindCounter,
275 Unit: "By",
276 Scope: scope("TestNewViewMatch", "v0.1.0", schemaURL),
277 },
278 {
279 Name: "foo",
280 Description: "foo desc",
281 Kind: InstrumentKindObservableUpDownCounter,
282 Unit: "By",
283 Scope: scope("TestNewViewMatch", "v0.1.0", schemaURL),
284 },
285 {
286 Name: "foo",
287 Description: "foo desc",
288 Kind: InstrumentKindCounter,
289 Unit: "1",
290 Scope: scope("TestNewViewMatch", "v0.1.0", schemaURL),
291 },
292 {
293 Name: "foo",
294 Description: "foo desc",
295 Kind: InstrumentKindCounter,
296 Unit: "By",
297 Scope: scope("Wrong Scope Name", "v0.1.0", schemaURL),
298 },
299 {
300 Name: "foo",
301 Description: "foo desc",
302 Kind: InstrumentKindCounter,
303 Unit: "By",
304 Scope: scope("TestNewViewMatch", "v1.4.3", schemaURL),
305 },
306 {
307 Name: "foo",
308 Description: "foo desc",
309 Kind: InstrumentKindCounter,
310 Unit: "By",
311 Scope: scope("TestNewViewMatch", "v0.1.0", "https://go.dev"),
312 },
313 },
314 },
315 }
316
317 for _, test := range tests {
318 v := NewView(test.criteria, Stream{})
319 t.Run(test.name, func(t *testing.T) {
320 for _, instrument := range test.matches {
321 _, matches := v(instrument)
322 assert.Truef(t, matches, "view does not match %#v", instrument)
323 }
324
325 for _, instrument := range test.notMatches {
326 _, matches := v(instrument)
327 assert.Falsef(t, matches, "view matches %#v", instrument)
328 }
329 })
330 }
331 }
332
333 func TestNewViewReplace(t *testing.T) {
334 alt := "alternative value"
335 tests := []struct {
336 name string
337 mask Stream
338 want func(Instrument) Stream
339 }{
340 {
341 name: "Nothing",
342 want: func(i Instrument) Stream {
343 return Stream{
344 Name: i.Name,
345 Description: i.Description,
346 Unit: i.Unit,
347 }
348 },
349 },
350 {
351 name: "Name",
352 mask: Stream{Name: alt},
353 want: func(i Instrument) Stream {
354 return Stream{
355 Name: alt,
356 Description: i.Description,
357 Unit: i.Unit,
358 }
359 },
360 },
361 {
362 name: "Description",
363 mask: Stream{Description: alt},
364 want: func(i Instrument) Stream {
365 return Stream{
366 Name: i.Name,
367 Description: alt,
368 Unit: i.Unit,
369 }
370 },
371 },
372 {
373 name: "Unit",
374 mask: Stream{Unit: "1"},
375 want: func(i Instrument) Stream {
376 return Stream{
377 Name: i.Name,
378 Description: i.Description,
379 Unit: "1",
380 }
381 },
382 },
383 {
384 name: "Aggregation",
385 mask: Stream{Aggregation: AggregationLastValue{}},
386 want: func(i Instrument) Stream {
387 return Stream{
388 Name: i.Name,
389 Description: i.Description,
390 Unit: i.Unit,
391 Aggregation: AggregationLastValue{},
392 }
393 },
394 },
395 {
396 name: "Complete",
397 mask: Stream{
398 Name: alt,
399 Description: alt,
400 Unit: "1",
401 Aggregation: AggregationLastValue{},
402 },
403 want: func(Instrument) Stream {
404 return Stream{
405 Name: alt,
406 Description: alt,
407 Unit: "1",
408 Aggregation: AggregationLastValue{},
409 }
410 },
411 },
412 }
413
414 for _, test := range tests {
415 t.Run(test.name, func(t *testing.T) {
416 got, match := NewView(completeIP, test.mask)(completeIP)
417 require.True(t, match, "view did not match exact criteria")
418 assert.Equal(t, test.want(completeIP), got)
419 })
420 }
421
422 // Go does not allow for the comparison of function values, even their
423 // addresses. Therefore, the AttributeFilter field needs an alternative
424 // testing strategy.
425 t.Run("AttributeFilter", func(t *testing.T) {
426 allowed := attribute.String("key", "val")
427 filter := func(kv attribute.KeyValue) bool {
428 return kv == allowed
429 }
430 mask := Stream{AttributeFilter: filter}
431 got, match := NewView(completeIP, mask)(completeIP)
432 require.True(t, match, "view did not match exact criteria")
433 require.NotNil(t, got.AttributeFilter, "AttributeFilter not set")
434 assert.True(t, got.AttributeFilter(allowed), "wrong AttributeFilter")
435 other := attribute.String("key", "other val")
436 assert.False(t, got.AttributeFilter(other), "wrong AttributeFilter")
437 })
438 }
439
440 type badAgg struct {
441 e error
442 }
443
444 func (a badAgg) copy() Aggregation { return a }
445
446 func (a badAgg) err() error { return a.e }
447
448 func TestNewViewAggregationErrorLogged(t *testing.T) {
449 tLog := testr.NewWithOptions(t, testr.Options{Verbosity: 6})
450 l := &logCounter{LogSink: tLog.GetSink()}
451 otel.SetLogger(logr.New(l))
452
453 agg := badAgg{e: assert.AnError}
454 mask := Stream{Aggregation: agg}
455 got, match := NewView(completeIP, mask)(completeIP)
456 require.True(t, match, "view did not match exact criteria")
457 assert.Nil(t, got.Aggregation, "erroring aggregation used")
458 assert.Equal(t, 1, l.ErrorN())
459 }
460
461 func TestNewViewEmptyViewErrorLogged(t *testing.T) {
462 var got string
463 otel.SetLogger(funcr.New(func(_, args string) {
464 got = args
465 }, funcr.Options{Verbosity: 6}))
466
467 _ = NewView(Instrument{}, Stream{})
468 assert.Contains(t, got, errEmptyView.Error())
469 }
470
471 func TestNewViewMultiInstMatchErrorLogged(t *testing.T) {
472 var got string
473 otel.SetLogger(funcr.New(func(_, args string) {
474 got = args
475 }, funcr.Options{Verbosity: 6}))
476
477 _ = NewView(Instrument{
478 Name: "*", // Wildcard match name (multiple instruments).
479 }, Stream{
480 Name: "non-empty",
481 })
482 assert.Contains(t, got, errMultiInst.Error())
483 }