diff --git a/example/prometheus/main.go b/example/prometheus/main.go index 254ed83a3e47..4d7be8b776d4 100644 --- a/example/prometheus/main.go +++ b/example/prometheus/main.go @@ -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 diff --git a/metric/example_test.go b/metric/example_test.go index 5c981329a41e..42abfe2cfd11 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -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) diff --git a/metric/internal/global/meter_test.go b/metric/internal/global/meter_test.go index 1286176ca2ec..27fd68f1a5d3 100644 --- a/metric/internal/global/meter_test.go +++ b/metric/internal/global/meter_test.go @@ -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 } @@ -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) @@ -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) @@ -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) diff --git a/metric/meter.go b/metric/meter.go index 559f55e67f4e..d01cd5e700cd 100644 --- a/metric/meter.go +++ b/metric/meter.go @@ -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 @@ -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 diff --git a/sdk/metric/meter.go b/sdk/metric/meter.go index 07294a04d67e..cf86ce85dd8e 100644 --- a/sdk/metric/meter.go +++ b/sdk/metric/meter.go @@ -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) { @@ -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 } @@ -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") @@ -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") diff --git a/sdk/metric/meter_test.go b/sdk/metric/meter_test.go index 7ede255360e2..0634bdb65122 100644 --- a/sdk/metric/meter_test.go +++ b/sdk/metric/meter_test.go @@ -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) { @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 }, @@ -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 }, @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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