-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
meter_test.go
337 lines (270 loc) · 10 KB
/
meter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package global // import "go.opentelemetry.io/otel/metric/internal/global"
import (
"context"
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/asyncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
)
func TestMeterProviderRace(t *testing.T) {
mp := &meterProvider{}
finish := make(chan struct{})
go func() {
for i := 0; ; i++ {
mp.Meter(fmt.Sprintf("a%d", i))
select {
case <-finish:
return
default:
}
}
}()
mp.setDelegate(metric.NewNoopMeterProvider())
close(finish)
}
func TestMeterRace(t *testing.T) {
mtr := &meter{}
wg := &sync.WaitGroup{}
wg.Add(1)
finish := make(chan struct{})
go func() {
for i, once := 0, false; ; i++ {
name := fmt.Sprintf("a%d", i)
_, _ = mtr.AsyncFloat64().Counter(name)
_, _ = mtr.AsyncFloat64().UpDownCounter(name)
_, _ = mtr.AsyncFloat64().Gauge(name)
_, _ = mtr.AsyncInt64().Counter(name)
_, _ = mtr.AsyncInt64().UpDownCounter(name)
_, _ = mtr.AsyncInt64().Gauge(name)
_, _ = mtr.SyncFloat64().Counter(name)
_, _ = mtr.SyncFloat64().UpDownCounter(name)
_, _ = mtr.SyncFloat64().Histogram(name)
_, _ = mtr.SyncInt64().Counter(name)
_, _ = mtr.SyncInt64().UpDownCounter(name)
_, _ = mtr.SyncInt64().Histogram(name)
_, _ = mtr.RegisterCallback(nil, func(ctx context.Context) {})
if !once {
wg.Done()
once = true
}
select {
case <-finish:
return
default:
}
}
}()
wg.Wait()
mtr.setDelegate(metric.NewNoopMeterProvider())
close(finish)
}
func TestUnregisterRace(t *testing.T) {
mtr := &meter{}
reg, err := mtr.RegisterCallback(nil, func(ctx context.Context) {})
require.NoError(t, err)
wg := &sync.WaitGroup{}
wg.Add(1)
finish := make(chan struct{})
go func() {
for i, once := 0, false; ; i++ {
_ = reg.Unregister()
if !once {
wg.Done()
once = true
}
select {
case <-finish:
return
default:
}
}
}()
_ = reg.Unregister()
wg.Wait()
mtr.setDelegate(metric.NewNoopMeterProvider())
close(finish)
}
func testSetupAllInstrumentTypes(t *testing.T, m metric.Meter) (syncfloat64.Counter, asyncfloat64.Counter) {
afcounter, err := m.AsyncFloat64().Counter("test_Async_Counter")
require.NoError(t, err)
_, err = m.AsyncFloat64().UpDownCounter("test_Async_UpDownCounter")
assert.NoError(t, err)
_, err = m.AsyncFloat64().Gauge("test_Async_Gauge")
assert.NoError(t, err)
_, err = m.AsyncInt64().Counter("test_Async_Counter")
assert.NoError(t, err)
_, err = m.AsyncInt64().UpDownCounter("test_Async_UpDownCounter")
assert.NoError(t, err)
_, err = m.AsyncInt64().Gauge("test_Async_Gauge")
assert.NoError(t, err)
_, err = m.RegisterCallback([]instrument.Asynchronous{afcounter}, func(ctx context.Context) {
afcounter.Observe(ctx, 3)
})
require.NoError(t, err)
sfcounter, err := m.SyncFloat64().Counter("test_Async_Counter")
require.NoError(t, err)
_, err = m.SyncFloat64().UpDownCounter("test_Async_UpDownCounter")
assert.NoError(t, err)
_, err = m.SyncFloat64().Histogram("test_Async_Histogram")
assert.NoError(t, err)
_, err = m.SyncInt64().Counter("test_Async_Counter")
assert.NoError(t, err)
_, err = m.SyncInt64().UpDownCounter("test_Async_UpDownCounter")
assert.NoError(t, err)
_, err = m.SyncInt64().Histogram("test_Async_Histogram")
assert.NoError(t, err)
return sfcounter, afcounter
}
// This is to emulate a read from an exporter.
func testCollect(t *testing.T, m metric.Meter) {
if tMeter, ok := m.(*meter); ok {
m, ok = tMeter.delegate.Load().(metric.Meter)
if !ok {
t.Error("meter was not delegated")
return
}
}
tMeter, ok := m.(*testMeter)
if !ok {
t.Error("collect called on non-test Meter")
return
}
tMeter.collect()
}
func TestMeterProviderDelegatesCalls(t *testing.T) {
// The global MeterProvider should directly call the underlying MeterProvider
// if it is set prior to Meter() being called.
// globalMeterProvider := otel.GetMeterProvider
globalMeterProvider := &meterProvider{}
mp := &testMeterProvider{}
// otel.SetMeterProvider(mp)
globalMeterProvider.setDelegate(mp)
assert.Equal(t, 0, mp.count)
meter := globalMeterProvider.Meter("go.opentelemetry.io/otel/metric/internal/global/meter_test")
ctr, actr := testSetupAllInstrumentTypes(t, meter)
ctr.Add(context.Background(), 5)
testCollect(t, meter) // This is a hacky way to emulate a read from an exporter
// Calls to Meter() after setDelegate() should be executed by the delegate
require.IsType(t, &testMeter{}, meter)
tMeter := meter.(*testMeter)
assert.Equal(t, 3, tMeter.afCount)
assert.Equal(t, 3, tMeter.aiCount)
assert.Equal(t, 3, tMeter.sfCount)
assert.Equal(t, 3, tMeter.siCount)
assert.Equal(t, 1, len(tMeter.callbacks))
// Because the Meter was provided by testmeterProvider it should also return our test instrument
require.IsType(t, &testCountingFloatInstrument{}, ctr, "the meter did not delegate calls to the meter")
assert.Equal(t, 1, ctr.(*testCountingFloatInstrument).count)
require.IsType(t, &testCountingFloatInstrument{}, actr, "the meter did not delegate calls to the meter")
assert.Equal(t, 1, actr.(*testCountingFloatInstrument).count)
assert.Equal(t, 1, mp.count)
}
func TestMeterDelegatesCalls(t *testing.T) {
// The global MeterProvider should directly provide a Meter instance that
// can be updated. If the SetMeterProvider is called after a Meter was
// obtained, but before instruments only the instrument should be generated
// by the delegated type.
globalMeterProvider := &meterProvider{}
mp := &testMeterProvider{}
assert.Equal(t, 0, mp.count)
m := globalMeterProvider.Meter("go.opentelemetry.io/otel/metric/internal/global/meter_test")
globalMeterProvider.setDelegate(mp)
ctr, actr := testSetupAllInstrumentTypes(t, m)
ctr.Add(context.Background(), 5)
testCollect(t, m) // This is a hacky way to emulate a read from an exporter
// Calls to Meter methods after setDelegate() should be executed by the delegate
require.IsType(t, &meter{}, m)
tMeter := m.(*meter).delegate.Load().(*testMeter)
require.NotNil(t, tMeter)
assert.Equal(t, 3, tMeter.afCount)
assert.Equal(t, 3, tMeter.aiCount)
assert.Equal(t, 3, tMeter.sfCount)
assert.Equal(t, 3, tMeter.siCount)
// Because the Meter was provided by testmeterProvider it should also return our test instrument
require.IsType(t, &testCountingFloatInstrument{}, ctr, "the meter did not delegate calls to the meter")
assert.Equal(t, 1, ctr.(*testCountingFloatInstrument).count)
// Because the Meter was provided by testmeterProvider it should also return our test instrument
require.IsType(t, &testCountingFloatInstrument{}, actr, "the meter did not delegate calls to the meter")
assert.Equal(t, 1, actr.(*testCountingFloatInstrument).count)
assert.Equal(t, 1, mp.count)
}
func TestMeterDefersDelegations(t *testing.T) {
// If SetMeterProvider is called after instruments are registered, the
// instruments should be recreated with the new meter.
// globalMeterProvider := otel.GetMeterProvider
globalMeterProvider := &meterProvider{}
m := globalMeterProvider.Meter("go.opentelemetry.io/otel/metric/internal/global/meter_test")
ctr, actr := testSetupAllInstrumentTypes(t, m)
ctr.Add(context.Background(), 5)
mp := &testMeterProvider{}
// otel.SetMeterProvider(mp)
globalMeterProvider.setDelegate(mp)
testCollect(t, m) // This is a hacky way to emulate a read from an exporter
// Calls to Meter() before setDelegate() should be the delegated type
require.IsType(t, &meter{}, m)
tMeter := m.(*meter).delegate.Load().(*testMeter)
require.NotNil(t, tMeter)
assert.Equal(t, 3, tMeter.afCount)
assert.Equal(t, 3, tMeter.aiCount)
assert.Equal(t, 3, tMeter.sfCount)
assert.Equal(t, 3, tMeter.siCount)
// Because the Meter was a delegate it should return a delegated instrument
assert.IsType(t, &sfCounter{}, ctr)
assert.IsType(t, &afCounter{}, actr)
assert.Equal(t, 1, mp.count)
}
func TestRegistrationDelegation(t *testing.T) {
// globalMeterProvider := otel.GetMeterProvider
globalMeterProvider := &meterProvider{}
m := globalMeterProvider.Meter("go.opentelemetry.io/otel/metric/internal/global/meter_test")
require.IsType(t, &meter{}, m)
mImpl := m.(*meter)
actr, err := m.AsyncFloat64().Counter("test_Async_Counter")
require.NoError(t, err)
var called0 bool
reg0, err := m.RegisterCallback([]instrument.Asynchronous{actr}, func(context.Context) {
called0 = true
})
require.NoError(t, err)
require.Equal(t, 1, mImpl.registry.Len(), "callback not registered")
// This means reg0 should not be delegated.
assert.NoError(t, reg0.Unregister())
assert.Equal(t, 0, mImpl.registry.Len(), "callback not unregistered")
var called1 bool
reg1, err := m.RegisterCallback([]instrument.Asynchronous{actr}, func(context.Context) {
called1 = true
})
require.NoError(t, err)
require.Equal(t, 1, mImpl.registry.Len(), "second callback not registered")
mp := &testMeterProvider{}
// otel.SetMeterProvider(mp)
globalMeterProvider.setDelegate(mp)
testCollect(t, m) // This is a hacky way to emulate a read from an exporter
require.False(t, called0, "pre-delegation unregistered callback called")
require.True(t, called1, "callback not called")
called1 = false
assert.NoError(t, reg1.Unregister(), "unregister second callback")
testCollect(t, m) // This is a hacky way to emulate a read from an exporter
assert.False(t, called1, "unregistered callback called")
assert.NotPanics(t, func() {
assert.NoError(t, reg1.Unregister(), "duplicate unregister calls")
})
}