Skip to content

Commit

Permalink
Merge branch 'main' into multi-cback-reg
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jan 13, 2023
2 parents 355064f + f941b3a commit a562252
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 72 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The exporter from `go.opentelemetry.io/otel/exporters/zipkin` is updated to use the `v1.16.0` version of semantic conventions.
This means it no longer uses the removed `net.peer.ip` or `http.host` attributes to determine the remote endpoint.
Instead it uses the `net.sock.peer` attributes. (#3581)
- The parameters for the `RegisterCallback` method of the `Meter` from `go.opentelemetry.io/otel/metric` are changed.
The slice of `instrument.Asynchronous` parameter is now passed as a variadic argument. (#3587)
- The `Callback` in `go.opentelemetry.io/otel/metric` has the added `Observer` parameter added.
This new parameter is used by `Callback` implementations to observe values for asynchronous instruments instead of calling the `Observe` method of the instrument directly. (#3584)

Expand Down
4 changes: 2 additions & 2 deletions example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func main() {
if err != nil {
log.Fatal(err)
}
_, err = meter.RegisterCallback([]instrument.Asynchronous{gauge}, func(_ context.Context, o api.Observer) error {
_, err = meter.RegisterCallback(func(_ context.Context, o api.Observer) error {
n := -10. + rand.Float64()*(90.) // [-10, 100)
o.ObserveFloat64(gauge, n, attrs...)
return nil
})
}, gauge)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ func ExampleMeter_asynchronous_multiple() {
gcCount, _ := meter.Int64ObservableCounter("gcCount")
gcPause, _ := meter.Float64Histogram("gcPause")

_, err := meter.RegisterCallback([]instrument.Asynchronous{
heapAlloc,
gcCount,
},
_, err := meter.RegisterCallback(
func(ctx context.Context, o metric.Observer) error {
memStats := &runtime.MemStats{}
// This call does work
Expand All @@ -105,6 +102,8 @@ func ExampleMeter_asynchronous_multiple() {
computeGCPauses(ctx, gcPause, memStats.PauseNs[:])
return nil
},
heapAlloc,
gcCount,
)

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions metric/internal/global/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ func (m *meter) Float64ObservableGauge(name string, options ...instrument.Float6
//
// It is only valid to call Observe within the scope of the passed function,
// and only on the instruments that were registered with this call.
func (m *meter) RegisterCallback(insts []instrument.Asynchronous, f metric.Callback) (metric.Registration, error) {
func (m *meter) RegisterCallback(f metric.Callback, insts ...instrument.Asynchronous) (metric.Registration, error) {
if del, ok := m.delegate.Load().(metric.Meter); ok {
insts = unwrapInstruments(insts)
return del.RegisterCallback(insts, f)
return del.RegisterCallback(f, insts...)
}

m.mtx.Lock()
Expand Down Expand Up @@ -335,7 +335,7 @@ func (c *registration) setDelegate(m metric.Meter) {
return
}

reg, err := m.RegisterCallback(insts, c.function)
reg, err := m.RegisterCallback(c.function, insts...)
if err != nil {
otel.Handle(err)
}
Expand Down
18 changes: 8 additions & 10 deletions metric/internal/global/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestMeterRace(t *testing.T) {
_, _ = mtr.Int64Counter(name)
_, _ = mtr.Int64UpDownCounter(name)
_, _ = mtr.Int64Histogram(name)
_, _ = mtr.RegisterCallback(nil, zeroCallback)
_, _ = mtr.RegisterCallback(zeroCallback)
if !once {
wg.Done()
once = true
Expand All @@ -90,7 +90,7 @@ func TestMeterRace(t *testing.T) {

func TestUnregisterRace(t *testing.T) {
mtr := &meter{}
reg, err := mtr.RegisterCallback(nil, zeroCallback)
reg, err := mtr.RegisterCallback(zeroCallback)
require.NoError(t, err)

wg := &sync.WaitGroup{}
Expand Down Expand Up @@ -132,12 +132,10 @@ func testSetupAllInstrumentTypes(t *testing.T, m metric.Meter) (instrument.Float
_, err = m.Int64ObservableGauge("test_Async_Gauge")
assert.NoError(t, err)

_, err = m.RegisterCallback([]instrument.Asynchronous{
afcounter,
}, func(ctx context.Context, obs metric.Observer) error {
_, err = m.RegisterCallback(func(ctx context.Context, obs metric.Observer) error {
obs.ObserveFloat64(afcounter, 3)
return nil
})
}, afcounter)
require.NoError(t, err)

sfcounter, err := m.Float64Counter("test_Async_Counter")
Expand Down Expand Up @@ -329,21 +327,21 @@ func TestRegistrationDelegation(t *testing.T) {
require.NoError(t, err)

var called0 bool
reg0, err := m.RegisterCallback([]instrument.Asynchronous{actr}, func(context.Context, metric.Observer) error {
reg0, err := m.RegisterCallback(func(context.Context, metric.Observer) error {
called0 = true
return nil
})
}, actr)
require.NoError(t, err)
require.Equal(t, 1, mImpl.registry.Len(), "callback not registered")
// This means reg0 should not be delegated.
assert.NoError(t, reg0.Unregister())
assert.Equal(t, 0, mImpl.registry.Len(), "callback not unregistered")

var called1 bool
reg1, err := m.RegisterCallback([]instrument.Asynchronous{actr}, func(context.Context, metric.Observer) error {
reg1, err := m.RegisterCallback(func(context.Context, metric.Observer) error {
called1 = true
return nil
})
}, actr)
require.NoError(t, err)
require.Equal(t, 1, mImpl.registry.Len(), "second callback not registered")

Expand Down
2 changes: 1 addition & 1 deletion metric/internal/global/meter_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (m *testMeter) Float64ObservableGauge(name string, options ...instrument.Fl
//
// It is only valid to call Observe within the scope of the passed function,
// and only on the instruments that were registered with this call.
func (m *testMeter) RegisterCallback(i []instrument.Asynchronous, f metric.Callback) (metric.Registration, error) {
func (m *testMeter) RegisterCallback(f metric.Callback, i ...instrument.Asynchronous) (metric.Registration, error) {
m.callbacks = append(m.callbacks, f)
return testReg{
f: func(idx int) func() {
Expand Down
2 changes: 1 addition & 1 deletion metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type Meter interface {
//
// If no instruments are passed, f should not be registered nor called
// during collection.
RegisterCallback(instruments []instrument.Asynchronous, f Callback) (Registration, error)
RegisterCallback(f Callback, instruments ...instrument.Asynchronous) (Registration, error)
}

// Callback is a function registered with a Meter that makes observations for
Expand Down
2 changes: 1 addition & 1 deletion metric/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (noopMeter) Float64ObservableGauge(string, ...instrument.Float64ObserverOpt
}

// RegisterCallback creates a register callback that does not record any metrics.
func (noopMeter) RegisterCallback([]instrument.Asynchronous, Callback) (Registration, error) {
func (noopMeter) RegisterCallback(Callback, ...instrument.Asynchronous) (Registration, error) {
return noopReg{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (m *meter) Float64ObservableGauge(name string, options ...instrument.Float6

// RegisterCallback registers the function f to be called when any of the
// insts Collect method is called.
func (m *meter) RegisterCallback(insts []instrument.Asynchronous, f metric.Callback) (metric.Registration, error) {
func (m *meter) RegisterCallback(f metric.Callback, insts ...instrument.Asynchronous) (metric.Registration, error) {
if len(insts) == 0 {
// Don't allocate a observer if not needed.
return noopRegister{}, nil
Expand Down
Loading

0 comments on commit a562252

Please sign in to comment.