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

Only registers callbacks if non-drop aggregation is used #3408

Merged
merged 20 commits into from Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Cumulative metrics from the OpenCensus bridge (`go.opentelemetry.io/otel/bridge/opencensus`) are defined as monotonic sums, instead of non-monotonic. (#3389)
- Asynchronous counters (`Counter` and `UpDownCounter`) from the metric SDK now produce delta sums when configured with delta temporality. (#3398)
- Exported `Status` codes in the `go.opentelemetry.io/otel/exporters/zipkin` exporter are now exported as all upper case values. (#3340)
- Asynchronous callbacks are only called if they are registered with at least one instrument that does not use drop aggragation. (#3408)

## [1.11.1/0.33.0] 2022-10-19

Expand Down
87 changes: 12 additions & 75 deletions sdk/metric/instrument_provider.go
Expand Up @@ -15,8 +15,6 @@
package metric // import "go.opentelemetry.io/otel/sdk/metric"

import (
"fmt"

"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/asyncfloat64"
"go.opentelemetry.io/otel/metric/instrument/asyncint64"
Expand All @@ -43,13 +41,7 @@ func (p asyncInt64Provider) Counter(name string, opts ...instrument.Option) (asy
Description: cfg.Description(),
Kind: view.AsyncCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}

return &instrumentImpl[int64]{
aggregators: aggs,
}, err
return &instrumentImpl[int64]{aggregators: aggs}, err
}

// UpDownCounter creates an instrument for recording changes of a value.
Expand All @@ -62,12 +54,7 @@ func (p asyncInt64Provider) UpDownCounter(name string, opts ...instrument.Option
Description: cfg.Description(),
Kind: view.AsyncUpDownCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[int64]{
aggregators: aggs,
}, err
return &instrumentImpl[int64]{aggregators: aggs}, err
}

// Gauge creates an instrument for recording the current value.
Expand All @@ -80,12 +67,7 @@ func (p asyncInt64Provider) Gauge(name string, opts ...instrument.Option) (async
Description: cfg.Description(),
Kind: view.AsyncGauge,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[int64]{
aggregators: aggs,
}, err
return &instrumentImpl[int64]{aggregators: aggs}, err
}

type asyncFloat64Provider struct {
Expand All @@ -105,12 +87,7 @@ func (p asyncFloat64Provider) Counter(name string, opts ...instrument.Option) (a
Description: cfg.Description(),
Kind: view.AsyncCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[float64]{
aggregators: aggs,
}, err
return &instrumentImpl[float64]{aggregators: aggs}, err
}

// UpDownCounter creates an instrument for recording changes of a value.
Expand All @@ -123,12 +100,7 @@ func (p asyncFloat64Provider) UpDownCounter(name string, opts ...instrument.Opti
Description: cfg.Description(),
Kind: view.AsyncUpDownCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[float64]{
aggregators: aggs,
}, err
return &instrumentImpl[float64]{aggregators: aggs}, err
}

// Gauge creates an instrument for recording the current value.
Expand All @@ -141,12 +113,7 @@ func (p asyncFloat64Provider) Gauge(name string, opts ...instrument.Option) (asy
Description: cfg.Description(),
Kind: view.AsyncGauge,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[float64]{
aggregators: aggs,
}, err
return &instrumentImpl[float64]{aggregators: aggs}, err
}

type syncInt64Provider struct {
Expand All @@ -166,12 +133,7 @@ func (p syncInt64Provider) Counter(name string, opts ...instrument.Option) (sync
Description: cfg.Description(),
Kind: view.SyncCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[int64]{
aggregators: aggs,
}, err
return &instrumentImpl[int64]{aggregators: aggs}, err
}

// UpDownCounter creates an instrument for recording changes of a value.
Expand All @@ -184,12 +146,7 @@ func (p syncInt64Provider) UpDownCounter(name string, opts ...instrument.Option)
Description: cfg.Description(),
Kind: view.SyncUpDownCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[int64]{
aggregators: aggs,
}, err
return &instrumentImpl[int64]{aggregators: aggs}, err
}

// Histogram creates an instrument for recording the current value.
Expand All @@ -202,12 +159,7 @@ func (p syncInt64Provider) Histogram(name string, opts ...instrument.Option) (sy
Description: cfg.Description(),
Kind: view.SyncHistogram,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[int64]{
aggregators: aggs,
}, err
return &instrumentImpl[int64]{aggregators: aggs}, err
}

type syncFloat64Provider struct {
Expand All @@ -227,12 +179,7 @@ func (p syncFloat64Provider) Counter(name string, opts ...instrument.Option) (sy
Description: cfg.Description(),
Kind: view.SyncCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[float64]{
aggregators: aggs,
}, err
return &instrumentImpl[float64]{aggregators: aggs}, err
}

// UpDownCounter creates an instrument for recording changes of a value.
Expand All @@ -245,12 +192,7 @@ func (p syncFloat64Provider) UpDownCounter(name string, opts ...instrument.Optio
Description: cfg.Description(),
Kind: view.SyncUpDownCounter,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[float64]{
aggregators: aggs,
}, err
return &instrumentImpl[float64]{aggregators: aggs}, err
}

// Histogram creates an instrument for recording the current value.
Expand All @@ -263,10 +205,5 @@ func (p syncFloat64Provider) Histogram(name string, opts ...instrument.Option) (
Description: cfg.Description(),
Kind: view.SyncHistogram,
}, cfg.Unit())
if len(aggs) == 0 && err != nil {
err = fmt.Errorf("instrument does not match any view: %w", err)
}
return &instrumentImpl[float64]{
aggregators: aggs,
}, err
return &instrumentImpl[float64]{aggregators: aggs}, err
}
19 changes: 18 additions & 1 deletion sdk/metric/meter.go
Expand Up @@ -16,6 +16,7 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric"

import (
"context"
"errors"

"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
Expand Down Expand Up @@ -77,7 +78,23 @@ func (m *meter) AsyncFloat64() asyncfloat64.InstrumentProvider {
// 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 func(context.Context)) error {
m.pipes.registerCallback(f)
for _, inst := range insts {
switch t := inst.(type) {
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
case *instrumentImpl[int64]:
if len(t.aggregators) > 0 {
m.pipes.registerCallback(f)
return nil
}
case *instrumentImpl[float64]:
if len(t.aggregators) > 0 {
m.pipes.registerCallback(f)
return nil
}
default:
return errors.New("invalid asynchronous instrument")
}
}
// Only instrument using drop aggregation passed.
return nil
}

Expand Down
59 changes: 59 additions & 0 deletions sdk/metric/meter_test.go
Expand Up @@ -26,8 +26,10 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"go.opentelemetry.io/otel/sdk/metric/view"
"go.opentelemetry.io/otel/sdk/resource"
)

Expand Down Expand Up @@ -474,3 +476,60 @@ func TestMetersProvideScope(t *testing.T) {
assert.NoError(t, err)
metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp())
}

func TestRegisterCallbackDropAggregations(t *testing.T) {
aggFn := func(view.InstrumentKind) aggregation.Aggregation {
return aggregation.Drop{}
}
r := NewManualReader(WithAggregationSelector(aggFn))
mp := NewMeterProvider(WithReader(r))
m := mp.Meter("testRegisterCallbackDropAggregations")

int64Counter, err := m.AsyncInt64().Counter("int64.counter")
require.NoError(t, err)

int64UpDownCounter, err := m.AsyncInt64().UpDownCounter("int64.up_down_counter")
require.NoError(t, err)

int64Gauge, err := m.AsyncInt64().Gauge("int64.gauge")
require.NoError(t, err)

floag64Counter, err := m.AsyncFloat64().Counter("floag64.counter")
require.NoError(t, err)

floag64UpDownCounter, err := m.AsyncFloat64().UpDownCounter("floag64.up_down_counter")
require.NoError(t, err)

floag64Gauge, err := m.AsyncFloat64().Gauge("floag64.gauge")
require.NoError(t, err)

var called bool
require.NoError(t, m.RegisterCallback([]instrument.Asynchronous{
int64Counter,
int64UpDownCounter,
int64Gauge,
floag64Counter,
floag64UpDownCounter,
floag64Gauge,
}, func(context.Context) { called = true }))

data, err := r.Collect(context.Background())
require.NoError(t, err)

assert.False(t, called, "callback called for all drop instruments")
assert.Len(t, data.ScopeMetrics, 0, "metrics exported for drop instruments")
}

func TestRegisterCallbackErrorForNonSDKInstrument(t *testing.T) {
type alien struct{ instrument.Asynchronous }

r := NewManualReader()
mp := NewMeterProvider(WithReader(r))
m := mp.Meter("TestRegisterCallbackErrorForNonSDKInstrument")

err := m.RegisterCallback([]instrument.Asynchronous{&alien{}}, func(context.Context) {
panic("should not be registered")
})
assert.Error(t, err)
assert.NotPanics(t, func() { _, _ = r.Collect(context.Background()) })
}
1 change: 0 additions & 1 deletion sdk/metric/pipeline.go
Expand Up @@ -430,7 +430,6 @@ func newPipelines(res *resource.Resource, readers []Reader, views []view.View) p
return pipes
}

// TODO (#3053) Only register callbacks if any instrument matches in a view.
func (p pipelines) registerCallback(fn func(context.Context)) {
for _, pipe := range p {
pipe.addCallback(fn)
Expand Down