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