Debian Codegraph @

internal/global/instruments_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package global
5
6 import (
7 "context"
8 "testing"
9
10 "go.opentelemetry.io/otel/metric"
11 "go.opentelemetry.io/otel/metric/embedded"
12 "go.opentelemetry.io/otel/metric/noop"
13 )
14
15 func testFloat64ConcurrentSafe(interact func(float64), setDelegate func(metric.Meter)) {
16 done := make(chan struct{})
17 finish := make(chan struct{})
18 go func() {
19 defer close(done)
20 for {
21 interact(1)
22 select {
23 case <-finish:
24 return
25 default:
26 }
27 }
28 }()
29
30 setDelegate(noop.NewMeterProvider().Meter(""))
31 close(finish)
32 <-done
33 }
34
35 func testInt64ConcurrentSafe(interact func(int64), setDelegate func(metric.Meter)) {
36 done := make(chan struct{})
37 finish := make(chan struct{})
38 go func() {
39 defer close(done)
40 for {
41 interact(1)
42 select {
43 case <-finish:
44 return
45 default:
46 }
47 }
48 }()
49
50 setDelegate(noop.NewMeterProvider().Meter(""))
51 close(finish)
52 <-done
53 }
54
55 func TestAsyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
56 // Float64 Instruments
57 t.Run("Float64", func(t *testing.T) {
58 t.Run("Counter", func(*testing.T) {
59 delegate := &afCounter{}
60 f := func(float64) { _ = delegate.unwrap() }
61 testFloat64ConcurrentSafe(f, delegate.setDelegate)
62 })
63
64 t.Run("UpDownCounter", func(*testing.T) {
65 delegate := &afUpDownCounter{}
66 f := func(float64) { _ = delegate.unwrap() }
67 testFloat64ConcurrentSafe(f, delegate.setDelegate)
68 })
69
70 t.Run("Gauge", func(*testing.T) {
71 delegate := &afGauge{}
72 f := func(float64) { _ = delegate.unwrap() }
73 testFloat64ConcurrentSafe(f, delegate.setDelegate)
74 })
75 })
76
77 // Int64 Instruments
78
79 t.Run("Int64", func(t *testing.T) {
80 t.Run("Counter", func(*testing.T) {
81 delegate := &aiCounter{}
82 f := func(int64) { _ = delegate.unwrap() }
83 testInt64ConcurrentSafe(f, delegate.setDelegate)
84 })
85
86 t.Run("UpDownCounter", func(*testing.T) {
87 delegate := &aiUpDownCounter{}
88 f := func(int64) { _ = delegate.unwrap() }
89 testInt64ConcurrentSafe(f, delegate.setDelegate)
90 })
91
92 t.Run("Gauge", func(*testing.T) {
93 delegate := &aiGauge{}
94 f := func(int64) { _ = delegate.unwrap() }
95 testInt64ConcurrentSafe(f, delegate.setDelegate)
96 })
97 })
98 }
99
100 func TestSyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
101 // Float64 Instruments
102 t.Run("Float64", func(*testing.T) {
103 t.Run("Counter", func(*testing.T) {
104 delegate := &sfCounter{}
105 f := func(v float64) { delegate.Add(t.Context(), v) }
106 testFloat64ConcurrentSafe(f, delegate.setDelegate)
107 })
108
109 t.Run("UpDownCounter", func(*testing.T) {
110 delegate := &sfUpDownCounter{}
111 f := func(v float64) { delegate.Add(t.Context(), v) }
112 testFloat64ConcurrentSafe(f, delegate.setDelegate)
113 })
114
115 t.Run("Histogram", func(*testing.T) {
116 delegate := &sfHistogram{}
117 f := func(v float64) { delegate.Record(t.Context(), v) }
118 testFloat64ConcurrentSafe(f, delegate.setDelegate)
119 })
120
121 t.Run("Gauge", func(*testing.T) {
122 delegate := &sfGauge{}
123 f := func(v float64) { delegate.Record(t.Context(), v) }
124 testFloat64ConcurrentSafe(f, delegate.setDelegate)
125 })
126 })
127
128 // Int64 Instruments
129
130 t.Run("Int64", func(*testing.T) {
131 t.Run("Counter", func(*testing.T) {
132 delegate := &siCounter{}
133 f := func(v int64) { delegate.Add(t.Context(), v) }
134 testInt64ConcurrentSafe(f, delegate.setDelegate)
135 })
136
137 t.Run("UpDownCounter", func(*testing.T) {
138 delegate := &siUpDownCounter{}
139 f := func(v int64) { delegate.Add(t.Context(), v) }
140 testInt64ConcurrentSafe(f, delegate.setDelegate)
141 })
142
143 t.Run("Histogram", func(*testing.T) {
144 delegate := &siHistogram{}
145 f := func(v int64) { delegate.Record(t.Context(), v) }
146 testInt64ConcurrentSafe(f, delegate.setDelegate)
147 })
148
149 t.Run("Gauge", func(*testing.T) {
150 delegate := &siGauge{}
151 f := func(v int64) { delegate.Record(t.Context(), v) }
152 testInt64ConcurrentSafe(f, delegate.setDelegate)
153 })
154 })
155 }
156
157 type testCountingFloatInstrument struct {
158 count int
159
160 metric.Float64Observable
161 embedded.Float64Counter
162 embedded.Float64UpDownCounter
163 embedded.Float64Histogram
164 embedded.Float64Gauge
165 embedded.Float64ObservableCounter
166 embedded.Float64ObservableUpDownCounter
167 embedded.Float64ObservableGauge
168 }
169
170 func (i *testCountingFloatInstrument) observe() {
171 i.count++
172 }
173
174 func (i *testCountingFloatInstrument) Add(context.Context, float64, ...metric.AddOption) {
175 i.count++
176 }
177
178 func (i *testCountingFloatInstrument) Record(context.Context, float64, ...metric.RecordOption) {
179 i.count++
180 }
181
182 func (*testCountingFloatInstrument) Enabled(context.Context) bool {
183 return true
184 }
185
186 type testCountingIntInstrument struct {
187 count int
188
189 metric.Int64Observable
190 embedded.Int64Counter
191 embedded.Int64UpDownCounter
192 embedded.Int64Histogram
193 embedded.Int64Gauge
194 embedded.Int64ObservableCounter
195 embedded.Int64ObservableUpDownCounter
196 embedded.Int64ObservableGauge
197 }
198
199 func (i *testCountingIntInstrument) observe() {
200 i.count++
201 }
202
203 func (i *testCountingIntInstrument) Add(context.Context, int64, ...metric.AddOption) {
204 i.count++
205 }
206
207 func (i *testCountingIntInstrument) Record(context.Context, int64, ...metric.RecordOption) {
208 i.count++
209 }
210
211 func (*testCountingIntInstrument) Enabled(context.Context) bool {
212 return true
213 }