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

Define the reader interface, and create a manual reader #2885

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
22 changes: 22 additions & 0 deletions sdk/metric/export/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

// TODO: NOTE this is a temporary space, it may be moved following the
// discussion of #2813, or #2841

package export // import "go.opentelemetry.io/otel/sdk/metric/export"

// Metrics is the result of a single collection.
type Metrics struct { /* TODO: implement #2889 */
}
5 changes: 4 additions & 1 deletion sdk/metric/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module go.opentelemetry.io/otel/sdk/metric

go 1.16

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

replace go.opentelemetry.io/otel => ../..

Expand Down
1 change: 1 addition & 0 deletions sdk/metric/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
86 changes: 86 additions & 0 deletions sdk/metric/manual_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 "go.opentelemetry.io/otel/sdk/metric"

import (
"context"
"fmt"
"sync"

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

// ManualReader is a a simple Reader that allows an application to
// read metrics on demand. It simply stores the Producer interface
// provided through registration. Flush and Shutdown are no-ops.
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
type ManualReader struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is odd to me that we are including the "manual reader" and not the periodic reader in this PR. Can this be added in a subsequent PR after we agree on the Reader design?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have gotten to an impasse on "Just Design" and I think either an implementation to demonstrate how this would work, or some logic in the MetricProducer of how this is used is warranted.

I for one have already seen the use case of the Shutdown method and will have to work to add that code into this.

But I still don't see how ForceFlush works in these implementations. This leads me to believe that I don't think we should be designing these in the absence of some use case, either the minimal implementation or where it is used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to understand this concern about Shutdown and ForceFlush. They way I understand both of them, both method calls pass-through to each of the readers (i.e., exporters). If a reader (i.e., exporter) has nothing applicable to do for flush or shutdown, they'll do nothing. The reason we want Reader, Producer, and the Produce() method to be public, is so that users and/or exporters can choose to perform manual reading simply by implementing the 1-line Register function plus custom Shutdown and ForceFlush logic.

lock sync.Mutex
producer producer
shutdown bool
}

var _ Reader = &ManualReader{}
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

// NewManualReader returns an Reader that stores the Producer for
// manual use and returns a configurable `name` as its String(),
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
func NewManualReader() *ManualReader {
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
return &ManualReader{}
}

// Register stores the Producer which enables the caller to read
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
// metrics on demand.
func (mr *ManualReader) register(p producer) {
mr.lock.Lock()
defer mr.lock.Unlock()
mr.producer = p
}

// ForceFlush is a no-op, always returns nil.
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
func (mr *ManualReader) ForceFlush(context.Context) error {
return nil
}

// Shutdown closes any connections and frees any resources used by the reader.
func (mr *ManualReader) Shutdown(context.Context) error {
mr.lock.Lock()
defer mr.lock.Unlock()
if mr.shutdown {
return ErrReaderShutdown
}
mr.shutdown = true
return nil
}

// Collect gathers all metrics from the SDK, calling any callbacks necessary.
// Collect will return an error if called after shutdown.
func (mr *ManualReader) Collect(ctx context.Context) (export.Metrics, error) {
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
mr.lock.Lock()
defer mr.lock.Unlock()
if mr.producer == nil {
return export.Metrics{}, ErrReaderNotRegistered
}
if mr.shutdown {
return export.Metrics{}, ErrReaderShutdown
}
return mr.producer.produce(ctx)
}

// ErrReaderNotRegistered is returned if Collect or Shutdown are called before
// the reader is registered with a MeterProvider
var ErrReaderNotRegistered = fmt.Errorf("reader is not registered")
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

// ErrReaderShutdown is returned if Collect or Shutdown are called after a
// reader has been Shutdown once.
var ErrReaderShutdown = fmt.Errorf("reader is shutdown")
72 changes: 72 additions & 0 deletions sdk/metric/manual_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 "go.opentelemetry.io/otel/sdk/metric/reader"

