diff --git a/CHANGELOG.md b/CHANGELOG.md index 444429393c7..4f23283478b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. (#4439) ### Deprecated diff --git a/metric/example_test.go b/metric/example_test.go index ed5c80e208a..9c18b9a6890 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -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 diff --git a/metric/internal/global/instruments_test.go b/metric/internal/global/instruments_test.go index 33c88f6b6f1..24c5eb4322e 100644 --- a/metric/internal/global/instruments_test.go +++ b/metric/internal/global/instruments_test.go @@ -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) } diff --git a/metric/internal/global/meter_test.go b/metric/internal/global/meter_test.go index 481c55e2273..447db967d84 100644 --- a/metric/internal/global/meter_test.go +++ b/metric/internal/global/meter_test.go @@ -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) } diff --git a/metric/internal/global/state_test.go b/metric/internal/global/state_test.go index 28b9ea1645a..0ef1f80f3a4 100644 --- a/metric/internal/global/state_test.go +++ b/metric/internal/global/state_test.go @@ -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) diff --git a/metric/nonrecording/meter.go b/metric/nonrecording/meter.go deleted file mode 100644 index 2acea49d8a1..00000000000 --- a/metric/nonrecording/meter.go +++ /dev/null @@ -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 -} diff --git a/metric/nonrecording/instruments.go b/metric/noop.go similarity index 57% rename from metric/nonrecording/instruments.go rename to metric/noop.go index 84a6aa89c31..2378fbb6334 100644 --- a/metric/nonrecording/instruments.go +++ b/metric/noop.go @@ -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,40 @@ 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{} + +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 +} + type nonrecordingAsyncFloat64Instrument struct { instrument.Asynchronous } @@ -36,15 +70,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 +97,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 +123,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 +154,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 } diff --git a/metric/noop_test.go b/metric/noop_test.go new file mode 100644 index 00000000000..4603cfce57b --- /dev/null +++ b/metric/noop_test.go @@ -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")) + }) +} diff --git a/sdk/metric/correct_test.go b/sdk/metric/correct_test.go index 3d18fc06655..4d63ccdc3f7 100644 --- a/sdk/metric/correct_test.go +++ b/sdk/metric/correct_test.go @@ -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(