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

Remove the unneeded Observe method from the async instruments #3586

Merged
merged 22 commits into from
Jan 25, 2023
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
Prev Previous commit
Next Next commit
Merge branch 'main' into rm-observe-method
  • Loading branch information
MrAlias committed Jan 19, 2023
commit 319353190e675658cab770f4b02dee55fec8831e
4 changes: 2 additions & 2 deletions example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func main() {
if err != nil {
log.Fatal(err)
}
_, err = meter.RegisterCallback(func(_ context.Context, o api.MultiObserver) error {
_, err = meter.RegisterCallback(func(_ context.Context, o api.Observer) error {
n := -10. + rand.Float64()*(90.) // [-10, 100)
o.Float64(gauge, n, attrs...)
o.ObserveFloat64(gauge, n, attrs...)
return nil
}, gauge)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func ExampleMeter_asynchronous_multiple() {
gcPause, _ := meter.Float64Histogram("gcPause")

_, err := meter.RegisterCallback(
func(ctx context.Context, o metric.MultiObserver) error {
func(ctx context.Context, o metric.Observer) error {
memStats := &runtime.MemStats{}
// This call does work
runtime.ReadMemStats(memStats)

o.Int64(heapAlloc, int64(memStats.HeapAlloc))
o.Int64(gcCount, int64(memStats.NumGC))
o.ObserveInt64(heapAlloc, int64(memStats.HeapAlloc))
o.ObserveInt64(gcCount, int64(memStats.NumGC))

// This function synchronously records the pauses
computeGCPauses(ctx, gcPause, memStats.PauseNs[:])
Expand Down
15 changes: 9 additions & 6 deletions metric/internal/global/instruments.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (i *afCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *afCounter) unwrap() instrument.Asynchronous {
func (i *afCounter) Unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Float64ObservableCounter)
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func (i *afUpDownCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *afUpDownCounter) unwrap() instrument.Asynchronous {
func (i *afUpDownCounter) Unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Float64ObservableUpDownCounter)
}
Expand All @@ -94,6 +94,9 @@ type afGauge struct {
delegate atomic.Value //instrument.Float64ObservableGauge
}

var _ unwrapper = (*afGauge)(nil)
var _ instrument.Float64ObservableGauge = (*afGauge)(nil)

func (i *afGauge) setDelegate(m metric.Meter) {
ctr, err := m.Float64ObservableGauge(i.name, i.opts...)
if err != nil {
Expand All @@ -103,7 +106,7 @@ func (i *afGauge) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *afGauge) unwrap() instrument.Asynchronous {
func (i *afGauge) Unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Float64ObservableGauge)
}
Expand Down Expand Up @@ -131,7 +134,7 @@ func (i *aiCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *aiCounter) unwrap() instrument.Asynchronous {
func (i *aiCounter) Unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Int64ObservableCounter)
}
Expand Down Expand Up @@ -159,7 +162,7 @@ func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *aiUpDownCounter) unwrap() instrument.Asynchronous {
func (i *aiUpDownCounter) Unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Int64ObservableUpDownCounter)
}
Expand Down Expand Up @@ -187,7 +190,7 @@ func (i *aiGauge) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *aiGauge) unwrap() instrument.Asynchronous {
func (i *aiGauge) Unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Int64ObservableGauge)
}
Expand Down
10 changes: 5 additions & 5 deletions metric/internal/global/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestMeterProviderRace(t *testing.T) {
close(finish)
}

var zeroCallback metric.Callback = func(ctx context.Context, or metric.MultiObserver) error {
var zeroCallback metric.Callback = func(ctx context.Context, or metric.Observer) error {
return nil
}

Expand Down Expand Up @@ -132,8 +132,8 @@ func testSetupAllInstrumentTypes(t *testing.T, m metric.Meter) (instrument.Float
_, err = m.Int64ObservableGauge("test_Async_Gauge")
assert.NoError(t, err)

_, err = m.RegisterCallback(func(ctx context.Context, obs metric.MultiObserver) error {
obs.Float64(afcounter, 3)
_, err = m.RegisterCallback(func(ctx context.Context, obs metric.Observer) error {
obs.ObserveFloat64(afcounter, 3)
return nil
}, afcounter)
require.NoError(t, err)
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestRegistrationDelegation(t *testing.T) {
require.NoError(t, err)

var called0 bool
reg0, err := m.RegisterCallback(func(context.Context, metric.MultiObserver) error {
reg0, err := m.RegisterCallback(func(context.Context, metric.Observer) error {
called0 = true
return nil
}, actr)
Expand All @@ -338,7 +338,7 @@ func TestRegistrationDelegation(t *testing.T) {
assert.Equal(t, 0, mImpl.registry.Len(), "callback not unregistered")

var called1 bool
reg1, err := m.RegisterCallback(func(context.Context, metric.MultiObserver) error {
reg1, err := m.RegisterCallback(func(context.Context, metric.Observer) error {
called1 = true
return nil
}, actr)
Expand Down
16 changes: 2 additions & 14 deletions metric/internal/global/meter_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,16 @@ type observationRecorder struct {
ctx context.Context
}

func (o observationRecorder) Float64(i instrument.Float64Observable, value float64, attr ...attribute.KeyValue) {
func (o observationRecorder) ObserveFloat64(i instrument.Float64Observable, value float64, attr ...attribute.KeyValue) {
iImpl, ok := i.(*testCountingFloatInstrument)
if ok {
iImpl.observe()
}
}

func (o observationRecorder) Int64(i instrument.Int64Observable, value int64, attr ...attribute.KeyValue) {
func (o observationRecorder) ObserveInt64(i instrument.Int64Observable, value int64, attr ...attribute.KeyValue) {
iImpl, ok := i.(*testCountingIntInstrument)
if ok {
iImpl.observe()
}
}

type observationRecorder struct {
ctx context.Context
}

func (o observationRecorder) ObserveFloat64(i instrument.Float64Observer, value float64, attr ...attribute.KeyValue) {
i.Observe(o.ctx, value, attr...)
}

func (o observationRecorder) ObserveInt64(i instrument.Int64Observer, value int64, attr ...attribute.KeyValue) {
i.Observe(o.ctx, value, attr...)
}
18 changes: 9 additions & 9 deletions metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ type Meter interface {
}

// Callback is a function registered with a Meter that makes observations for
// the set of instruments it is registered with. The MultiObserver parameter is
// used to record measurment observations for these instruments.
// the set of instruments it is registered with. The Observer parameter is used
// to record measurment observations for these instruments.
//
// The function needs to complete in a finite amount of time and the deadline
// of the passed context is expected to be honored.
Expand All @@ -118,14 +118,14 @@ type Meter interface {
// the same attributes as another Callback will report.
//
// The function needs to be concurrent safe.
type Callback func(context.Context, MultiObserver) error
type Callback func(context.Context, Observer) error

// MultiObserver records measurements for multiple instruments in a Callback.
type MultiObserver interface {
// Float64 records the float64 value with attributes for obsrv.
Float64(obsrv instrument.Float64Observable, value float64, attributes ...attribute.KeyValue)
// Int64 records the int64 value with attributes for obsrv.
Int64(obsrv instrument.Int64Observable, value int64, attributes ...attribute.KeyValue)
// Observer records measurements for multiple instruments in a Callback.
type Observer interface {
// ObserveFloat64 records the float64 value with attributes for obsrv.
ObserveFloat64(obsrv instrument.Float64Observable, value float64, attributes ...attribute.KeyValue)
// ObserveInt64 records the int64 value with attributes for obsrv.
ObserveInt64(obsrv instrument.Int64Observable, value int64, attributes ...attribute.KeyValue)
}

// Registration is an token representing the unique registration of a callback
Expand Down
20 changes: 12 additions & 8 deletions sdk/metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/instrumentation"
Expand Down Expand Up @@ -202,8 +201,8 @@ func (i *instrumentImpl[N]) aggregate(ctx context.Context, val N, attrs []attrib
}
}

// observerID is a comparable unique identifier of an observer.
type observerID[N int64 | float64] struct {
// observablID is a comparable unique identifier of an observable.
type observablID[N int64 | float64] struct {
name string
description string
kind InstrumentKind
Expand Down Expand Up @@ -242,14 +241,15 @@ func newInt64Observable(scope instrumentation.Scope, kind InstrumentKind, name,
}

type observable[N int64 | float64] struct {
observerID[N]
instrument.Asynchronous
observablID[N]

aggregators []internal.Aggregator[N]
}

func newObservable[N int64 | float64](scope instrumentation.Scope, kind InstrumentKind, name, desc string, u unit.Unit, agg []internal.Aggregator[N]) *observable[N] {
return &observable[N]{
observerID: observerID[N]{
observablID: observablID[N]{
name: name,
description: desc,
kind: kind,
Expand All @@ -266,18 +266,22 @@ func (o *observable[N]) observe(val N, attrs []attribute.KeyValue) {
}
}

var errEmptyAgg = errors.New("no aggregators for observer")
var errEmptyAgg = errors.New("no aggregators for observable instrument")

// registerable returns an error if the observable o should not be registered,
// and nil if it should. An errEmptyAgg error is returned if o is effecively a
// no-op because it does not have any aggregators. Also, an error is returned
// if scope defines a Meter other than the one o was created by.
func (o *observable[N]) registerable(scope instrumentation.Scope) error {
if len(o.aggregators) == 0 {
return errEmptyAgg
}
if scope != o.scope {
return fmt.Errorf(
"invalid registration: observer %q from Meter %q, registered with Meter %q",
"invalid registration: observable %q from Meter %q, registered with Meter %q",
o.name,
scope.Name,
o.scope.Name,
scope.Name,
)
}
return nil
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.