From 395c0317de0febe0933dd8b31258ca97563acde0 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Thu, 16 Dec 2021 11:35:55 -0800 Subject: [PATCH] Fixes the instrument kind for noop async instruments Signed-off-by: Bogdan Drutu --- internal/metric/registry/registry_test.go | 11 +++++++++++ metric/sdkapi/noop.go | 24 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/internal/metric/registry/registry_test.go b/internal/metric/registry/registry_test.go index 04884898822..05cbeddf316 100644 --- a/internal/metric/registry/registry_test.go +++ b/internal/metric/registry/registry_test.go @@ -19,6 +19,7 @@ import ( "errors" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/internal/metric/registry" @@ -125,3 +126,13 @@ func TestRegistryDiffInstruments(t *testing.T) { } } } + +func TestDoubleNewInstrument(t *testing.T) { + reg := registry.NewUniqueInstrumentMeterImpl( + metrictest.NewMeterProvider().Meter("meter").MeterImpl(), + ) + _, err := reg.NewAsyncInstrument(sdkapi.NewNoopAsyncInstrument().Descriptor(), nil) + require.NoError(t, err) + _, err = reg.NewSyncInstrument(sdkapi.NewNoopSyncInstrument().Descriptor()) + assert.Error(t, err) +} diff --git a/metric/sdkapi/noop.go b/metric/sdkapi/noop.go index 986f5efec45..f22895dae6f 100644 --- a/metric/sdkapi/noop.go +++ b/metric/sdkapi/noop.go @@ -21,7 +21,9 @@ import ( "go.opentelemetry.io/otel/metric/number" ) -type noopInstrument struct{} +type noopInstrument struct { + descriptor Descriptor +} type noopSyncInstrument struct{ noopInstrument } type noopAsyncInstrument struct{ noopInstrument } @@ -31,21 +33,33 @@ var _ AsyncImpl = noopAsyncInstrument{} // NewNoopSyncInstrument returns a No-op implementation of the // synchronous instrument interface. func NewNoopSyncInstrument() SyncImpl { - return noopSyncInstrument{} + return noopSyncInstrument{ + noopInstrument{ + descriptor: Descriptor{ + instrumentKind: CounterInstrumentKind, + }, + }, + } } // NewNoopAsyncInstrument returns a No-op implementation of the // asynchronous instrument interface. func NewNoopAsyncInstrument() AsyncImpl { - return noopAsyncInstrument{} + return noopAsyncInstrument{ + noopInstrument{ + descriptor: Descriptor{ + instrumentKind: CounterObserverInstrumentKind, + }, + }, + } } func (noopInstrument) Implementation() interface{} { return nil } -func (noopInstrument) Descriptor() Descriptor { - return Descriptor{} +func (n noopInstrument) Descriptor() Descriptor { + return n.descriptor } func (noopSyncInstrument) RecordOne(context.Context, number.Number, []attribute.KeyValue) {