1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-02-03 13:11:53 +02:00

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>
This commit is contained in:
Bogdan Drutu 2022-04-28 12:52:10 -07:00 committed by GitHub
parent a14ecc7a57
commit a9a519277a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 184 additions and 92 deletions

View File

@ -32,6 +32,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
The method included in the renamed interface also changed from `LabelFilterFor` to `AttributeFilterFor`. (#2790)
- The `Metadata.Labels` method from the `go.opentelemetry.io/otel/sdk/metric/export` package is renamed to `Metadata.Attributes`.
Consequentially, the `Record` type from the same package also has had the embedded method renamed. (#2790)
- Move metric no-op implementation form `nonrecording` to `metric` package. (#2866)
### Deprecated

View File

@ -20,16 +20,16 @@ import (
"runtime"
"time"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/nonrecording"
"go.opentelemetry.io/otel/metric/unit"
)
//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 := nonrecording.NewNoopMeterProvider()
meterProvider := metric.NewNoopMeterProvider()
workDuration, err := meterProvider.Meter("go.opentelemetry.io/otel/metric#SyncExample").SyncInt64().Histogram(
"workDuration",
@ -50,7 +50,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 := nonrecording.NewNoopMeterProvider()
meterProvider := metric.NewNoopMeterProvider()
meter := meterProvider.Meter("go.opentelemetry.io/otel/metric#AsyncExample")
memoryUsage, err := meter.AsyncInt64().Gauge(
@ -79,7 +79,7 @@ func ExampleMeter_asynchronous_single() {
//nolint:govet // Meter doesn't register for go vet
func ExampleMeter_asynchronous_multiple() {
meterProvider := nonrecording.NewNoopMeterProvider()
meterProvider := metric.NewNoopMeterProvider()
meter := meterProvider.Meter("go.opentelemetry.io/otel/metric#MultiAsyncExample")
// This is just a sample of memory stats to record from the Memstats

View File

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

View File

@ -27,7 +27,6 @@ import (
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/asyncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/nonrecording"
)
func TestMeterProviderRace(t *testing.T) {
@ -44,7 +43,7 @@ func TestMeterProviderRace(t *testing.T) {
}
}()
mp.setDelegate(nonrecording.NewNoopMeterProvider())
mp.setDelegate(metric.NewNoopMeterProvider())
close(finish)
}
@ -84,7 +83,7 @@ func TestMeterRace(t *testing.T) {
}()
wg.Wait()
mtr.setDelegate(nonrecording.NewNoopMeterProvider())
mtr.setDelegate(metric.NewNoopMeterProvider())
close(finish)
}

View File

@ -21,7 +21,6 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/nonrecording"
)
func resetGlobalMeterProvider() {
@ -55,7 +54,7 @@ func TestSetMeterProvider(t *testing.T) {
t.Run("First Set() should replace the delegate", func(t *testing.T) {
resetGlobalMeterProvider()
SetMeterProvider(nonrecording.NewNoopMeterProvider())
SetMeterProvider(metric.NewNoopMeterProvider())
_, ok := MeterProvider().(*meterProvider)
if ok {
@ -68,7 +67,7 @@ func TestSetMeterProvider(t *testing.T) {
mp := MeterProvider()
SetMeterProvider(nonrecording.NewNoopMeterProvider())
SetMeterProvider(metric.NewNoopMeterProvider())
dmp := mp.(*meterProvider)

View File

@ -1,64 +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 nonrecording // import "go.opentelemetry.io/otel/metric/nonrecording"
import (
"context"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/asyncfloat64"
"go.opentelemetry.io/otel/metric/instrument/asyncint64"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
)
// NewNoopMeterProvider creates a MeterProvider that does not record any metrics.
func NewNoopMeterProvider() metric.MeterProvider {
return noopMeterProvider{}
}
type noopMeterProvider struct{}
var _ metric.MeterProvider = noopMeterProvider{}
func (noopMeterProvider) Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter {
return noopMeter{}
}
// NewNoopMeter creates a Meter that does not record any metrics.
func NewNoopMeter() metric.Meter {
return noopMeter{}
}
type noopMeter struct{}
var _ metric.Meter = noopMeter{}
func (noopMeter) AsyncInt64() asyncint64.InstrumentProvider {
return nonrecordingAsyncInt64Instrument{}
}
func (noopMeter) AsyncFloat64() asyncfloat64.InstrumentProvider {
return nonrecordingAsyncFloat64Instrument{}
}
func (noopMeter) SyncInt64() syncint64.InstrumentProvider {
return nonrecordingSyncInt64Instrument{}
}
func (noopMeter) SyncFloat64() syncfloat64.InstrumentProvider {
return nonrecordingSyncFloat64Instrument{}
}
func (noopMeter) RegisterCallback([]instrument.Asynchronous, func(context.Context)) error {
return nil
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package nonrecording // import "go.opentelemetry.io/otel/metric/nonrecording"
package metric // import "go.opentelemetry.io/otel/metric"
import (
"context"
@ -25,6 +25,49 @@ import (
"go.opentelemetry.io/otel/metric/instrument/syncint64"
)
// 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{}
// AsyncInt64 creates a instrument that does not record any metrics.
func (noopMeter) AsyncInt64() asyncint64.InstrumentProvider {
return nonrecordingAsyncInt64Instrument{}
}
// AsyncFloat64 creates a instrument that does not record any metrics.
func (noopMeter) AsyncFloat64() asyncfloat64.InstrumentProvider {
return nonrecordingAsyncFloat64Instrument{}
}
// SyncInt64 creates a instrument that does not record any metrics.
func (noopMeter) SyncInt64() syncint64.InstrumentProvider {
return nonrecordingSyncInt64Instrument{}
}
// SyncFloat64 creates a instrument that does not record any metrics.
func (noopMeter) SyncFloat64() syncfloat64.InstrumentProvider {
return nonrecordingSyncFloat64Instrument{}
}
// RegisterCallback creates a register callback that does not record any metrics.
func (noopMeter) RegisterCallback([]instrument.Asynchronous, func(context.Context)) error {
return nil
}
type nonrecordingAsyncFloat64Instrument struct {
instrument.Asynchronous
}
@ -36,15 +79,15 @@ var (
_ asyncfloat64.Gauge = nonrecordingAsyncFloat64Instrument{}
)
func (n nonrecordingAsyncFloat64Instrument) Counter(name string, opts ...instrument.Option) (asyncfloat64.Counter, error) {
func (n nonrecordingAsyncFloat64Instrument) Counter(string, ...instrument.Option) (asyncfloat64.Counter, error) {
return n, nil
}
func (n nonrecordingAsyncFloat64Instrument) UpDownCounter(name string, opts ...instrument.Option) (asyncfloat64.UpDownCounter, error) {
func (n nonrecordingAsyncFloat64Instrument) UpDownCounter(string, ...instrument.Option) (asyncfloat64.UpDownCounter, error) {
return n, nil
}
func (n nonrecordingAsyncFloat64Instrument) Gauge(name string, opts ...instrument.Option) (asyncfloat64.Gauge, error) {
func (n nonrecordingAsyncFloat64Instrument) Gauge(string, ...instrument.Option) (asyncfloat64.Gauge, error) {
return n, nil
}
@ -63,15 +106,15 @@ var (
_ asyncint64.Gauge = nonrecordingAsyncInt64Instrument{}
)
func (n nonrecordingAsyncInt64Instrument) Counter(name string, opts ...instrument.Option) (asyncint64.Counter, error) {
func (n nonrecordingAsyncInt64Instrument) Counter(string, ...instrument.Option) (asyncint64.Counter, error) {
return n, nil
}
func (n nonrecordingAsyncInt64Instrument) UpDownCounter(name string, opts ...instrument.Option) (asyncint64.UpDownCounter, error) {
func (n nonrecordingAsyncInt64Instrument) UpDownCounter(string, ...instrument.Option) (asyncint64.UpDownCounter, error) {
return n, nil
}
func (n nonrecordingAsyncInt64Instrument) Gauge(name string, opts ...instrument.Option) (asyncint64.Gauge, error) {
func (n nonrecordingAsyncInt64Instrument) Gauge(string, ...instrument.Option) (asyncint64.Gauge, error) {
return n, nil
}
@ -89,15 +132,15 @@ var (
_ syncfloat64.Histogram = nonrecordingSyncFloat64Instrument{}
)
func (n nonrecordingSyncFloat64Instrument) Counter(name string, opts ...instrument.Option) (syncfloat64.Counter, error) {
func (n nonrecordingSyncFloat64Instrument) Counter(string, ...instrument.Option) (syncfloat64.Counter, error) {
return n, nil
}
func (n nonrecordingSyncFloat64Instrument) UpDownCounter(name string, opts ...instrument.Option) (syncfloat64.UpDownCounter, error) {
func (n nonrecordingSyncFloat64Instrument) UpDownCounter(string, ...instrument.Option) (syncfloat64.UpDownCounter, error) {
return n, nil
}
func (n nonrecordingSyncFloat64Instrument) Histogram(name string, opts ...instrument.Option) (syncfloat64.Histogram, error) {
func (n nonrecordingSyncFloat64Instrument) Histogram(string, ...instrument.Option) (syncfloat64.Histogram, error) {
return n, nil
}
@ -120,15 +163,15 @@ var (
_ syncint64.Histogram = nonrecordingSyncInt64Instrument{}
)
func (n nonrecordingSyncInt64Instrument) Counter(name string, opts ...instrument.Option) (syncint64.Counter, error) {
func (n nonrecordingSyncInt64Instrument) Counter(string, ...instrument.Option) (syncint64.Counter, error) {
return n, nil
}
func (n nonrecordingSyncInt64Instrument) UpDownCounter(name string, opts ...instrument.Option) (syncint64.UpDownCounter, error) {
func (n nonrecordingSyncInt64Instrument) UpDownCounter(string, ...instrument.Option) (syncint64.UpDownCounter, error) {
return n, nil
}
func (n nonrecordingSyncInt64Instrument) Histogram(name string, opts ...instrument.Option) (syncint64.Histogram, error) {
func (n nonrecordingSyncInt64Instrument) Histogram(string, ...instrument.Option) (syncint64.Histogram, error) {
return n, nil
}

116
metric/noop_test.go Normal file
View File

@ -0,0 +1,116 @@
// 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.SyncFloat64().Counter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), 1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.SyncFloat64().UpDownCounter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), -1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.SyncFloat64().Histogram("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.SyncInt64().Counter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), 1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.SyncInt64().UpDownCounter("test instrument")
require.NoError(t, err)
inst.Add(context.Background(), -1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.SyncInt64().Histogram("test instrument")
require.NoError(t, err)
inst.Record(context.Background(), 1, attribute.String("key", "value"))
})
}
func TestAsyncFloat64(t *testing.T) {
meter := NewNoopMeterProvider().Meter("test instrumentation")
assert.NotPanics(t, func() {
inst, err := meter.AsyncFloat64().Counter("test instrument")
require.NoError(t, err)
inst.Observe(context.Background(), 1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.AsyncFloat64().UpDownCounter("test instrument")
require.NoError(t, err)
inst.Observe(context.Background(), -1.0, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.AsyncFloat64().Gauge("test instrument")
require.NoError(t, err)
inst.Observe(context.Background(), 1.0, attribute.String("key", "value"))
})
}
func TestAsyncInt64(t *testing.T) {
meter := NewNoopMeterProvider().Meter("test instrumentation")
assert.NotPanics(t, func() {
inst, err := meter.AsyncInt64().Counter("test instrument")
require.NoError(t, err)
inst.Observe(context.Background(), 1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.AsyncInt64().UpDownCounter("test instrument")
require.NoError(t, err)
inst.Observe(context.Background(), -1, attribute.String("key", "value"))
})
assert.NotPanics(t, func() {
inst, err := meter.AsyncInt64().Gauge("test instrument")
require.NoError(t, err)
inst.Observe(context.Background(), 1, attribute.String("key", "value"))
})
}

View File

@ -28,7 +28,6 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/asyncint64"
"go.opentelemetry.io/otel/metric/nonrecording"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregator"
"go.opentelemetry.io/otel/sdk/metric/export"
@ -532,7 +531,7 @@ func TestIncorrectInstruments(t *testing.T) {
require.Equal(t, 0, collected)
// Now try with instruments from another SDK.
noopMeter := nonrecording.NewNoopMeter()
noopMeter := metric.NewNoopMeter()
observer, _ = noopMeter.AsyncInt64().Gauge("observer")
err = meter.RegisterCallback(