Debian Codegraph @

metric/instrument_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package metric // import "go.opentelemetry.io/otel/metric"
5
6 import (
7 "sync"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11
12 "go.opentelemetry.io/otel/attribute"
13 )
14
15 type attrConf interface {
16 Attributes() attribute.Set
17 }
18
19 func TestConfigAttrs(t *testing.T) {
20 t.Run("AddConfig", testConfAttr(func(mo ...MeasurementOption) attrConf {
21 opts := make([]AddOption, len(mo))
22 for i := range mo {
23 opts[i] = mo[i].(AddOption)
24 }
25 return NewAddConfig(opts)
26 }))
27
28 t.Run("RecordConfig", testConfAttr(func(mo ...MeasurementOption) attrConf {
29 opts := make([]RecordOption, len(mo))
30 for i := range mo {
31 opts[i] = mo[i].(RecordOption)
32 }
33 return NewRecordConfig(opts)
34 }))
35
36 t.Run("ObserveConfig", testConfAttr(func(mo ...MeasurementOption) attrConf {
37 opts := make([]ObserveOption, len(mo))
38 for i := range mo {
39 opts[i] = mo[i].(ObserveOption)
40 }
41 return NewObserveConfig(opts)
42 }))
43 }
44
45 func testConfAttr(newConf func(...MeasurementOption) attrConf) func(t *testing.T) {
46 return func(t *testing.T) {
47 t.Run("ZeroConfigEmpty", func(t *testing.T) {
48 c := newConf()
49 assert.Equal(t, *attribute.EmptySet(), c.Attributes())
50 })
51
52 t.Run("EmptySet", func(t *testing.T) {
53 c := newConf(WithAttributeSet(*attribute.EmptySet()))
54 assert.Equal(t, *attribute.EmptySet(), c.Attributes())
55 })
56
57 aliceAttr := attribute.String("user", "Alice")
58 alice := attribute.NewSet(aliceAttr)
59 t.Run("SingleWithAttributeSet", func(t *testing.T) {
60 c := newConf(WithAttributeSet(alice))
61 assert.Equal(t, alice, c.Attributes())
62 })
63
64 t.Run("SingleWithAttributes", func(t *testing.T) {
65 c := newConf(WithAttributes(aliceAttr))
66 assert.Equal(t, alice, c.Attributes())
67 })
68
69 bobAttr := attribute.String("user", "Bob")
70 bob := attribute.NewSet(bobAttr)
71 t.Run("MultiWithAttributeSet", func(t *testing.T) {
72 c := newConf(WithAttributeSet(alice), WithAttributeSet(bob))
73 assert.Equal(t, bob, c.Attributes())
74 })
75
76 t.Run("MergedWithAttributes", func(t *testing.T) {
77 c := newConf(WithAttributes(aliceAttr, bobAttr))
78 assert.Equal(t, bob, c.Attributes())
79 })
80
81 t.Run("MultiWithAttributeSet", func(t *testing.T) {
82 c := newConf(WithAttributes(aliceAttr), WithAttributes(bobAttr))
83 assert.Equal(t, bob, c.Attributes())
84 })
85
86 t.Run("MergedEmpty", func(t *testing.T) {
87 c := newConf(WithAttributeSet(alice), WithAttributeSet(*attribute.EmptySet()))
88 assert.Equal(t, alice, c.Attributes())
89 })
90 }
91 }
92
93 func TestWithAttributesConcurrentSafe(*testing.T) {
94 attrs := []attribute.KeyValue{
95 attribute.String("user", "Alice"),
96 attribute.Bool("admin", true),
97 attribute.String("user", "Bob"),
98 }
99
100 var wg sync.WaitGroup
101 wg.Go(func() {
102 opt := []AddOption{WithAttributes(attrs...)}
103 _ = NewAddConfig(opt)
104 })
105 wg.Go(func() {
106 opt := []AddOption{WithAttributes(attrs...)}
107 _ = NewAddConfig(opt)
108 })
109
110 wg.Go(func() {
111 opt := []RecordOption{WithAttributes(attrs...)}
112 _ = NewRecordConfig(opt)
113 })
114 wg.Go(func() {
115 opt := []RecordOption{WithAttributes(attrs...)}
116 _ = NewRecordConfig(opt)
117 })
118
119 wg.Go(func() {
120 opt := []ObserveOption{WithAttributes(attrs...)}
121 _ = NewObserveConfig(opt)
122 })
123 wg.Go(func() {
124 opt := []ObserveOption{WithAttributes(attrs...)}
125 _ = NewObserveConfig(opt)
126 })
127
128 wg.Wait()
129 }