Skip to content

Commit

Permalink
Fixes the instrument kind for noop async instruments
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Dec 16, 2021
1 parent 4654d78 commit 652b9d3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Import path changed `import "go.opentelemetry.io/otel/sdk/export/metric"` to `import go.opentelemetry.io/otel/sdk/metric/export` (#2382).
- Deprecate `AtomicFieldOffsets`, unnecessary public func (#2445)

### Fixed

- Fixes the instrument kind for noop async instruments (#2461)

## [1.3.0] - 2021-12-10

### ⚠️ Notice ⚠️
Expand Down
11 changes: 11 additions & 0 deletions internal/metric/registry/registry_test.go
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/internal/metric/registry"
Expand Down Expand Up @@ -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)
}
24 changes: 19 additions & 5 deletions metric/sdkapi/noop.go
Expand Up @@ -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 }

Expand All @@ -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) {
Expand Down

0 comments on commit 652b9d3

Please sign in to comment.