1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-29 23:07:45 +02:00

Add single instrument callback and split metric instrument configuration (#3507)

* Split metric inst config

Instead of having the same configuration for both the Synchronous and
Asynchronous instruments, use specific options for both.

* Use Async/Sync opt for appropriate inst

* Update noop inst providers

* Update internal global impl

* Update sdk

* Remove unused method for callbackOption

* Test instrument configuration

* Lint imports

* Add changes to changelog

* Refactor callbacks and further split opts

Define callbacks to return the value observed. Because of the different
types returned for different observables, the callbacks and options are
move to the sync/async packages.

* Update noop impl

* Fix example_test.go

* Fix internal impl

* Update Callbacks

Return observations for distinct attr sets.

* Refactor common code in sdk/metric inst provider

* Update examples and prom exporter

* Generalize callback

* Update changelog

* Add unit tests for callback

* Add meter tests for cbacks on creation

* Rename Observations to Measurements

* Update Callback to accept an Observer

* Update SDK impl

* Move conf to instrument pkg

* Apply suggestions from code review
This commit is contained in:
Tyler Yahn
2023-01-06 09:20:49 -08:00
committed by GitHub
parent 0851690095
commit 1f9cc3036b
23 changed files with 887 additions and 286 deletions

View File

@@ -76,8 +76,9 @@ type pipeline struct {
views []View
sync.Mutex
aggregations map[instrumentation.Scope][]instrumentSync
callbacks list.List
aggregations map[instrumentation.Scope][]instrumentSync
callbacks []func(context.Context) error
multiCallbacks list.List
}
// addSync adds the instrumentSync to pipeline p with scope. This method is not
@@ -95,14 +96,23 @@ func (p *pipeline) addSync(scope instrumentation.Scope, iSync instrumentSync) {
p.aggregations[scope] = append(p.aggregations[scope], iSync)
}
// addCallback registers a callback to be run when `produce()` is called.
func (p *pipeline) addCallback(c metric.Callback) (unregister func()) {
// addCallback registers a single instrument callback to be run when
// `produce()` is called.
func (p *pipeline) addCallback(cback func(context.Context) error) {
p.Lock()
defer p.Unlock()
e := p.callbacks.PushBack(c)
p.callbacks = append(p.callbacks, cback)
}
// addMultiCallback registers a multi-instrument callback to be run when
// `produce()` is called.
func (p *pipeline) addMultiCallback(c metric.Callback) (unregister func()) {
p.Lock()
defer p.Unlock()
e := p.multiCallbacks.PushBack(c)
return func() {
p.Lock()
p.callbacks.Remove(e)
p.multiCallbacks.Remove(e)
p.Unlock()
}
}
@@ -124,7 +134,17 @@ func (p *pipeline) produce(ctx context.Context) (metricdata.ResourceMetrics, err
p.Lock()
defer p.Unlock()
for e := p.callbacks.Front(); e != nil; e = e.Next() {
var errs multierror
for _, c := range p.callbacks {
// TODO make the callbacks parallel. ( #3034 )
if err := c(ctx); err != nil {
errs.append(err)
}
if err := ctx.Err(); err != nil {
return metricdata.ResourceMetrics{}, err
}
}
for e := p.multiCallbacks.Front(); e != nil; e = e.Next() {
// TODO make the callbacks parallel. ( #3034 )
f := e.Value.(metric.Callback)
f(ctx)
@@ -159,7 +179,7 @@ func (p *pipeline) produce(ctx context.Context) (metricdata.ResourceMetrics, err
return metricdata.ResourceMetrics{
Resource: p.resource,
ScopeMetrics: sm,
}, nil
}, errs.errorOrNil()
}
// inserter facilitates inserting of new instruments from a single scope into a
@@ -447,10 +467,16 @@ func newPipelines(res *resource.Resource, readers []Reader, views []View) pipeli
return pipes
}
func (p pipelines) registerCallback(c metric.Callback) metric.Registration {
func (p pipelines) registerCallback(cback func(context.Context) error) {
for _, pipe := range p {
pipe.addCallback(cback)
}
}
func (p pipelines) registerMultiCallback(c metric.Callback) metric.Registration {
unregs := make([]func(), len(p))
for i, pipe := range p {
unregs[i] = pipe.addCallback(c)
unregs[i] = pipe.addMultiCallback(c)
}
return unregisterFuncs(unregs)
}