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

Redesign RegisterCallback API (#3584)

* Update RegisterCallback and Callback declerations

RegisterCallback accepts variadic Asynchronous instruments instead of a
slice.

Callback accepts an observation result recorder to ensure instruments
that are observed by a callback.

* Update global, noop, SDK implementations
* Fix examples
* Add changes to changelog
* Test RegisterCallback for invalid observers
* Test callbacks from foreign sources not collected
* Support registering delegating instruments
This commit is contained in:
Tyler Yahn
2023-01-19 06:18:26 -08:00
committed by GitHub
parent e8c6e45178
commit 69b18e62a7
12 changed files with 630 additions and 114 deletions

View File

@@ -104,9 +104,11 @@ func (p *pipeline) addCallback(cback func(context.Context) error) {
p.callbacks = append(p.callbacks, cback)
}
type multiCallback func(context.Context) error
// addMultiCallback registers a multi-instrument callback to be run when
// `produce()` is called.
func (p *pipeline) addMultiCallback(c metric.Callback) (unregister func()) {
func (p *pipeline) addMultiCallback(c multiCallback) (unregister func()) {
p.Lock()
defer p.Unlock()
e := p.multiCallbacks.PushBack(c)
@@ -146,7 +148,7 @@ func (p *pipeline) produce(ctx context.Context) (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 := e.Value.(multiCallback)
if err := f(ctx); err != nil {
errs.append(err)
}
@@ -475,7 +477,7 @@ func (p pipelines) registerCallback(cback func(context.Context) error) {
}
}
func (p pipelines) registerMultiCallback(c metric.Callback) metric.Registration {
func (p pipelines) registerMultiCallback(c multiCallback) metric.Registration {
unregs := make([]func(), len(p))
for i, pipe := range p {
unregs[i] = pipe.addMultiCallback(c)