1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-26 03:52:03 +02:00

Move metric No-Op to metric/noop (#3893)

* Move metric No-Op to noop pkg

* Remove the unneeded embedded ifaces

* Update CHANGELOG.md

Co-authored-by: Peter Liu <lpfvip2008@gmail.com>

---------

Co-authored-by: Peter Liu <lpfvip2008@gmail.com>
Co-authored-by: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com>
This commit is contained in:
Tyler Yahn 2023-03-21 12:25:23 -07:00 committed by GitHub
parent 571ff65854
commit 3c75a44f84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 451 additions and 226 deletions

View File

@ -24,6 +24,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Both the `Histogram` and `HistogramDataPoint` are redefined with a generic argument of `[N int64 | float64]` in `go.opentelemetry.io/otel/sdk/metric/metricdata`. (#3849)
- The metric `Export` interface from `go.opentelemetry.io/otel/sdk/metric` accepts a `*ResourceMetrics` instead of `ResourceMetrics`. (#3853)
- Rename `Asynchronous` to `Observable` in `go.opentelemetry.io/otel/metric/instrument`. (#3892)
- Move No-Op implementation from `go.opentelemetry.io/otel/metric` into its own package `go.opentelemetry.io/otel/metric/noop`. (#3893)
- `NewNoopMeterProvider` is replaced with `noop.NewMeterProvider`
- `NewNoopMeter` is replaced with `noop.NewMeterProvider().Meter("")`
- Rename `Int64ObserverOption` to `Int64ObservableOption` in `go.opentelemetry.io/otel/metric/instrument`. (#3895)
- Rename `Float64ObserverOption` to `Float64ObservableOption` in `go.opentelemetry.io/otel/metric/instrument`. (#3895)

View File

@ -21,6 +21,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/noop"
)
func testFloat64Race(interact func(context.Context, float64, ...attribute.KeyValue), setDelegate func(metric.Meter)) {
@ -36,7 +37,7 @@ func testFloat64Race(interact func(context.Context, float64, ...attribute.KeyVal
}
}()
setDelegate(metric.NewNoopMeter())
setDelegate(noop.NewMeterProvider().Meter(""))
close(finish)
}
@ -53,7 +54,7 @@ func testInt64Race(interact func(context.Context, int64, ...attribute.KeyValue),
}
}()
setDelegate(metric.NewNoopMeter())
setDelegate(noop.NewMeterProvider().Meter(""))
close(finish)
}

View File

@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/noop"
)
func TestMeterProviderRace(t *testing.T) {
@ -41,7 +42,7 @@ func TestMeterProviderRace(t *testing.T) {
}
}()
mp.setDelegate(metric.NewNoopMeterProvider())
mp.setDelegate(noop.NewMeterProvider())
close(finish)
}
@ -84,7 +85,7 @@ func TestMeterRace(t *testing.T) {
}()
wg.Wait()
mtr.setDelegate(metric.NewNoopMeterProvider())
mtr.setDelegate(noop.NewMeterProvider())
close(finish)
}
@ -113,7 +114,7 @@ func TestUnregisterRace(t *testing.T) {
_ = reg.Unregister()
wg.Wait()
mtr.setDelegate(metric.NewNoopMeterProvider())
mtr.setDelegate(noop.NewMeterProvider())
close(finish)
}

View File

@ -20,6 +20,7 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
@ -152,7 +153,7 @@ func TestSetMeterProvider(t *testing.T) {
t.Run("First Set() should replace the delegate", func(t *testing.T) {
ResetForTest(t)
SetMeterProvider(metric.NewNoopMeterProvider())
SetMeterProvider(noop.NewMeterProvider())
_, ok := MeterProvider().(*meterProvider)
if ok {
@ -165,7 +166,7 @@ func TestSetMeterProvider(t *testing.T) {
mp := MeterProvider()
SetMeterProvider(metric.NewNoopMeterProvider())
SetMeterProvider(noop.NewMeterProvider())
dmp := mp.(*meterProvider)

View File

@ -1,139 +0,0 @@
// 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 metric // import "go.opentelemetry.io/otel/metric"
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument"
)
// NewNoopMeterProvider creates a MeterProvider that does not record any metrics.
func NewNoopMeterProvider() MeterProvider {
return noopMeterProvider{}
}
type noopMeterProvider struct{}
func (noopMeterProvider) Meter(string, ...MeterOption) Meter {
return noopMeter{}
}
// NewNoopMeter creates a Meter that does not record any metrics.
func NewNoopMeter() Meter {
return noopMeter{}
}
type noopMeter struct{}
func (noopMeter) Int64Counter(string, ...instrument.Int64CounterOption) (instrument.Int64Counter, error) {
return nonrecordingSyncInt64Instrument{}, nil
}
func (noopMeter) Int64UpDownCounter(string, ...instrument.Int64UpDownCounterOption) (instrument.Int64UpDownCounter, error) {
return nonrecordingSyncInt64Instrument{}, nil
}
func (noopMeter) Int64Histogram(string, ...instrument.Int64HistogramOption) (instrument.Int64Histogram, error) {
return nonrecordingSyncInt64Instrument{}, nil
}
func (noopMeter) Int64ObservableCounter(string, ...instrument.Int64ObservableCounterOption) (instrument.Int64ObservableCounter, error) {
return nonrecordingAsyncInt64Instrument{}, nil
}
func (noopMeter) Int64ObservableUpDownCounter(string, ...instrument.Int64ObservableUpDownCounterOption) (instrument.Int64ObservableUpDownCounter, error) {
return nonrecordingAsyncInt64Instrument{}, nil
}
func (noopMeter) Int64ObservableGauge(string, ...instrument.Int64ObservableGaugeOption) (instrument.Int64ObservableGauge, error) {
return nonrecordingAsyncInt64Instrument{}, nil
}
func (noopMeter) Float64Counter(string, ...instrument.Float64CounterOption) (instrument.Float64Counter, error) {
return nonrecordingSyncFloat64Instrument{}, nil
}
func (noopMeter) Float64UpDownCounter(string, ...instrument.Float64UpDownCounterOption) (instrument.Float64UpDownCounter, error) {
return nonrecordingSyncFloat64Instrument{}, nil
}
func (noopMeter) Float64Histogram(string, ...instrument.Float64HistogramOption) (instrument.Float64Histogram, error) {
return nonrecordingSyncFloat64Instrument{}, nil
}
func (noopMeter) Float64ObservableCounter(string, ...instrument.Float64ObservableCounterOption) (instrument.Float64ObservableCounter, error) {
return nonrecordingAsyncFloat64Instrument{}, nil
}
func (noopMeter) Float64ObservableUpDownCounter(string, ...instrument.Float64ObservableUpDownCounterOption) (instrument.Float64ObservableUpDownCounter, error) {
return nonrecordingAsyncFloat64Instrument{}, nil
}
func (noopMeter) Float64ObservableGauge(string, ...instrument.Float64ObservableGaugeOption) (instrument.Float64ObservableGauge, error) {
return nonrecordingAsyncFloat64Instrument{}, nil
}
// RegisterCallback creates a register callback that does not record any metrics.
func (noopMeter) RegisterCallback(Callback, ...instrument.Observable) (Registration, error) {
return noopReg{}, nil
}
type noopReg struct{}
func (noopReg) Unregister() error { return nil }
type nonrecordingAsyncFloat64Instrument struct {
instrument.Float64Observable
}
var (
_ instrument.Float64ObservableCounter = nonrecordingAsyncFloat64Instrument{}
_ instrument.Float64ObservableUpDownCounter = nonrecordingAsyncFloat64Instrument{}
_ instrument.Float64ObservableGauge = nonrecordingAsyncFloat64Instrument{}
)
type nonrecordingAsyncInt64Instrument struct {
instrument.Int64Observable
}
var (
_ instrument.Int64ObservableCounter = nonrecordingAsyncInt64Instrument{}
_ instrument.Int64ObservableUpDownCounter = nonrecordingAsyncInt64Instrument{}
_ instrument.Int64ObservableGauge = nonrecordingAsyncInt64Instrument{}
)
type nonrecordingSyncFloat64Instrument struct{}
var (
_ instrument.Float64Counter = nonrecordingSyncFloat64Instrument{}
_ instrument.Float64UpDownCounter = nonrecordingSyncFloat64Instrument{}
_ instrument.Float64Histogram = nonrecordingSyncFloat64Instrument{}
)
func (nonrecordingSyncFloat64Instrument) Add(context.Context, float64, ...attribute.KeyValue) {}
func (nonrecordingSyncFloat64Instrument) Record(context.Context, float64, ...attribute.KeyValue) {}
type nonrecordingSyncInt64Instrument struct{}
var (
_ instrument.Int64Counter = nonrecordingSyncInt64Instrument{}
_ instrument.Int64UpDownCounter = nonrecordingSyncInt64Instrument{}
_ instrument.Int64Histogram = nonrecordingSyncInt64Instrument{}
)
func (nonrecordingSyncInt64Instrument) Add(context.Context, int64, ...attribute.KeyValue) {}
func (nonrecordingSyncInt64Instrument) Record(context.Context, int64, ...attribute.KeyValue) {}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package metric_test
package noop_test
import (
"context"
@ -23,12 +23,13 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/noop"
)
//nolint:govet // Meter doesn't register for go vet
func ExampleMeter_synchronous() {
// In a library or program this would be provided by otel.GetMeterProvider().
meterProvider := metric.NewNoopMeterProvider()
meterProvider := noop.NewMeterProvider()
workDuration, err := meterProvider.Meter("go.opentelemetry.io/otel/metric#SyncExample").Int64Histogram(
"workDuration",
@ -48,7 +49,7 @@ func ExampleMeter_synchronous() {
//nolint:govet // Meter doesn't register for go vet
func ExampleMeter_asynchronous_single() {
// In a library or program this would be provided by otel.GetMeterProvider().
meterProvider := metric.NewNoopMeterProvider()
meterProvider := noop.NewMeterProvider()
meter := meterProvider.Meter("go.opentelemetry.io/otel/metric#AsyncExample")
_, err := meter.Int64ObservableGauge(
@ -80,7 +81,7 @@ func ExampleMeter_asynchronous_single() {
//nolint:govet // Meter doesn't register for go vet
func ExampleMeter_asynchronous_multiple() {
meterProvider := metric.NewNoopMeterProvider()
meterProvider := noop.NewMeterProvider()
meter := meterProvider.Meter("go.opentelemetry.io/otel/metric#MultiAsyncExample")
// This is just a sample of memory stats to record from the Memstats

246
metric/noop/noop.go Normal file
View File

@ -0,0 +1,246 @@
// 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 noop provides an implementation of the OpenTelemetry API that
// produces no telemetry and minimizes used computation resources.
//
// The API implementation can be used to effectively disable OpenTelemetry. It
// can also be used as the embedded structs of other OpenTelemetry
// implementations. These alternate implementation that embed this noop
// implementation will default to no-action instead of panicking when methods
// are added to the OpenTelemetry API interfaces.
package noop // import "go.opentelemetry.io/otel/metric/noop"
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
)
var (
// Compile-time check this implements the OpenTelemetry API.
_ metric.MeterProvider = MeterProvider{}
_ metric.Meter = Meter{}
_ metric.Observer = Observer{}
_ metric.Registration = Registration{}
_ instrument.Int64Counter = Int64Counter{}
_ instrument.Float64Counter = Float64Counter{}
_ instrument.Int64UpDownCounter = Int64UpDownCounter{}
_ instrument.Float64UpDownCounter = Float64UpDownCounter{}
_ instrument.Int64Histogram = Int64Histogram{}
_ instrument.Float64Histogram = Float64Histogram{}
_ instrument.Int64ObservableCounter = Int64ObservableCounter{}
_ instrument.Float64ObservableCounter = Float64ObservableCounter{}
_ instrument.Int64ObservableGauge = Int64ObservableGauge{}
_ instrument.Float64ObservableGauge = Float64ObservableGauge{}
_ instrument.Int64ObservableUpDownCounter = Int64ObservableUpDownCounter{}
_ instrument.Float64ObservableUpDownCounter = Float64ObservableUpDownCounter{}
_ instrument.Int64Observer = Int64Observer{}
_ instrument.Float64Observer = Float64Observer{}
)
// MeterProvider is an OpenTelemetry No-Op MeterProvider.
type MeterProvider struct{}
// NewMeterProvider returns a MeterProvider that does not record any telemetry.
func NewMeterProvider() MeterProvider {
return MeterProvider{}
}
// Meter returns an OpenTelemetry Meter that does not record any telemetry.
func (MeterProvider) Meter(string, ...metric.MeterOption) metric.Meter {
return Meter{}
}
// Meter is an OpenTelemetry No-Op Meter.
type Meter struct{}
// Int64Counter returns a Counter used to record int64 measurements that
// produces no telemetry.
func (Meter) Int64Counter(string, ...instrument.Int64CounterOption) (instrument.Int64Counter, error) {
return Int64Counter{}, nil
}
// Int64UpDownCounter returns an UpDownCounter used to record int64
// measurements that produces no telemetry.
func (Meter) Int64UpDownCounter(string, ...instrument.Int64UpDownCounterOption) (instrument.Int64UpDownCounter, error) {
return Int64UpDownCounter{}, nil
}
// Int64Histogram returns a Histogram used to record int64 measurements that
// produces no telemetry.
func (Meter) Int64Histogram(string, ...instrument.Int64HistogramOption) (instrument.Int64Histogram, error) {
return Int64Histogram{}, nil
}
// Int64ObservableCounter returns an ObservableCounter used to record int64
// measurements that produces no telemetry.
func (Meter) Int64ObservableCounter(string, ...instrument.Int64ObservableCounterOption) (instrument.Int64ObservableCounter, error) {
return Int64ObservableCounter{}, nil
}
// Int64ObservableUpDownCounter returns an ObservableUpDownCounter used to
// record int64 measurements that produces no telemetry.
func (Meter) Int64ObservableUpDownCounter(string, ...instrument.Int64ObservableUpDownCounterOption) (instrument.Int64ObservableUpDownCounter, error) {
return Int64ObservableUpDownCounter{}, nil
}
// Int64ObservableGauge returns an ObservableGauge used to record int64
// measurements that produces no telemetry.
func (Meter) Int64ObservableGauge(string, ...instrument.Int64ObservableGaugeOption) (instrument.Int64ObservableGauge, error) {
return Int64ObservableGauge{}, nil
}
// Float64Counter returns a Counter used to record int64 measurements that
// produces no telemetry.
func (Meter) Float64Counter(string, ...instrument.Float64CounterOption) (instrument.Float64Counter, error) {
return Float64Counter{}, nil
}
// Float64UpDownCounter returns an UpDownCounter used to record int64
// measurements that produces no telemetry.
func (Meter) Float64UpDownCounter(string, ...instrument.Float64UpDownCounterOption) (instrument.Float64UpDownCounter, error) {
return Float64UpDownCounter{}, nil
}
// Float64Histogram returns a Histogram used to record int64 measurements that
// produces no telemetry.
func (Meter) Float64Histogram(string, ...instrument.Float64HistogramOption) (instrument.Float64Histogram, error) {
return Float64Histogram{}, nil
}
// Float64ObservableCounter returns an ObservableCounter used to record int64
// measurements that produces no telemetry.
func (Meter) Float64ObservableCounter(string, ...instrument.Float64ObservableCounterOption) (instrument.Float64ObservableCounter, error) {
return Float64ObservableCounter{}, nil
}
// Float64ObservableUpDownCounter returns an ObservableUpDownCounter used to
// record int64 measurements that produces no telemetry.
func (Meter) Float64ObservableUpDownCounter(string, ...instrument.Float64ObservableUpDownCounterOption) (instrument.Float64ObservableUpDownCounter, error) {
return Float64ObservableUpDownCounter{}, nil
}
// Float64ObservableGauge returns an ObservableGauge used to record int64
// measurements that produces no telemetry.
func (Meter) Float64ObservableGauge(string, ...instrument.Float64ObservableGaugeOption) (instrument.Float64ObservableGauge, error) {
return Float64ObservableGauge{}, nil
}
// RegisterCallback performs no operation.
func (Meter) RegisterCallback(metric.Callback, ...instrument.Observable) (metric.Registration, error) {
return Registration{}, nil
}
// Observer acts as a recorder of measurements for multiple instruments in a
// Callback, it performing no operation.
type Observer struct{}
// ObserveFloat64 performs no operation.
func (Observer) ObserveFloat64(instrument.Float64Observable, float64, ...attribute.KeyValue) {
}
// ObserveInt64 performs no operation.
func (Observer) ObserveInt64(instrument.Int64Observable, int64, ...attribute.KeyValue) {
}
// Registration is the registration of a Callback with a No-Op Meter.
type Registration struct{}
// Unregister unregisters the Callback the Registration represents with the
// No-Op Meter. This will always return nil because the No-Op Meter performs no
// operation, including hold any record of registrations.
func (Registration) Unregister() error { return nil }
// Int64Counter is an OpenTelemetry Counter used to record int64 measurements.
// It produces no telemetry.
type Int64Counter struct{}
// Add performs no operation.
func (Int64Counter) Add(context.Context, int64, ...attribute.KeyValue) {}
// Float64Counter is an OpenTelemetry Counter used to record float64
// measurements. It produces no telemetry.
type Float64Counter struct{}
// Add performs no operation.
func (Float64Counter) Add(context.Context, float64, ...attribute.KeyValue) {}
// Int64UpDownCounter is an OpenTelemetry UpDownCounter used to record int64
// measurements. It produces no telemetry.
type Int64UpDownCounter struct{}
// Add performs no operation.
func (Int64UpDownCounter) Add(context.Context, int64, ...attribute.KeyValue) {}
// Float64UpDownCounter is an OpenTelemetry UpDownCounter used to record
// float64 measurements. It produces no telemetry.
type Float64UpDownCounter struct{}
// Add performs no operation.
func (Float64UpDownCounter) Add(context.Context, float64, ...attribute.KeyValue) {}
// Int64Histogram is an OpenTelemetry Histogram used to record int64
// measurements. It produces no telemetry.
type Int64Histogram struct{}
// Record performs no operation.
func (Int64Histogram) Record(context.Context, int64, ...attribute.KeyValue) {}
// Float64Histogram is an OpenTelemetry Histogram used to record float64
// measurements. It produces no telemetry.
type Float64Histogram struct{}
// Record performs no operation.
func (Float64Histogram) Record(context.Context, float64, ...attribute.KeyValue) {}
// Int64ObservableCounter is an OpenTelemetry ObservableCounter used to record
// int64 measurements. It produces no telemetry.
type Int64ObservableCounter struct{ instrument.Int64Observable }
// Float64ObservableCounter is an OpenTelemetry ObservableCounter used to record
// float64 measurements. It produces no telemetry.
type Float64ObservableCounter struct{ instrument.Float64Observable }
// Int64ObservableGauge is an OpenTelemetry ObservableGauge used to record
// int64 measurements. It produces no telemetry.
type Int64ObservableGauge struct{ instrument.Int64Observable }
// Float64ObservableGauge is an OpenTelemetry ObservableGauge used to record
// float64 measurements. It produces no telemetry.
type Float64ObservableGauge struct{ instrument.Float64Observable }
// Int64ObservableUpDownCounter is an OpenTelemetry ObservableUpDownCounter
// used to record int64 measurements. It produces no telemetry.
type Int64ObservableUpDownCounter struct{ instrument.Int64Observable }
// Float64ObservableUpDownCounter is an OpenTelemetry ObservableUpDownCounter
// used to record float64 measurements. It produces no telemetry.
type Float64ObservableUpDownCounter struct{ instrument.Float64Observable }
// Int64Observer is a recorder of int64 measurements that performs no operation.
type Int64Observer struct{}
// Observe performs no operation.
func (Int64Observer) Observe(int64, ...attribute.KeyValue) {}
// Float64Observer is a recorder of float64 measurements that performs no
// operation.
type Float64Observer struct{}
// Observe performs no operation.
func (Float64Observer) Observe(float64, ...attribute.KeyValue) {}

184
metric/noop/noop_test.go Normal file
View File

@ -0,0 +1,184 @@
// 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 noop // import "go.opentelemetry.io/otel/metric/noop"
import (
"context"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
)
func TestImplementationNoPanics(t *testing.T) {
// Check that if type has an embedded interface and that interface has
// methods added to it than the No-Op implementation implements them.
t.Run("MeterProvider", assertAllExportedMethodNoPanic(
reflect.ValueOf(MeterProvider{}),
reflect.TypeOf((*metric.MeterProvider)(nil)).Elem(),
))
t.Run("Meter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Meter{}),
reflect.TypeOf((*metric.Meter)(nil)).Elem(),
))
t.Run("Observer", assertAllExportedMethodNoPanic(
reflect.ValueOf(Observer{}),
reflect.TypeOf((*metric.Observer)(nil)).Elem(),
))
t.Run("Registration", assertAllExportedMethodNoPanic(
reflect.ValueOf(Registration{}),
reflect.TypeOf((*metric.Registration)(nil)).Elem(),
))
t.Run("Int64Counter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64Counter{}),
reflect.TypeOf((*instrument.Int64Counter)(nil)).Elem(),
))
t.Run("Float64Counter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64Counter{}),
reflect.TypeOf((*instrument.Float64Counter)(nil)).Elem(),
))
t.Run("Int64UpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64UpDownCounter{}),
reflect.TypeOf((*instrument.Int64UpDownCounter)(nil)).Elem(),
))
t.Run("Float64UpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64UpDownCounter{}),
reflect.TypeOf((*instrument.Float64UpDownCounter)(nil)).Elem(),
))
t.Run("Int64Histogram", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64Histogram{}),
reflect.TypeOf((*instrument.Int64Histogram)(nil)).Elem(),
))
t.Run("Float64Histogram", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64Histogram{}),
reflect.TypeOf((*instrument.Float64Histogram)(nil)).Elem(),
))
t.Run("Int64ObservableCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64ObservableCounter{}),
reflect.TypeOf((*instrument.Int64ObservableCounter)(nil)).Elem(),
))
t.Run("Float64ObservableCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64ObservableCounter{}),
reflect.TypeOf((*instrument.Float64ObservableCounter)(nil)).Elem(),
))
t.Run("Int64ObservableGauge", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64ObservableGauge{}),
reflect.TypeOf((*instrument.Int64ObservableGauge)(nil)).Elem(),
))
t.Run("Float64ObservableGauge", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64ObservableGauge{}),
reflect.TypeOf((*instrument.Float64ObservableGauge)(nil)).Elem(),
))
t.Run("Int64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64ObservableUpDownCounter{}),
reflect.TypeOf((*instrument.Int64ObservableUpDownCounter)(nil)).Elem(),
))
t.Run("Float64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64ObservableUpDownCounter{}),
reflect.TypeOf((*instrument.Float64ObservableUpDownCounter)(nil)).Elem(),
))
t.Run("Int64Observer", assertAllExportedMethodNoPanic(
reflect.ValueOf(Int64Observer{}),
reflect.TypeOf((*instrument.Int64Observer)(nil)).Elem(),
))
t.Run("Float64Observer", assertAllExportedMethodNoPanic(
reflect.ValueOf(Float64Observer{}),
reflect.TypeOf((*instrument.Float64Observer)(nil)).Elem(),
))
}
func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) {
return func(t *testing.T) {
for n := 0; n < rType.NumMethod(); n++ {
mType := rType.Method(n)
if !mType.IsExported() {
t.Logf("ignoring unexported %s", mType.Name)
continue
}
m := rVal.MethodByName(mType.Name)
if !m.IsValid() {
t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name)
}
numIn := mType.Type.NumIn()
if mType.Type.IsVariadic() {
numIn--
}
args := make([]reflect.Value, numIn)
for i := range args {
aType := mType.Type.In(i)
args[i] = reflect.New(aType).Elem()
}
assert.NotPanicsf(t, func() {
_ = m.Call(args)
}, "%s.%s", rVal.Type().Name(), mType.Name)
}
}
}
func TestNewMeterProvider(t *testing.T) {
mp := NewMeterProvider()
assert.Equal(t, mp, MeterProvider{})
meter := mp.Meter("")
assert.Equal(t, meter, Meter{})
}
func TestSyncFloat64(t *testing.T) {
meter := NewMeterProvider().Meter("test instrumentation")
assert.NotPanics(t, func() {
inst, err := meter.Float64Counter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), 1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Float64UpDownCounter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), -1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Float64Histogram("test instrument")
require.NoError(t, err)
inst.Record(context.Background(), 1.0, attribute.String("key", "value"))
})
}
func TestSyncInt64(t *testing.T) {
meter := NewMeterProvider().Meter("test instrumentation")
assert.NotPanics(t, func() {
inst, err := meter.Int64Counter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), 1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Int64UpDownCounter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), -1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Int64Histogram("test instrument")
require.NoError(t, err)
inst.Record(context.Background(), 1, attribute.String("key", "value"))
})
}

