Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test duplicate Reader registration #2914

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/metric/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.6.3
go.opentelemetry.io/otel/metric v0.0.0-00010101000000-000000000000
)

Expand Down
2 changes: 2 additions & 0 deletions sdk/metric/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
Expand Down
6 changes: 6 additions & 0 deletions sdk/metric/manual_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"sync"

"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/sdk/metric/export"
)

Expand All @@ -43,6 +44,11 @@ func NewManualReader() Reader {
func (mr *manualReader) register(p producer) {
mr.lock.Lock()
defer mr.lock.Unlock()
if mr.producer != nil {
jmacd marked this conversation as resolved.
Show resolved Hide resolved
msg := "did not register manualReader"
global.Error(errDuplicateRegister, msg)
return
}
mr.producer = p
}

Expand Down
5 changes: 5 additions & 0 deletions sdk/metric/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric"

import (
"context"
"fmt"

"go.opentelemetry.io/otel/sdk/metric/export"
)

// errDuplicateRegister is logged by a Reader when an attempt to registered it
// more than once occurs.
var errDuplicateRegister = fmt.Errorf("duplicate reader registration")

// Reader is the interface used between the SDK and an
// exporter. Control flow is bi-directional through the
// Reader, since the SDK initiates ForceFlush and Shutdown
Expand Down
32 changes: 30 additions & 2 deletions sdk/metric/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,42 @@ func testReaderHarness(t *testing.T, f readerFactory) {
// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(ctx)
})

t.Run("MultipleRegister", func(t *testing.T) {
p0 := testProducer{
produceFunc: func(ctx context.Context) (export.Metrics, error) {
// Differentiate this producer from the second by returning an
// error.
return testMetrics, assert.AnError
},
}
p1 := testProducer{}

r := f()
r.register(p0)
// This should be ignored.
r.register(p1)

ctx := context.Background()
_, err := r.Collect(ctx)
assert.Equal(t, assert.AnError, err)

// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(ctx)
})
}

var testMetrics = export.Metrics{
// TODO: test with actual data.
}

type testProducer struct{}
type testProducer struct {
produceFunc func(context.Context) (export.Metrics, error)
}

func (p testProducer) produce(context.Context) (export.Metrics, error) {
func (p testProducer) produce(ctx context.Context) (export.Metrics, error) {
if p.produceFunc != nil {
return p.produceFunc(ctx)
}
return testMetrics, nil
}