From 5f694b2e3eac01ba81615ff14dac3fe3b3a137f3 Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Sun, 11 Oct 2020 11:46:29 -0700 Subject: [PATCH] Move asyncronous metrics state helper out of apimetric/metrictes (#1234) --- api/metric/metrictest/meter.go | 5 +++-- .../metrictest => internal/metric}/async.go | 22 +++++++++---------- sdk/metric/sdk.go | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) rename {api/metric/metrictest => internal/metric}/async.go (88%) diff --git a/api/metric/metrictest/meter.go b/api/metric/metrictest/meter.go index 40524cac33c..fb0a817d1ef 100644 --- a/api/metric/metrictest/meter.go +++ b/api/metric/metrictest/meter.go @@ -21,6 +21,7 @@ import ( "go.opentelemetry.io/otel/api/metric" apimetric "go.opentelemetry.io/otel/api/metric" "go.opentelemetry.io/otel/api/metric/registry" + internalmetric "go.opentelemetry.io/otel/internal/metric" "go.opentelemetry.io/otel/label" ) @@ -44,7 +45,7 @@ type ( MeasurementBatches []Batch - asyncInstruments *AsyncInstrumentState + asyncInstruments *internalmetric.AsyncInstrumentState } Measurement struct { @@ -115,7 +116,7 @@ func (m *MeterImpl) doRecordSingle(ctx context.Context, labels []label.KeyValue, func NewMeterProvider() (*MeterImpl, apimetric.MeterProvider) { impl := &MeterImpl{ - asyncInstruments: NewAsyncInstrumentState(), + asyncInstruments: internalmetric.NewAsyncInstrumentState(), } return impl, registry.NewMeterProvider(impl) } diff --git a/api/metric/metrictest/async.go b/internal/metric/async.go similarity index 88% rename from api/metric/metrictest/async.go rename to internal/metric/async.go index cd908f2e2b0..c2096f8c96f 100644 --- a/api/metric/metrictest/async.go +++ b/internal/metric/async.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package metrictest +package metric import ( "context" @@ -21,7 +21,7 @@ import ( "sync" "go.opentelemetry.io/otel/api/global" - "go.opentelemetry.io/otel/api/metric" + api "go.opentelemetry.io/otel/api/metric" "go.opentelemetry.io/otel/label" ) @@ -32,7 +32,7 @@ var ErrInvalidAsyncRunner = errors.New("unknown async runner type") // the SDK to provide support for running observer callbacks. type AsyncCollector interface { // CollectAsync passes a batch of observations to the MeterImpl. - CollectAsync(labels []label.KeyValue, observation ...metric.Observation) + CollectAsync(labels []label.KeyValue, observation ...api.Observation) } // AsyncInstrumentState manages an ordered set of asynchronous @@ -60,18 +60,18 @@ type AsyncInstrumentState struct { // instruments maintains the set of instruments in the order // they were registered. - instruments []metric.AsyncImpl + instruments []api.AsyncImpl } // asyncRunnerPair is a map entry for Observer callback runners. type asyncRunnerPair struct { // runner is used as a map key here. The API ensures // that all callbacks are pointers for this reason. - runner metric.AsyncRunner + runner api.AsyncRunner // inst refers to a non-nil instrument when `runner` is a // AsyncSingleRunner. - inst metric.AsyncImpl + inst api.AsyncImpl } // NewAsyncInstrumentState returns a new *AsyncInstrumentState, for @@ -86,7 +86,7 @@ func NewAsyncInstrumentState() *AsyncInstrumentState { // Instruments returns the asynchronous instruments managed by this // object, the set that should be checkpointed after observers are // run. -func (a *AsyncInstrumentState) Instruments() []metric.AsyncImpl { +func (a *AsyncInstrumentState) Instruments() []api.AsyncImpl { a.lock.Lock() defer a.lock.Unlock() return a.instruments @@ -96,7 +96,7 @@ func (a *AsyncInstrumentState) Instruments() []metric.AsyncImpl { // object. This should be called during NewAsyncInstrument() and // assumes that errors (e.g., duplicate registration) have already // been checked. -func (a *AsyncInstrumentState) Register(inst metric.AsyncImpl, runner metric.AsyncRunner) { +func (a *AsyncInstrumentState) Register(inst api.AsyncImpl, runner api.AsyncRunner) { a.lock.Lock() defer a.lock.Unlock() @@ -110,7 +110,7 @@ func (a *AsyncInstrumentState) Register(inst metric.AsyncImpl, runner metric.Asy rp := asyncRunnerPair{ runner: runner, } - if _, ok := runner.(metric.AsyncSingleRunner); ok { + if _, ok := runner.(api.AsyncSingleRunner); ok { rp.inst = inst } @@ -131,12 +131,12 @@ func (a *AsyncInstrumentState) Run(ctx context.Context, collector AsyncCollector // other implementations are possible because the // interface has un-exported methods. - if singleRunner, ok := rp.runner.(metric.AsyncSingleRunner); ok { + if singleRunner, ok := rp.runner.(api.AsyncSingleRunner); ok { singleRunner.Run(ctx, rp.inst, collector.CollectAsync) continue } - if multiRunner, ok := rp.runner.(metric.AsyncBatchRunner); ok { + if multiRunner, ok := rp.runner.(api.AsyncBatchRunner); ok { multiRunner.Run(ctx, collector.CollectAsync) continue } diff --git a/sdk/metric/sdk.go b/sdk/metric/sdk.go index 4e16f279828..50ee358e384 100644 --- a/sdk/metric/sdk.go +++ b/sdk/metric/sdk.go @@ -24,7 +24,7 @@ import ( "go.opentelemetry.io/otel/api/global" "go.opentelemetry.io/otel/api/metric" api "go.opentelemetry.io/otel/api/metric" - internal "go.opentelemetry.io/otel/api/metric/metrictest" + internal "go.opentelemetry.io/otel/internal/metric" "go.opentelemetry.io/otel/label" export "go.opentelemetry.io/otel/sdk/export/metric" "go.opentelemetry.io/otel/sdk/metric/aggregator"