View File

@ -1,74 +0,0 @@
// 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 metric
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
)
func TestNewNoopMeterProvider(t *testing.T) {
mp := NewNoopMeterProvider()
assert.Equal(t, mp, noopMeterProvider{})
meter := mp.Meter("")
assert.Equal(t, meter, noopMeter{})
}
func TestSyncFloat64(t *testing.T) {
meter := NewNoopMeterProvider().Meter("test instrumentation")
assert.NotPanics(t, func() {
inst, err := meter.Float64Counter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), 1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Float64UpDownCounter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), -1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Float64Histogram("test instrument")
require.NoError(t, err)
inst.Record(context.Background(), 1.0, attribute.String("key", "value"))
})
}
func TestSyncInt64(t *testing.T) {
meter := NewNoopMeterProvider().Meter("test instrumentation")
assert.NotPanics(t, func() {
inst, err := meter.Int64Counter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), 1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Int64UpDownCounter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), -1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.Int64Histogram("test instrument")
require.NoError(t, err)
inst.Record(context.Background(), 1, attribute.String("key", "value"))
})
}

View File

@ -20,6 +20,7 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
)
type testMeterProvider struct{}
@ -27,12 +28,12 @@ type testMeterProvider struct{}
var _ metric.MeterProvider = &testMeterProvider{}
func (*testMeterProvider) Meter(_ string, _ ...metric.MeterOption) metric.Meter {
return metric.NewNoopMeterProvider().Meter("")
return noop.NewMeterProvider().Meter("")
}
func TestMultipleGlobalMeterProvider(t *testing.T) {
p1 := testMeterProvider{}
p2 := metric.NewNoopMeterProvider()
p2 := noop.NewMeterProvider()
SetMeterProvider(&p1)
SetMeterProvider(p2)