Skip to content

Commit

Permalink
Rename ObservationRecorder to MultiObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jan 11, 2023
1 parent 93a5d85 commit de250ca
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
_, err = meter.RegisterCallback(func(_ context.Context, o api.ObservationRecorder) error {
_, err = meter.RegisterCallback(func(_ context.Context, o api.MultiObserver) error {
n := -10. + rand.Float64()*(90.) // [-10, 100)
o.Float64(gauge, n, attrs...)
return nil
Expand Down
2 changes: 1 addition & 1 deletion metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func ExampleMeter_asynchronous_multiple() {
gcPause, _ := meter.Float64Histogram("gcPause")

_, err := meter.RegisterCallback(
func(ctx context.Context, o metric.ObservationRecorder) error {
func(ctx context.Context, o metric.MultiObserver) error {
memStats := &runtime.MemStats{}
// This call does work
runtime.ReadMemStats(memStats)
Expand Down
8 changes: 4 additions & 4 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.ObservationRecorder) error {
var zeroCallback metric.Callback = func(ctx context.Context, or metric.MultiObserver) error {
return nil
}

Expand Down Expand Up @@ -132,7 +132,7 @@ 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.ObservationRecorder) error {
_, err = m.RegisterCallback(func(ctx context.Context, obs metric.MultiObserver) error {
obs.Float64(afcounter, 3)
return nil
}, afcounter)
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.ObservationRecorder) error {
reg0, err := m.RegisterCallback(func(context.Context, metric.MultiObserver) 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.ObservationRecorder) error {
reg1, err := m.RegisterCallback(func(context.Context, metric.MultiObserver) error {
called1 = true
return nil
}, actr)
Expand Down
16 changes: 8 additions & 8 deletions metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type Meter interface {
// Callback is a function registered with a Meter that makes observations for
// the set of instruments it is registered with.
//
// The ObservationRecorder parameter is used to record measurment observations.
// The MultiObserver parameter is used to record measurment observations.
// Measurments should not be recorded directly with an Observer.
//
// The function needs to complete in a finite amount of time and the deadline
Expand All @@ -120,14 +120,14 @@ type Meter interface {
// the same attributes as another Callback will report.
//
// The function needs to be concurrent safe.
type Callback func(context.Context, ObservationRecorder) error
type Callback func(context.Context, MultiObserver) error

// ObservationRecorder records the measurements in a Callback.
type ObservationRecorder interface {
// Float64 records the float64 value with attributes for inst.
Float64(inst instrument.Float64Observer, value float64, attributes ...attribute.KeyValue)
// Int64 records the int64 value with attributes for inst.
Int64(inst instrument.Int64Observer, value int64, attributes ...attribute.KeyValue)
// MultiObserver records measurements for different instruments in a Callback.
type MultiObserver interface {
// Float64 records the float64 value with attributes for obsrv.
Float64(obsrv instrument.Float64Observer, value float64, attributes ...attribute.KeyValue)
// Int64 records the int64 value with attributes for obsrv.
Int64(obsrv instrument.Int64Observer, value int64, attributes ...attribute.KeyValue)
}

// Registration is an token representing the unique registration of a callback
Expand Down
28 changes: 14 additions & 14 deletions sdk/metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (m *meter) RegisterCallback(f metric.Callback, insts ...instrument.Asynchro
return noopRegister{}, nil
}

reg := newObservationRegistry()
reg := newObserverRegistry()
var errs multierror
for _, inst := range insts {
switch o := inst.(type) {
Expand Down Expand Up @@ -249,41 +249,41 @@ func (m *meter) RegisterCallback(f metric.Callback, insts ...instrument.Asynchro
}

cback := func(ctx context.Context) error {
return f(ctx, reg.recorder(ctx))
return f(ctx, reg.multiObserver(ctx))
}
return m.pipes.registerMultiCallback(cback), nil
}

type obeservationRegistry struct {
type obeserverRegistry struct {
float64 map[observerID[float64]]struct{}
int64 map[observerID[int64]]struct{}
}

func newObservationRegistry() obeservationRegistry {
return obeservationRegistry{
func newObserverRegistry() obeserverRegistry {
return obeserverRegistry{
float64: make(map[observerID[float64]]struct{}),
int64: make(map[observerID[int64]]struct{}),
}
}

func (r obeservationRegistry) len() int {
func (r obeserverRegistry) len() int {
return len(r.float64) + len(r.int64)
}

func (r obeservationRegistry) registerFloat64(id observerID[float64]) {
func (r obeserverRegistry) registerFloat64(id observerID[float64]) {
r.float64[id] = struct{}{}
}

func (r obeservationRegistry) registerInt64(id observerID[int64]) {
func (r obeserverRegistry) registerInt64(id observerID[int64]) {
r.int64[id] = struct{}{}
}

func (r obeservationRegistry) recorder(ctx context.Context) metric.ObservationRecorder {
return obeservationRecorder{obeservationRegistry: r, ctx: ctx}
func (r obeserverRegistry) multiObserver(ctx context.Context) metric.MultiObserver {
return multiObserver{obeserverRegistry: r, ctx: ctx}
}

type obeservationRecorder struct {
obeservationRegistry
type multiObserver struct {
obeserverRegistry

ctx context.Context
}
Expand All @@ -293,7 +293,7 @@ var (
errUnregObserver = errors.New("observer not registered for callback")
)

func (r obeservationRecorder) Float64(o instrument.Float64Observer, v float64, a ...attribute.KeyValue) {
func (r multiObserver) Float64(o instrument.Float64Observer, v float64, a ...attribute.KeyValue) {
oImpl, ok := o.(*observer[float64])
if !ok {
global.Error(errUnknownObserver, "failed to record")
Expand All @@ -311,7 +311,7 @@ func (r obeservationRecorder) Float64(o instrument.Float64Observer, v float64, a
oImpl.observe(r.ctx, v, a)
}

func (r obeservationRecorder) Int64(o instrument.Int64Observer, v int64, a ...attribute.KeyValue) {
func (r multiObserver) Int64(o instrument.Int64Observer, v int64, a ...attribute.KeyValue) {
oImpl, ok := o.(*observer[int64])
if !ok {
global.Error(errUnknownObserver, "failed to record")
Expand Down
34 changes: 17 additions & 17 deletions sdk/metric/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestMeterInstrumentConcurrency(t *testing.T) {
wg.Wait()
}

var emptyCallback metric.Callback = func(context.Context, metric.ObservationRecorder) error { return nil }
var emptyCallback metric.Callback = func(context.Context, metric.MultiObserver) error { return nil }

// A Meter Should be able register Callbacks Concurrently.
func TestMeterCallbackCreationConcurrency(t *testing.T) {
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestMeterCreatesInstruments(t *testing.T) {
}
ctr, err := m.Int64ObservableCounter("aint", instrument.WithInt64Callback(cback))
assert.NoError(t, err)
_, err = m.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = m.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Int64(ctr, 3)
return nil
}, ctr)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestMeterCreatesInstruments(t *testing.T) {
}
ctr, err := m.Int64ObservableUpDownCounter("aint", instrument.WithInt64Callback(cback))
assert.NoError(t, err)
_, err = m.RegisterCallback(func(ctx context.Context, o metric.ObservationRecorder) error {
_, err = m.RegisterCallback(func(ctx context.Context, o metric.MultiObserver) error {
o.Int64(ctr, 11)
return nil
}, ctr)
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestMeterCreatesInstruments(t *testing.T) {
}
gauge, err := m.Int64ObservableGauge("agauge", instrument.WithInt64Callback(cback))
assert.NoError(t, err)
_, err = m.RegisterCallback(func(ctx context.Context, o metric.ObservationRecorder) error {
_, err = m.RegisterCallback(func(ctx context.Context, o metric.MultiObserver) error {
o.Int64(gauge, 11)
return nil
}, gauge)
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestMeterCreatesInstruments(t *testing.T) {
}
ctr, err := m.Float64ObservableCounter("afloat", instrument.WithFloat64Callback(cback))
assert.NoError(t, err)
_, err = m.RegisterCallback(func(ctx context.Context, o metric.ObservationRecorder) error {
_, err = m.RegisterCallback(func(ctx context.Context, o metric.MultiObserver) error {
o.Float64(ctr, 3)
return nil
}, ctr)
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestMeterCreatesInstruments(t *testing.T) {
}
ctr, err := m.Float64ObservableUpDownCounter("afloat", instrument.WithFloat64Callback(cback))
assert.NoError(t, err)
_, err = m.RegisterCallback(func(ctx context.Context, o metric.ObservationRecorder) error {
_, err = m.RegisterCallback(func(ctx context.Context, o metric.MultiObserver) error {
o.Float64(ctr, 11)
return nil
}, ctr)
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestMeterCreatesInstruments(t *testing.T) {
}
gauge, err := m.Float64ObservableGauge("agauge", instrument.WithFloat64Callback(cback))
assert.NoError(t, err)
_, err = m.RegisterCallback(func(ctx context.Context, o metric.ObservationRecorder) error {
_, err = m.RegisterCallback(func(ctx context.Context, o metric.MultiObserver) error {
o.Float64(gauge, 11)
return nil
}, gauge)
Expand Down Expand Up @@ -503,7 +503,7 @@ func TestMetersProvideScope(t *testing.T) {
m1 := mp.Meter("scope1")
ctr1, err := m1.Float64ObservableCounter("ctr1")
assert.NoError(t, err)
_, err = m1.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = m1.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Float64(ctr1, 5)
return nil
}, ctr1)
Expand All @@ -512,7 +512,7 @@ func TestMetersProvideScope(t *testing.T) {
m2 := mp.Meter("scope2")
ctr2, err := m2.Int64ObservableCounter("ctr2")
assert.NoError(t, err)
_, err = m2.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = m2.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Int64(ctr2, 7)
return nil
}, ctr2)
Expand Down Expand Up @@ -593,7 +593,7 @@ func TestUnregisterUnregisters(t *testing.T) {

var called bool
reg, err := m.RegisterCallback(
func(context.Context, metric.ObservationRecorder) error {
func(context.Context, metric.MultiObserver) error {
called = true
return nil
},
Expand Down Expand Up @@ -647,7 +647,7 @@ func TestRegisterCallbackDropAggregations(t *testing.T) {

var called bool
_, err = m.RegisterCallback(
func(context.Context, metric.ObservationRecorder) error {
func(context.Context, metric.MultiObserver) error {
called = true
return nil
},
Expand Down Expand Up @@ -682,7 +682,7 @@ func TestAttributeFilter(t *testing.T) {
if err != nil {
return err
}
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Float64(ctr, 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1))
o.Float64(ctr, 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2))
return nil
Expand Down Expand Up @@ -710,7 +710,7 @@ func TestAttributeFilter(t *testing.T) {
if err != nil {
return err
}
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Float64(ctr, 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1))
o.Float64(ctr, 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2))
return nil
Expand Down Expand Up @@ -738,7 +738,7 @@ func TestAttributeFilter(t *testing.T) {
if err != nil {
return err
}
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Float64(ctr, 1.0, attribute.String("foo", "bar"), attribute.Int("version", 1))
o.Float64(ctr, 2.0, attribute.String("foo", "bar"), attribute.Int("version", 2))
return nil
Expand All @@ -764,7 +764,7 @@ func TestAttributeFilter(t *testing.T) {
if err != nil {
return err
}
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Int64(ctr, 10, attribute.String("foo", "bar"), attribute.Int("version", 1))
o.Int64(ctr, 20, attribute.String("foo", "bar"), attribute.Int("version", 2))
return nil
Expand Down Expand Up @@ -792,7 +792,7 @@ func TestAttributeFilter(t *testing.T) {
if err != nil {
return err
}
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Int64(ctr, 10, attribute.String("foo", "bar"), attribute.Int("version", 1))
o.Int64(ctr, 20, attribute.String("foo", "bar"), attribute.Int("version", 2))
return nil
Expand Down Expand Up @@ -820,7 +820,7 @@ func TestAttributeFilter(t *testing.T) {
if err != nil {
return err
}
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.ObservationRecorder) error {
_, err = mtr.RegisterCallback(func(_ context.Context, o metric.MultiObserver) error {
o.Int64(ctr, 10, attribute.String("foo", "bar"), attribute.Int("version", 1))
o.Int64(ctr, 20, attribute.String("foo", "bar"), attribute.Int("version", 2))
return nil
Expand Down

0 comments on commit de250ca

Please sign in to comment.