1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00
Files
opentelemetry-go/internal/global/instruments_test.go
T

214 lines
5.0 KiB
Go
Raw Normal View History

2022-03-22 10:33:13 -05:00
// Copyright The OpenTelemetry Authors
2024-02-29 07:05:28 +01:00
// SPDX-License-Identifier: Apache-2.0
2022-03-22 10:33:13 -05:00
package global
import (
"context"
"testing"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/embedded"
2023-03-29 08:05:07 -07:00
"go.opentelemetry.io/otel/metric/noop"
2022-03-22 10:33:13 -05:00
)
func testFloat64ConcurrentSafe(interact func(float64), setDelegate func(metric.Meter)) {
done := make(chan struct{})
2022-03-22 10:33:13 -05:00
finish := make(chan struct{})
go func() {
defer close(done)
2022-03-22 10:33:13 -05:00
for {
2023-04-18 07:16:06 -07:00
interact(1)
2022-03-22 10:33:13 -05:00
select {
case <-finish:
return
default:
}
}
}()
2023-03-29 08:05:07 -07:00
setDelegate(noop.NewMeterProvider().Meter(""))
2022-03-22 10:33:13 -05:00
close(finish)
<-done
2022-03-22 10:33:13 -05:00
}
func testInt64ConcurrentSafe(interact func(int64), setDelegate func(metric.Meter)) {
done := make(chan struct{})
2022-03-22 10:33:13 -05:00
finish := make(chan struct{})
go func() {
defer close(done)
2022-03-22 10:33:13 -05:00
for {
2023-04-18 07:16:06 -07:00
interact(1)
2022-03-22 10:33:13 -05:00
select {
case <-finish:
return
default:
}
}
}()
2023-03-29 08:05:07 -07:00
setDelegate(noop.NewMeterProvider().Meter(""))
2022-03-22 10:33:13 -05:00
close(finish)
<-done
2022-03-22 10:33:13 -05:00
}
func TestAsyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
2022-03-22 10:33:13 -05:00
// Float64 Instruments
t.Run("Float64", func(t *testing.T) {
t.Run("Counter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &afCounter{}
f := func(float64) { _ = delegate.unwrap() }
testFloat64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("UpDownCounter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &afUpDownCounter{}
f := func(float64) { _ = delegate.unwrap() }
testFloat64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("Gauge", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &afGauge{}
f := func(float64) { _ = delegate.unwrap() }
testFloat64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
})
// Int64 Instruments
t.Run("Int64", func(t *testing.T) {
t.Run("Counter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &aiCounter{}
f := func(int64) { _ = delegate.unwrap() }
testInt64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("UpDownCounter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &aiUpDownCounter{}
f := func(int64) { _ = delegate.unwrap() }
testInt64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("Gauge", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &aiGauge{}
f := func(int64) { _ = delegate.unwrap() }
testInt64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
})
}
func TestSyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
2022-03-22 10:33:13 -05:00
// Float64 Instruments
t.Run("Float64", func(*testing.T) {
t.Run("Counter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &sfCounter{}
f := func(v float64) { delegate.Add(t.Context(), v) }
testFloat64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("UpDownCounter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &sfUpDownCounter{}
f := func(v float64) { delegate.Add(t.Context(), v) }
testFloat64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("Histogram", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &sfHistogram{}
f := func(v float64) { delegate.Record(t.Context(), v) }
testFloat64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("Gauge", func(*testing.T) {
delegate := &sfGauge{}
f := func(v float64) { delegate.Record(t.Context(), v) }
testFloat64ConcurrentSafe(f, delegate.setDelegate)
})
2022-03-22 10:33:13 -05:00
})
// Int64 Instruments
t.Run("Int64", func(*testing.T) {
t.Run("Counter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &siCounter{}
f := func(v int64) { delegate.Add(t.Context(), v) }
testInt64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("UpDownCounter", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &siUpDownCounter{}
f := func(v int64) { delegate.Add(t.Context(), v) }
testInt64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("Histogram", func(*testing.T) {
2022-03-22 10:33:13 -05:00
delegate := &siHistogram{}
f := func(v int64) { delegate.Record(t.Context(), v) }
testInt64ConcurrentSafe(f, delegate.setDelegate)
2022-03-22 10:33:13 -05:00
})
t.Run("Gauge", func(*testing.T) {
delegate := &siGauge{}
f := func(v int64) { delegate.Record(t.Context(), v) }
testInt64ConcurrentSafe(f, delegate.setDelegate)
})
2022-03-22 10:33:13 -05:00
})
}
type testCountingFloatInstrument struct {
count int
metric.Float64Observable
embedded.Float64Counter
embedded.Float64UpDownCounter
embedded.Float64Histogram
embedded.Float64Gauge
embedded.Float64ObservableCounter
embedded.Float64ObservableUpDownCounter
embedded.Float64ObservableGauge
2022-03-22 10:33:13 -05:00
}
func (i *testCountingFloatInstrument) observe() {
2022-03-22 10:33:13 -05:00
i.count++
}
2023-10-16 10:02:21 -07:00
func (i *testCountingFloatInstrument) Add(context.Context, float64, ...metric.AddOption) {
2022-03-22 10:33:13 -05:00
i.count++
}
2023-10-16 10:02:21 -07:00
func (i *testCountingFloatInstrument) Record(context.Context, float64, ...metric.RecordOption) {
2022-03-22 10:33:13 -05:00
i.count++
}
func (*testCountingFloatInstrument) Enabled(context.Context) bool {
return true
}
2022-03-22 10:33:13 -05:00
type testCountingIntInstrument struct {
count int
metric.Int64Observable
embedded.Int64Counter
embedded.Int64UpDownCounter
embedded.Int64Histogram
embedded.Int64Gauge
embedded.Int64ObservableCounter
embedded.Int64ObservableUpDownCounter
embedded.Int64ObservableGauge
2022-03-22 10:33:13 -05:00
}
func (i *testCountingIntInstrument) observe() {
2022-03-22 10:33:13 -05:00
i.count++
}
2023-10-16 10:02:21 -07:00
func (i *testCountingIntInstrument) Add(context.Context, int64, ...metric.AddOption) {
2022-03-22 10:33:13 -05:00
i.count++
}
2023-10-16 10:02:21 -07:00
func (i *testCountingIntInstrument) Record(context.Context, int64, ...metric.RecordOption) {
2022-03-22 10:33:13 -05:00
i.count++
}
func (*testCountingIntInstrument) Enabled(context.Context) bool {
return true
}