import (
"context"
"testing"

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

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

func TestManualReaderNotRegistered(t *testing.T) {
rdr := &ManualReader{}

_, err := rdr.Collect(context.Background())
require.ErrorIs(t, err, ErrReaderNotRegistered)
}

type testProducer struct{}

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

func (p testProducer) produce(context.Context) (export.Metrics, error) {
return testMetrics, nil
}

func TestManualReaderProducer(t *testing.T) {
rdr := &ManualReader{}
rdr.register(testProducer{})

m, err := rdr.Collect(context.Background())
assert.NoError(t, err)
assert.Equal(t, testMetrics, m)
}

func TestManualReaderCollectAfterShutdown(t *testing.T) {
rdr := &ManualReader{}
rdr.register(testProducer{})
_ = rdr.Shutdown(context.Background())
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

m, err := rdr.Collect(context.Background())
assert.Error(t, err)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, export.Metrics{}, m)
}
func TestManualReaderShutdown(t *testing.T) {
rdr := &ManualReader{}
rdr.register(testProducer{})

err := rdr.Shutdown(context.Background())
assert.NoError(t, err)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

err = rdr.Shutdown(context.Background())
assert.Error(t, err)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

}
75 changes: 75 additions & 0 deletions sdk/metric/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 "go.opentelemetry.io/otel/sdk/metric"

import (
"context"

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

// 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
// while the initiates collection. The Register() method here
// informs the Reader that it can begin reading, signaling the
// start of bi-directional control flow.
//
// Typically, push-based exporters that are periodic will
// implement PeroidicExporter themselves and construct a
// PeriodicReader to satisfy this interface.
//
// Pull-based exporters will typically implement Register
// themselves, since they read on demand.
jmacd marked this conversation as resolved.
Show resolved Hide resolved
type Reader interface {
// register registers a Reader with a MeterProvider.
// The producer argument allows the Reader to signal
// back to the MeterProvider it is registered with to collect
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
// and send aggregated metric measurements.
register(producer)
jmacd marked this conversation as resolved.
Show resolved Hide resolved

// Collect gathers all metrics from the SDK, calling any callbacks necessary.
// TODO: How does this impact Push exporters?
// Collect will return an error if called after shutdown.
Collect(context.Context) (export.Metrics, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing documentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed, it's not needed if the Register method, Producer type, and Produce method are exposed. When these are exposed, the ManualReader is only needed for simple applications; anything that wants manual reading behavior and also to control flush/shutdown has the option to manually implement the reader themselves, it's very simple.

In my branch, Collect() is an interface method that the (async/sync)x(cumulative/delta) instrument objects implement, used by the producer during Produce()

// Collector is an interface for things that produce Instrument data.
// One instrument may output more than one Instrument data by
// appending to `output`.
type Collector interface {
	Collect(sequence Sequence, output *[]Instrument)
}

In my branch I named what is export here as the package sdk/metric/data, so ^^^ is the data.Collector interface and Collect() collects is how one API-level instrument outputs multiple View-level metric data objects. The user would not use this, it's between the *meter and the internal packages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// ForceFlush flushes all metric measurements held in an export pipeline.
//
// This deadline or cancellation of the passed context are honored. An appropriate
// error will be returned in these situations. There is no guaranteed that all
// telemetry be flushed or all resources have been released in these
// situations.
ForceFlush(context.Context) error

// Shutdown flushes all metric measurements held in an export pipeline and releases any
// held computational resources.
//
// This deadline or cancellation of the passed context are honored. An appropriate
// error will be returned in these situations. There is no guaranteed that all
// telemetry be flushed or all resources have been released in these
// situations.
//
// After Shutdown is called, calls to Collect will perform no operation and instead will return
// an error indicating the shutdown state.
Shutdown(context.Context) error
}

// producer produces metrics for a Reader.
type producer interface {
// produce returns aggregated metrics from a single collection.
//
// This method is safe to call concurrently.
produce(context.Context) (export.Metrics, error)
}