Skip to content

Commit

Permalink
Remove the Observe method from async inst
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jan 11, 2023
1 parent d0947fb commit f576781
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 337 deletions.
4 changes: 2 additions & 2 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ExampleMeter_asynchronous_single() {
_, err := meter.Int64ObservableGauge(
"DiskUsage",
instrument.WithUnit(unit.Bytes),
instrument.WithInt64Callback(func(ctx context.Context, inst instrument.Int64Observer) error {
instrument.WithInt64Callback(func(_ context.Context, observe instrument.Int64Observer) error {
// Do the real work here to get the real disk usage. For example,
//
// usage, err := GetDiskUsage(diskID)
Expand All @@ -69,7 +69,7 @@ func ExampleMeter_asynchronous_single() {
//
// For demonstration purpose, a static value is used here.
usage := 75000
inst.Observe(ctx, int64(usage), attribute.Int("disk.id", 3))
observe(int64(usage), attribute.Int("disk.id", 3))
return nil
}),
)
Expand Down
43 changes: 23 additions & 20 deletions metric/instrument/asyncfloat64.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,48 @@ import (
"go.opentelemetry.io/otel/metric/unit"
)

// Float64Observer is a recorder of float64 measurement values.
// Float64ObservableCounter describes a set of instruments used asynchronously
// to record float64 measurements once per a measurement collection cycle.
// Observations of these instruments are only made within a callback for this
// instrument.
//
// Warning: methods may be added to this interface in minor releases.
type Float64Observer interface {
type Float64Observable interface {
Asynchronous

// Observe records the measurement value for a set of attributes.
//
// It is only valid to call this within a callback. If called outside of
// the registered callback it should have no effect on the instrument, and
// an error will be reported via the error handler.
Observe(ctx context.Context, value float64, attributes ...attribute.KeyValue)
float64Observable()
}

// Float64ObservableCounter is an instrument used to asynchronously record
// increasing float64 measurements once per a measurement collection cycle. The
// Observe method is used to record the measured state of the instrument when
// it is called. Implementations will assume the observed value to be the
// cumulative sum of the count.
// increasing float64 measurements once per a measurement collection cycle.
// Observations are only made within a callback for this instrument. The value
// observed is assumed the to be the cumulative sum of the count.
//
// Warning: methods may be added to this interface in minor releases.
type Float64ObservableCounter interface{ Float64Observer }
type Float64ObservableCounter interface{ Float64Observable }

// Float64ObservableUpDownCounter is an instrument used to asynchronously
// record float64 measurements once per a measurement collection cycle. The
// Observe method is used to record the measured state of the instrument when
// it is called. Implementations will assume the observed value to be the
// cumulative sum of the count.
// record float64 measurements once per a measurement collection cycle.
// Observations are only made within a callback for this instrument. The value
// observed is assumed the to be the cumulative sum of the count.
//
// Warning: methods may be added to this interface in minor releases.
type Float64ObservableUpDownCounter interface{ Float64Observer }
type Float64ObservableUpDownCounter interface{ Float64Observable }

// Float64ObservableGauge is an instrument used to asynchronously record
// instantaneous float64 measurements once per a measurement collection cycle.
// Observations are only made within a callback for this instrument.
//
// Warning: methods may be added to this interface in minor releases.
type Float64ObservableGauge interface{ Float64Observer }
type Float64ObservableGauge interface{ Float64Observable }

// Float64Observer records float64 measurement values for a set of attributes.
type Float64Observer func(value float64, attributes ...attribute.KeyValue)

// Float64Callback is a function registered with a Meter that makes
// observations for a Float64Observer it is registered with.
// observations for a Float64Observerable instrument it is registered with.
// Calls to the Float64Observer record measurement values for the
// Float64Observable.
//
// The function needs to complete in a finite amount of time and the deadline
// of the passed context is expected to be honored.
Expand Down
22 changes: 8 additions & 14 deletions metric/instrument/asyncfloat64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestFloat64ObserverOptions(t *testing.T) {
got := NewFloat64ObserverConfig(
WithDescription(desc),
WithUnit(uBytes),
WithFloat64Callback(func(ctx context.Context, o Float64Observer) error {
o.Observe(ctx, token)
WithFloat64Callback(func(ctx context.Context, observe Float64Observer) error {
observe(token)
return nil
}),
)
Expand All @@ -46,17 +46,11 @@ func TestFloat64ObserverOptions(t *testing.T) {
// Functions are not comparable.
cBacks := got.Callbacks()
require.Len(t, cBacks, 1, "callbacks")
o := &float64Observer{}
err := cBacks[0](context.Background(), o)
var gotV float64
err := cBacks[0](
context.Background(),
func(v float64, _ ...attribute.KeyValue) { gotV = v },
)
require.NoError(t, err)
assert.Equal(t, token, o.got, "callback not set")
}

type float64Observer struct {
Asynchronous
got float64
}

func (o *float64Observer) Observe(_ context.Context, v float64, _ ...attribute.KeyValue) {
o.got = v
assert.Equal(t, token, gotV, "callback not set")
}
44 changes: 24 additions & 20 deletions metric/instrument/asyncint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,50 @@ import (
"go.opentelemetry.io/otel/metric/unit"
)

// Int64Observer is a recorder of int64 measurement values.
// Int64ObservableCounter describes a set of instruments used asynchronously
// to record int64 measurements once per a measurement collection cycle.
// Observations of these instruments are only made within a callback for this
// instrument.
//
// Warning: methods may be added to this interface in minor releases.
type Int64Observer interface {
type Int64Observable interface {
Asynchronous

// Observe records the measurement value for a set of attributes.
//
// It is only valid to call this within a callback. If called outside of
// the registered callback it should have no effect on the instrument, and
// an error will be reported via the error handler.
Observe(ctx context.Context, value int64, attributes ...attribute.KeyValue)
int64Observable()
}

// Int64ObservableCounter is an instrument used to asynchronously record
// increasing int64 measurements once per a measurement collection cycle. The
// Observe method is used to record the measured state of the instrument when
// it is called. Implementations will assume the observed value to be the
// cumulative sum of the count.
// increasing int64 measurements once per a measurement collection cycle.
// Observations are only made within a callback for this instrument. The value
// observed is assumed the to be the cumulative sum of the count.
//
// Warning: methods may be added to this interface in minor releases.
type Int64ObservableCounter interface{ Int64Observer }
type Int64ObservableCounter interface{ Int64Observable }

// Int64ObservableUpDownCounter is an instrument used to asynchronously record
// int64 measurements once per a measurement collection cycle. The Observe
// method is used to record the measured state of the instrument when it is
// called. Implementations will assume the observed value to be the cumulative
// sum of the count.
// int64 measurements once per a measurement collection cycle. Observations are
// only made within a callback for this instrument. The value observed is
// assumed the to be the cumulative sum of the count.
//
// Warning: methods may be added to this interface in minor releases.
type Int64ObservableUpDownCounter interface{ Int64Observer }
type Int64ObservableUpDownCounter interface{ Int64Observable }

// Int64ObservableGauge is an instrument used to asynchronously record
// instantaneous int64 measurements once per a measurement collection cycle.
// Observations are only made within a callback for this instrument.
//
// Warning: methods may be added to this interface in minor releases.
type Int64ObservableGauge interface{ Int64Observable }

// Int64Observer is a recorder of int64 measurement values.
//
// Warning: methods may be added to this interface in minor releases.
type Int64ObservableGauge interface{ Int64Observer }
type Int64Observer func(value int64, attributes ...attribute.KeyValue)

// Int64Callback is a function registered with a Meter that makes
// observations for an Int64Observer it is registered with.
// observations for a Int64Observerable instrument it is registered with.
// Calls to the Int64Observer record measurement values for the
// Int64Observable.
//
// The function needs to complete in a finite amount of time and the deadline
// of the passed context is expected to be honored.
Expand Down
22 changes: 8 additions & 14 deletions metric/instrument/asyncint64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestInt64ObserverOptions(t *testing.T) {
got := NewInt64ObserverConfig(
WithDescription(desc),
WithUnit(uBytes),
WithInt64Callback(func(ctx context.Context, o Int64Observer) error {
o.Observe(ctx, token)
WithInt64Callback(func(_ context.Context, observe Int64Observer) error {
observe(token)
return nil
}),
)
Expand All @@ -46,17 +46,11 @@ func TestInt64ObserverOptions(t *testing.T) {
// Functions are not comparable.
cBacks := got.Callbacks()
require.Len(t, cBacks, 1, "callbacks")
o := &int64Observer{}
err := cBacks[0](context.Background(), o)
var gotV int64
err := cBacks[0](
context.Background(),
func(v int64, _ ...attribute.KeyValue) { gotV = v },
)
require.NoError(t, err)
assert.Equal(t, token, o.got, "callback not set")
}

type int64Observer struct {
Asynchronous
got int64
}

func (o *int64Observer) Observe(_ context.Context, v int64, _ ...attribute.KeyValue) {
o.got = v
assert.Equal(t, token, gotV, "callback not set")
}
60 changes: 12 additions & 48 deletions metric/internal/global/instruments.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
)

type afCounter struct {
instrument.Float64Observable

name string
opts []instrument.Float64ObserverOption

delegate atomic.Value //instrument.Float64ObservableCounter

instrument.Asynchronous
}

func (i *afCounter) setDelegate(m metric.Meter) {
Expand All @@ -42,12 +42,6 @@ func (i *afCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *afCounter) Observe(ctx context.Context, x float64, attrs ...attribute.KeyValue) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(instrument.Float64ObservableCounter).Observe(ctx, x, attrs...)
}
}

func (i *afCounter) unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Float64ObservableCounter)
Expand All @@ -56,12 +50,12 @@ func (i *afCounter) unwrap() instrument.Asynchronous {
}

type afUpDownCounter struct {
instrument.Float64Observable

name string
opts []instrument.Float64ObserverOption

delegate atomic.Value //instrument.Float64ObservableUpDownCounter

instrument.Asynchronous
}

func (i *afUpDownCounter) setDelegate(m metric.Meter) {
Expand All @@ -73,12 +67,6 @@ func (i *afUpDownCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *afUpDownCounter) Observe(ctx context.Context, x float64, attrs ...attribute.KeyValue) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(instrument.Float64ObservableUpDownCounter).Observe(ctx, x, attrs...)
}
}

func (i *afUpDownCounter) unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Float64ObservableUpDownCounter)
Expand All @@ -87,12 +75,12 @@ func (i *afUpDownCounter) unwrap() instrument.Asynchronous {
}

type afGauge struct {
instrument.Float64Observable

name string
opts []instrument.Float64ObserverOption

delegate atomic.Value //instrument.Float64ObservableGauge

instrument.Asynchronous
}

func (i *afGauge) setDelegate(m metric.Meter) {
Expand All @@ -104,12 +92,6 @@ func (i *afGauge) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *afGauge) Observe(ctx context.Context, x float64, attrs ...attribute.KeyValue) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(instrument.Float64ObservableGauge).Observe(ctx, x, attrs...)
}
}

func (i *afGauge) unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Float64ObservableGauge)
Expand All @@ -118,12 +100,12 @@ func (i *afGauge) unwrap() instrument.Asynchronous {
}

type aiCounter struct {
instrument.Int64Observable

name string
opts []instrument.Int64ObserverOption

delegate atomic.Value //instrument.Int64ObservableCounter

instrument.Asynchronous
}

func (i *aiCounter) setDelegate(m metric.Meter) {
Expand All @@ -135,12 +117,6 @@ func (i *aiCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *aiCounter) Observe(ctx context.Context, x int64, attrs ...attribute.KeyValue) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(instrument.Int64ObservableCounter).Observe(ctx, x, attrs...)
}
}

func (i *aiCounter) unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Int64ObservableCounter)
Expand All @@ -149,12 +125,12 @@ func (i *aiCounter) unwrap() instrument.Asynchronous {
}

type aiUpDownCounter struct {
instrument.Int64Observable

name string
opts []instrument.Int64ObserverOption

delegate atomic.Value //instrument.Int64ObservableUpDownCounter

instrument.Asynchronous
}

func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
Expand All @@ -166,12 +142,6 @@ func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *aiUpDownCounter) Observe(ctx context.Context, x int64, attrs ...attribute.KeyValue) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(instrument.Int64ObservableUpDownCounter).Observe(ctx, x, attrs...)
}
}

func (i *aiUpDownCounter) unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Int64ObservableUpDownCounter)
Expand All @@ -180,12 +150,12 @@ func (i *aiUpDownCounter) unwrap() instrument.Asynchronous {
}

type aiGauge struct {
instrument.Int64Observable

name string
opts []instrument.Int64ObserverOption

delegate atomic.Value //instrument.Int64ObservableGauge

instrument.Asynchronous
}

func (i *aiGauge) setDelegate(m metric.Meter) {
Expand All @@ -197,12 +167,6 @@ func (i *aiGauge) setDelegate(m metric.Meter) {
i.delegate.Store(ctr)
}

func (i *aiGauge) Observe(ctx context.Context, x int64, attrs ...attribute.KeyValue) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(instrument.Int64ObservableGauge).Observe(ctx, x, attrs...)
}
}

func (i *aiGauge) unwrap() instrument.Asynchronous {
if ctr := i.delegate.Load(); ctr != nil {
return ctr.(instrument.Int64ObservableGauge)
Expand Down
Loading

0 comments on commit f576781

Please sign in to comment.