2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-29 22:27:22 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-11-04 19:10:58 +02:00
|
|
|
package metric // import "go.opentelemetry.io/otel/sdk/metric"
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-15 23:01:20 +02:00
|
|
|
"fmt"
|
2020-02-11 02:20:29 +02:00
|
|
|
"runtime"
|
2019-10-29 22:27:22 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2022-03-02 17:50:29 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/instrument"
|
2020-06-10 07:53:30 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator"
|
2021-12-13 22:13:03 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/export"
|
2022-03-02 17:50:29 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/number"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-05-11 19:23:06 +02:00
|
|
|
// Accumulator implements the OpenTelemetry Meter API. The
|
2020-06-23 21:00:15 +02:00
|
|
|
// Accumulator is bound to a single export.Processor in
|
2020-05-11 19:23:06 +02:00
|
|
|
// `NewAccumulator()`.
|
2019-10-29 22:27:22 +02:00
|
|
|
//
|
2020-05-11 19:23:06 +02:00
|
|
|
// The Accumulator supports a Collect() API to gather and export
|
2019-10-29 22:27:22 +02:00
|
|
|
// current data. Collect() should be arranged according to
|
2020-06-23 21:00:15 +02:00
|
|
|
// the processor model. Push-based processors will setup a
|
|
|
|
// timer to call Collect() periodically. Pull-based processors
|
2019-10-29 22:27:22 +02:00
|
|
|
// will call Collect() when a pull request arrives.
|
2020-05-11 19:23:06 +02:00
|
|
|
Accumulator struct {
|
2019-10-29 22:27:22 +02:00
|
|
|
// current maps `mapkey` to *record.
|
|
|
|
current sync.Map
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
callbackLock sync.Mutex
|
|
|
|
callbacks map[*callback]struct{}
|
2020-03-05 22:15:30 +02:00
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// currentEpoch is the current epoch number. It is
|
|
|
|
// incremented in `Collect()`.
|
|
|
|
currentEpoch int64
|
|
|
|
|
2020-06-23 21:00:15 +02:00
|
|
|
// processor is the configured processor+configuration.
|
|
|
|
processor export.Processor
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// collectLock prevents simultaneous calls to Collect().
|
|
|
|
collectLock sync.Mutex
|
2022-03-02 17:50:29 +02:00
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
callback struct {
|
|
|
|
insts map[*asyncInstrument]struct{}
|
|
|
|
f func(context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncContextKey struct{}
|
|
|
|
|
|
|
|
asyncInstrument struct {
|
|
|
|
baseInstrument
|
|
|
|
instrument.Asynchronous
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
syncInstrument struct {
|
2022-03-02 17:50:29 +02:00
|
|
|
baseInstrument
|
|
|
|
instrument.Synchronous
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 16:31:31 +02:00
|
|
|
// mapkey uniquely describes a metric instrument in terms of its
|
|
|
|
// InstrumentID and the encoded form of its attributes.
|
2019-10-29 22:27:22 +02:00
|
|
|
mapkey struct {
|
2021-10-14 18:06:22 +02:00
|
|
|
descriptor *sdkapi.Descriptor
|
2021-02-18 19:59:37 +02:00
|
|
|
ordered attribute.Distinct
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// record maintains the state of one metric instrument. Due
|
|
|
|
// the use of lock-free algorithms, there may be more than one
|
|
|
|
// `record` in existence at a time, although at most one can
|
2020-05-11 19:23:06 +02:00
|
|
|
// be referenced from the `Accumulator.current` map.
|
2019-10-29 22:27:22 +02:00
|
|
|
record struct {
|
2020-02-07 00:45:56 +02:00
|
|
|
// refMapped keeps track of refcounts and the mapping state to the
|
2020-05-11 19:23:06 +02:00
|
|
|
// Accumulator.current map.
|
2020-02-07 00:45:56 +02:00
|
|
|
refMapped refcountMapped
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-04-22 05:23:15 +02:00
|
|
|
// updateCount is incremented on every Update.
|
|
|
|
updateCount int64
|
|
|
|
|
|
|
|
// collectedCount is set to updateCount on collection,
|
|
|
|
// supports checking for no updates during a round.
|
|
|
|
collectedCount int64
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2022-04-18 16:31:31 +02:00
|
|
|
// attrs is the stored attribute set for this record, except in cases
|
|
|
|
// where a attribute set is shared due to batch recording.
|
|
|
|
attrs attribute.Set
|
2020-03-27 23:06:48 +02:00
|
|
|
|
2022-04-18 16:31:31 +02:00
|
|
|
// sortSlice has a single purpose - as a temporary place for sorting
|
|
|
|
// during attributes creation to avoid allocation.
|
2021-02-18 19:59:37 +02:00
|
|
|
sortSlice attribute.Sortable
|
2020-01-06 20:08:40 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
// inst is a pointer to the corresponding instrument.
|
2022-03-02 17:50:29 +02:00
|
|
|
inst *baseInstrument
|
2020-01-06 20:08:40 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
// current implements the actual RecordOne() API,
|
2019-10-29 22:27:22 +02:00
|
|
|
// depending on the type of aggregation. If nil, the
|
|
|
|
// metric was disabled by the exporter.
|
2021-12-15 18:13:48 +02:00
|
|
|
current aggregator.Aggregator
|
|
|
|
checkpoint aggregator.Aggregator
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
baseInstrument struct {
|
2020-05-11 19:23:06 +02:00
|
|
|
meter *Accumulator
|
2021-10-14 18:06:22 +02:00
|
|
|
descriptor sdkapi.Descriptor
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-11-17 17:51:04 +02:00
|
|
|
_ sdkapi.MeterImpl = &Accumulator{}
|
2020-05-20 19:19:51 +02:00
|
|
|
|
2021-06-08 19:10:01 +02:00
|
|
|
// ErrUninitializedInstrument is returned when an instrument is used when uninitialized.
|
2020-05-20 19:19:51 +02:00
|
|
|
ErrUninitializedInstrument = fmt.Errorf("use of an uninitialized instrument")
|
2022-03-02 17:50:29 +02:00
|
|
|
|
|
|
|
ErrBadInstrument = fmt.Errorf("use of a instrument from another SDK")
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
func (b *baseInstrument) Descriptor() sdkapi.Descriptor {
|
|
|
|
return b.descriptor
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func (a *asyncInstrument) Implementation() interface{} {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *syncInstrument) Implementation() interface{} {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
// acquireHandle gets or creates a `*record` corresponding to `kvs`,
|
2022-04-18 16:31:31 +02:00
|
|
|
// the input attributes.
|
2022-03-02 17:50:29 +02:00
|
|
|
func (b *baseInstrument) acquireHandle(kvs []attribute.KeyValue) *record {
|
|
|
|
|
|
|
|
// This memory allocation may not be used, but it's
|
|
|
|
// needed for the `sortSlice` field, to avoid an
|
|
|
|
// allocation while sorting.
|
|
|
|
rec := &record{}
|
2022-04-18 16:31:31 +02:00
|
|
|
rec.attrs = attribute.NewSetWithSortable(kvs, &rec.sortSlice)
|
2020-03-27 23:06:48 +02:00
|
|
|
|
|
|
|
// Create lookup key for sync.Map (one allocation, as this
|
|
|
|
// passes through an interface{})
|
2019-10-29 22:27:22 +02:00
|
|
|
mk := mapkey{
|
2022-03-02 17:50:29 +02:00
|
|
|
descriptor: &b.descriptor,
|
2022-04-18 16:31:31 +02:00
|
|
|
ordered: rec.attrs.Equivalent(),
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
if actual, ok := b.meter.current.Load(mk); ok {
|
2020-03-27 23:06:48 +02:00
|
|
|
// Existing record case.
|
|
|
|
existingRec := actual.(*record)
|
|
|
|
if existingRec.refMapped.ref() {
|
2020-02-07 00:45:56 +02:00
|
|
|
// At this moment it is guaranteed that the entry is in
|
|
|
|
// the map and will not be removed.
|
2020-03-27 23:06:48 +02:00
|
|
|
return existingRec
|
2020-02-07 00:45:56 +02:00
|
|
|
}
|
|
|
|
// This entry is no longer mapped, try to add a new entry.
|
2019-11-06 20:54:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
rec.refMapped = refcountMapped{value: 2}
|
2022-03-02 17:50:29 +02:00
|
|
|
rec.inst = b
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
b.meter.processor.AggregatorFor(&b.descriptor, &rec.current, &rec.checkpoint)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-02-07 00:45:56 +02:00
|
|
|
for {
|
|
|
|
// Load/Store: there's a memory allocation to place `mk` into
|
|
|
|
// an interface here.
|
2022-03-02 17:50:29 +02:00
|
|
|
if actual, loaded := b.meter.current.LoadOrStore(mk, rec); loaded {
|
2020-02-07 00:45:56 +02:00
|
|
|
// Existing record case. Cannot change rec here because if fail
|
|
|
|
// will try to add rec again to avoid new allocations.
|
|
|
|
oldRec := actual.(*record)
|
|
|
|
if oldRec.refMapped.ref() {
|
|
|
|
// At this moment it is guaranteed that the entry is in
|
|
|
|
// the map and will not be removed.
|
|
|
|
return oldRec
|
|
|
|
}
|
|
|
|
// This loaded entry is marked as unmapped (so Collect will remove
|
|
|
|
// it from the map immediately), try again - this is a busy waiting
|
|
|
|
// strategy to wait until Collect() removes this entry from the map.
|
|
|
|
//
|
|
|
|
// This can be improved by having a list of "Unmapped" entries for
|
|
|
|
// one time only usages, OR we can make this a blocking path and use
|
|
|
|
// a Mutex that protects the delete operation (delete only if the old
|
|
|
|
// record is associated with the key).
|
2020-02-11 02:20:29 +02:00
|
|
|
|
|
|
|
// Let collector get work done to remove the entry from the map.
|
|
|
|
runtime.Gosched()
|
2020-02-07 00:45:56 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// The new entry was added to the map, good to go.
|
2019-10-29 22:27:22 +02:00
|
|
|
return rec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
// RecordOne captures a single synchronous metric event.
|
|
|
|
//
|
2021-04-20 23:29:13 +02:00
|
|
|
// The order of the input array `kvs` may be sorted after the function is called.
|
2021-02-18 19:59:37 +02:00
|
|
|
func (s *syncInstrument) RecordOne(ctx context.Context, num number.Number, kvs []attribute.KeyValue) {
|
2022-03-02 17:50:29 +02:00
|
|
|
h := s.acquireHandle(kvs)
|
2021-11-17 17:51:04 +02:00
|
|
|
defer h.unbind()
|
2022-03-02 17:50:29 +02:00
|
|
|
h.captureOne(ctx, num)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObserveOne captures a single asynchronous metric event.
|
|
|
|
|
|
|
|
// The order of the input array `kvs` may be sorted after the function is called.
|
|
|
|
func (a *asyncInstrument) ObserveOne(ctx context.Context, num number.Number, attrs []attribute.KeyValue) {
|
|
|
|
h := a.acquireHandle(attrs)
|
|
|
|
defer h.unbind()
|
|
|
|
h.captureOne(ctx, num)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
// NewAccumulator constructs a new Accumulator for the given
|
2020-06-23 21:00:15 +02:00
|
|
|
// processor. This Accumulator supports only a single processor.
|
2019-10-29 22:27:22 +02:00
|
|
|
//
|
2020-05-11 19:23:06 +02:00
|
|
|
// The Accumulator does not start any background process to collect itself
|
2020-12-15 18:06:58 +02:00
|
|
|
// periodically, this responsibility lies with the processor, typically,
|
2019-10-29 22:27:22 +02:00
|
|
|
// depending on the type of export. For example, a pull-based
|
2020-06-23 21:00:15 +02:00
|
|
|
// processor will call Collect() when it receives a request to scrape
|
|
|
|
// current metric values. A push-based processor should configure its
|
2019-10-29 22:27:22 +02:00
|
|
|
// own periodic collection.
|
2021-08-13 00:44:58 +02:00
|
|
|
func NewAccumulator(processor export.Processor) *Accumulator {
|
2020-05-11 19:23:06 +02:00
|
|
|
return &Accumulator{
|
2022-03-02 17:50:29 +02:00
|
|
|
processor: processor,
|
|
|
|
callbacks: map[*callback]struct{}{},
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
var _ sdkapi.MeterImpl = &Accumulator{}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// NewSyncInstrument implements sdkapi.MetricImpl.
|
|
|
|
func (m *Accumulator) NewSyncInstrument(descriptor sdkapi.Descriptor) (sdkapi.SyncImpl, error) {
|
2020-03-19 21:02:46 +02:00
|
|
|
return &syncInstrument{
|
2022-03-02 17:50:29 +02:00
|
|
|
baseInstrument: baseInstrument{
|
2020-03-19 21:02:46 +02:00
|
|
|
descriptor: descriptor,
|
|
|
|
meter: m,
|
|
|
|
},
|
2020-03-11 20:57:57 +02:00
|
|
|
}, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
// NewAsyncInstrument implements sdkapi.MetricImpl.
|
2022-03-02 17:50:29 +02:00
|
|
|
func (m *Accumulator) NewAsyncInstrument(descriptor sdkapi.Descriptor) (sdkapi.AsyncImpl, error) {
|
2020-03-19 21:02:46 +02:00
|
|
|
a := &asyncInstrument{
|
2022-03-02 17:50:29 +02:00
|
|
|
baseInstrument: baseInstrument{
|
2020-03-19 21:02:46 +02:00
|
|
|
descriptor: descriptor,
|
|
|
|
meter: m,
|
|
|
|
},
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
return a, nil
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
func (m *Accumulator) RegisterCallback(insts []instrument.Asynchronous, f func(context.Context)) error {
|
|
|
|
cb := &callback{
|
|
|
|
insts: map[*asyncInstrument]struct{}{},
|
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
for _, inst := range insts {
|
|
|
|
impl, ok := inst.(sdkapi.AsyncImpl)
|
|
|
|
if !ok {
|
|
|
|
return ErrBadInstrument
|
|
|
|
}
|
|
|
|
|
|
|
|
ai, err := m.fromAsync(impl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cb.insts[ai] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.callbackLock.Lock()
|
|
|
|
defer m.callbackLock.Unlock()
|
|
|
|
m.callbacks[cb] = struct{}{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:15:30 +02:00
|
|
|
// Collect traverses the list of active records and observers and
|
|
|
|
// exports data for each active instrument. Collect() may not be
|
|
|
|
// called concurrently.
|
2019-10-29 22:27:22 +02:00
|
|
|
//
|
2020-06-23 21:00:15 +02:00
|
|
|
// During the collection pass, the export.Processor will receive
|
2019-10-29 22:27:22 +02:00
|
|
|
// one Export() call per current aggregation.
|
2019-11-15 23:01:20 +02:00
|
|
|
//
|
|
|
|
// Returns the number of records that were checkpointed.
|
2020-05-11 19:23:06 +02:00
|
|
|
func (m *Accumulator) Collect(ctx context.Context) int {
|
2019-10-29 22:27:22 +02:00
|
|
|
m.collectLock.Lock()
|
|
|
|
defer m.collectLock.Unlock()
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
m.runAsyncCallbacks(ctx)
|
|
|
|
checkpointed := m.collectInstruments()
|
2020-03-05 22:15:30 +02:00
|
|
|
m.currentEpoch++
|
2020-05-19 03:37:41 +02:00
|
|
|
|
2020-03-05 22:15:30 +02:00
|
|
|
return checkpointed
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
func (m *Accumulator) collectInstruments() int {
|
2019-11-15 23:01:20 +02:00
|
|
|
checkpointed := 0
|
|
|
|
|
2020-02-07 00:45:56 +02:00
|
|
|
m.current.Range(func(key interface{}, value interface{}) bool {
|
2020-04-22 05:23:15 +02:00
|
|
|
// Note: always continue to iterate over the entire
|
|
|
|
// map by returning `true` in this function.
|
2020-02-07 00:45:56 +02:00
|
|
|
inuse := value.(*record)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-04-22 05:23:15 +02:00
|
|
|
mods := atomic.LoadInt64(&inuse.updateCount)
|
|
|
|
coll := inuse.collectedCount
|
|
|
|
|
|
|
|
if mods != coll {
|
|
|
|
// Updates happened in this interval,
|
|
|
|
// checkpoint and continue.
|
2020-06-09 20:00:50 +02:00
|
|
|
checkpointed += m.checkpointRecord(inuse)
|
2020-04-22 05:23:15 +02:00
|
|
|
inuse.collectedCount = mods
|
|
|
|
return true
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 05:23:15 +02:00
|
|
|
// Having no updates since last collection, try to unmap:
|
|
|
|
if unmapped := inuse.refMapped.tryUnmap(); !unmapped {
|
|
|
|
// The record is referenced by a binding, continue.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any other goroutines are now trying to re-insert this
|
|
|
|
// entry in the map, they are busy calling Gosched() awaiting
|
|
|
|
// this deletion:
|
|
|
|
m.current.Delete(inuse.mapkey())
|
|
|
|
|
|
|
|
// There's a potential race between `LoadInt64` and
|
|
|
|
// `tryUnmap` in this function. Since this is the
|
|
|
|
// last we'll see of this record, checkpoint
|
|
|
|
mods = atomic.LoadInt64(&inuse.updateCount)
|
|
|
|
if mods != coll {
|
2020-06-09 20:00:50 +02:00
|
|
|
checkpointed += m.checkpointRecord(inuse)
|
2020-04-22 05:23:15 +02:00
|
|
|
}
|
2020-02-07 00:45:56 +02:00
|
|
|
return true
|
|
|
|
})
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
return checkpointed
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
func (m *Accumulator) runAsyncCallbacks(ctx context.Context) {
|
|
|
|
m.callbackLock.Lock()
|
|
|
|
defer m.callbackLock.Unlock()
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
ctx = context.WithValue(ctx, asyncContextKey{}, m)
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
for cb := range m.callbacks {
|
|
|
|
cb.f(ctx)
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 20:00:50 +02:00
|
|
|
func (m *Accumulator) checkpointRecord(r *record) int {
|
2020-06-13 09:55:01 +02:00
|
|
|
if r.current == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2020-06-23 19:41:11 +02:00
|
|
|
err := r.current.SynchronizedMove(r.checkpoint, &r.inst.descriptor)
|
2020-06-13 09:55:01 +02:00
|
|
|
if err != nil {
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.Handle(err)
|
2020-06-13 09:55:01 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-04-18 16:31:31 +02:00
|
|
|
a := export.NewAccumulation(&r.inst.descriptor, &r.attrs, r.checkpoint)
|
2020-06-23 21:00:15 +02:00
|
|
|
err = m.processor.Process(a)
|
2020-06-13 09:55:01 +02:00
|
|
|
if err != nil {
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.Handle(err)
|
2020-06-13 09:55:01 +02:00
|
|
|
}
|
|
|
|
return 1
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:50:29 +02:00
|
|
|
func (r *record) captureOne(ctx context.Context, num number.Number) {
|
2020-06-13 09:55:01 +02:00
|
|
|
if r.current == nil {
|
2020-06-23 19:51:15 +02:00
|
|
|
// The instrument is disabled according to the AggregatorSelector.
|
2019-11-15 23:01:20 +02:00
|
|
|
return
|
|
|
|
}
|
2020-11-11 17:24:12 +02:00
|
|
|
if err := aggregator.RangeTest(num, &r.inst.descriptor); err != nil {
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.Handle(err)
|
2019-11-15 23:01:20 +02:00
|
|
|
return
|
|
|
|
}
|
2020-11-11 17:24:12 +02:00
|
|
|
if err := r.current.Update(ctx, num, &r.inst.descriptor); err != nil {
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.Handle(err)
|
2019-11-15 23:01:20 +02:00
|
|
|
return
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
2020-04-22 05:23:15 +02:00
|
|
|
// Record was modified, inform the Collect() that things need
|
|
|
|
// to be collected while the record is still mapped.
|
|
|
|
atomic.AddInt64(&r.updateCount, 1)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2021-11-17 17:51:04 +02:00
|
|
|
func (r *record) unbind() {
|
2020-02-07 00:45:56 +02:00
|
|
|
r.refMapped.unref()
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *record) mapkey() mapkey {
|
|
|
|
return mapkey{
|
2020-03-19 21:02:46 +02:00
|
|
|
descriptor: &r.inst.descriptor,
|
2022-04-18 16:31:31 +02:00
|
|
|
ordered: r.attrs.Equivalent(),
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 19:19:51 +02:00
|
|
|
|
|
|
|
// fromSync gets an async implementation object, checking for
|
|
|
|
// uninitialized instruments and instruments created by another SDK.
|
2022-03-02 17:50:29 +02:00
|
|
|
func (m *Accumulator) fromAsync(async sdkapi.AsyncImpl) (*asyncInstrument, error) {
|
|
|
|
if async == nil {
|
|
|
|
return nil, ErrUninitializedInstrument
|
2020-05-20 19:19:51 +02:00
|
|
|
}
|
2022-03-02 17:50:29 +02:00
|
|
|
inst, ok := async.Implementation().(*asyncInstrument)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrBadInstrument
|
|
|
|
}
|
|
|
|
return inst, nil
|
|
|
|
|
2020-05-20 19:19:51 +02:00
|
|
|
}
|