Skip to content

Commit

Permalink
Move metric no-op implementation to metric package
Browse files Browse the repository at this point in the history
This is to be consistent with trace package.

Fixes: #2767

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Apr 27, 2022
1 parent 776accd commit c66ceb8
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 90 deletions.
8 changes: 4 additions & 4 deletions metric/example_test.go
Expand Up @@ -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",
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions metric/internal/global/instruments_test.go
Expand Up @@ -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)) {
Expand All @@ -37,7 +36,7 @@ func testFloat64Race(interact func(context.Context, float64, ...attribute.KeyVal
}
}()

setDelegate(nonrecording.NewNoopMeter())
setDelegate(metric.NewNoopMeter())
close(finish)
}

Expand All @@ -54,7 +53,7 @@ func testInt64Race(interact func(context.Context, int64, ...attribute.KeyValue),
}
}()

setDelegate(nonrecording.NewNoopMeter())
setDelegate(metric.NewNoopMeter())
close(finish)
}

Expand Down
5 changes: 2 additions & 3 deletions metric/internal/global/meter_test.go
Expand Up @@ -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) {
Expand All @@ -44,7 +43,7 @@ func TestMeterProviderRace(t *testing.T) {
}
}()

mp.setDelegate(nonrecording.NewNoopMeterProvider())
mp.setDelegate(metric.NewNoopMeterProvider())
close(finish)

}
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestMeterRace(t *testing.T) {
}()

wg.Wait()
mtr.setDelegate(nonrecording.NewNoopMeterProvider())
mtr.setDelegate(metric.NewNoopMeterProvider())
close(finish)
}

Expand Down
5 changes: 2 additions & 3 deletions metric/internal/global/state_test.go
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/nonrecording"
)

func resetGlobalMeterProvider() {
Expand Down Expand Up @@ -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 {
Expand All @@ -68,7 +67,7 @@ func TestSetMeterProvider(t *testing.T) {

mp := MeterProvider()

SetMeterProvider(nonrecording.NewNoopMeterProvider())
SetMeterProvider(metric.NewNoopMeterProvider())

dmp := mp.(*meterProvider)

Expand Down
64 changes: 0 additions & 64 deletions metric/nonrecording/meter.go

This file was deleted.

60 changes: 47 additions & 13 deletions metric/nonrecording/instruments.go → metric/noop.go
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit c66ceb8

Please sign in to comment.