1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/metric/internal/global/instruments_test.go
Bogdan Drutu a9a519277a
Move metric no-op implementation to metric package (#2866)
* Move metric no-op implementation to metric package

This is to be consistent with trace package.

Fixes: https://github.com/open-telemetry/opentelemetry-go/issues/2767

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>

* Update CHANGELOG.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update metric/noop.go

Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>

* Update noop.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
2022-04-28 12:52:10 -07:00

171 lines
4.2 KiB
Go

// 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 (
"context"
"testing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
)
func testFloat64Race(interact func(context.Context, float64, ...attribute.KeyValue), setDelegate func(metric.Meter)) {
finish := make(chan struct{})
go func() {
for {
interact(context.Background(), 1)
select {
case <-finish:
return
default:
}
}
}()
setDelegate(metric.NewNoopMeter())
close(finish)
}
func testInt64Race(interact func(context.Context, int64, ...attribute.KeyValue), setDelegate func(metric.Meter)) {
finish := make(chan struct{})
go func() {
for {
interact(context.Background(), 1)
select {
case <-finish:
return
default:
}
}
}()
setDelegate(metric.NewNoopMeter())
close(finish)
}
func TestAsyncInstrumentSetDelegateRace(t *testing.T) {
// Float64 Instruments
t.Run("Float64", func(t *testing.T) {
t.Run("Counter", func(t *testing.T) {
delegate := &afCounter{}
testFloat64Race(delegate.Observe, delegate.setDelegate)
})
t.Run("UpDownCounter", func(t *testing.T) {
delegate := &afUpDownCounter{}
testFloat64Race(delegate.Observe, delegate.setDelegate)
})
t.Run("Gauge", func(t *testing.T) {
delegate := &afGauge{}
testFloat64Race(delegate.Observe, delegate.setDelegate)
})
})
// Int64 Instruments
t.Run("Int64", func(t *testing.T) {
t.Run("Counter", func(t *testing.T) {
delegate := &aiCounter{}
testInt64Race(delegate.Observe, delegate.setDelegate)
})
t.Run("UpDownCounter", func(t *testing.T) {
delegate := &aiUpDownCounter{}
testInt64Race(delegate.Observe, delegate.setDelegate)
})
t.Run("Gauge", func(t *testing.T) {
delegate := &aiGauge{}
testInt64Race(delegate.Observe, delegate.setDelegate)
})
})
}
func TestSyncInstrumentSetDelegateRace(t *testing.T) {
// Float64 Instruments
t.Run("Float64", func(t *testing.T) {
t.Run("Counter", func(t *testing.T) {
delegate := &sfCounter{}
testFloat64Race(delegate.Add, delegate.setDelegate)
})
t.Run("UpDownCounter", func(t *testing.T) {
delegate := &sfUpDownCounter{}
testFloat64Race(delegate.Add, delegate.setDelegate)
})
t.Run("Histogram", func(t *testing.T) {
delegate := &sfHistogram{}
testFloat64Race(delegate.Record, delegate.setDelegate)
})
})
// Int64 Instruments
t.Run("Int64", func(t *testing.T) {
t.Run("Counter", func(t *testing.T) {
delegate := &siCounter{}
testInt64Race(delegate.Add, delegate.setDelegate)
})
t.Run("UpDownCounter", func(t *testing.T) {
delegate := &siUpDownCounter{}
testInt64Race(delegate.Add, delegate.setDelegate)
})
t.Run("Histogram", func(t *testing.T) {
delegate := &siHistogram{}
testInt64Race(delegate.Record, delegate.setDelegate)
})
})
}
type testCountingFloatInstrument struct {
count int
instrument.Asynchronous
instrument.Synchronous
}
func (i *testCountingFloatInstrument) Observe(context.Context, float64, ...attribute.KeyValue) {
i.count++
}
func (i *testCountingFloatInstrument) Add(context.Context, float64, ...attribute.KeyValue) {
i.count++
}
func (i *testCountingFloatInstrument) Record(context.Context, float64, ...attribute.KeyValue) {
i.count++
}
type testCountingIntInstrument struct {
count int
instrument.Asynchronous
instrument.Synchronous
}
func (i *testCountingIntInstrument) Observe(context.Context, int64, ...attribute.KeyValue) {
i.count++
}
func (i *testCountingIntInstrument) Add(context.Context, int64, ...attribute.KeyValue) {
i.count++
}
func (i *testCountingIntInstrument) Record(context.Context, int64, ...attribute.KeyValue) {
i.count++
}