2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-29 13:27:22 -07: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-03-11 20:21:34 -07:00
|
|
|
package sum // import "go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
2019-10-29 13:27:22 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-11-12 16:28:32 +01:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2020-11-11 16:24:12 +01:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2019-11-05 13:08:55 -08:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-06-09 22:53:30 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator"
|
2019-10-29 13:27:22 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Aggregator aggregates counter events.
|
|
|
|
type Aggregator struct {
|
|
|
|
// current holds current increments to this counter record
|
2020-01-06 10:08:40 -08:00
|
|
|
// current needs to be aligned for 64-bit atomic operations.
|
2020-11-11 16:24:12 +01:00
|
|
|
value number.Number
|
2019-10-29 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2019-11-05 13:08:55 -08:00
|
|
|
var _ export.Aggregator = &Aggregator{}
|
2020-06-22 22:59:51 -07:00
|
|
|
var _ export.Subtractor = &Aggregator{}
|
2020-06-09 22:53:30 -07:00
|
|
|
var _ aggregation.Sum = &Aggregator{}
|
2019-10-29 13:27:22 -07:00
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// New returns a new counter aggregator implemented by atomic
|
2020-06-09 22:53:30 -07:00
|
|
|
// operations. This aggregator implements the aggregation.Sum
|
2019-11-15 13:01:20 -08:00
|
|
|
// export interface.
|
2020-06-13 00:55:01 -07:00
|
|
|
func New(cnt int) []Aggregator {
|
|
|
|
return make([]Aggregator, cnt)
|
2019-10-29 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:16:33 -07:00
|
|
|
// Aggregation returns an interface for reading the state of this aggregator.
|
|
|
|
func (c *Aggregator) Aggregation() aggregation.Aggregation {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-06-10 00:32:14 -07:00
|
|
|
// Kind returns aggregation.SumKind.
|
|
|
|
func (c *Aggregator) Kind() aggregation.Kind {
|
|
|
|
return aggregation.SumKind
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// Sum returns the last-checkpointed sum. This will never return an
|
|
|
|
// error.
|
2020-11-11 16:24:12 +01:00
|
|
|
func (c *Aggregator) Sum() (number.Number, error) {
|
2020-06-13 00:55:01 -07:00
|
|
|
return c.value, nil
|
2019-10-29 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:41:11 -07:00
|
|
|
// SynchronizedMove atomically saves the current value into oa and resets the
|
2019-11-15 13:01:20 -08:00
|
|
|
// current sum to zero.
|
2020-11-12 16:28:32 +01:00
|
|
|
func (c *Aggregator) SynchronizedMove(oa export.Aggregator, _ *metric.Descriptor) error {
|
2020-06-13 00:55:01 -07:00
|
|
|
o, _ := oa.(*Aggregator)
|
|
|
|
if o == nil {
|
|
|
|
return aggregator.NewInconsistentAggregatorError(c, oa)
|
|
|
|
}
|
2020-11-11 16:24:12 +01:00
|
|
|
o.value = c.value.SwapNumberAtomic(number.Number(0))
|
2020-06-13 00:55:01 -07:00
|
|
|
return nil
|
2019-10-29 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// Update atomically adds to the current value.
|
2020-11-12 16:28:32 +01:00
|
|
|
func (c *Aggregator) Update(_ context.Context, num number.Number, desc *metric.Descriptor) error {
|
2020-11-11 16:24:12 +01:00
|
|
|
c.value.AddNumberAtomic(desc.NumberKind(), num)
|
2019-11-15 13:01:20 -08:00
|
|
|
return nil
|
2019-10-29 13:27:22 -07:00
|
|
|
}
|
2019-10-30 22:15:27 -07:00
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// Merge combines two counters by adding their sums.
|
2020-11-12 16:28:32 +01:00
|
|
|
func (c *Aggregator) Merge(oa export.Aggregator, desc *metric.Descriptor) error {
|
2019-10-30 22:15:27 -07:00
|
|
|
o, _ := oa.(*Aggregator)
|
|
|
|
if o == nil {
|
2020-06-13 00:55:01 -07:00
|
|
|
return aggregator.NewInconsistentAggregatorError(c, oa)
|
2019-10-30 22:15:27 -07:00
|
|
|
}
|
2020-06-13 00:55:01 -07:00
|
|
|
c.value.AddNumber(desc.NumberKind(), o.value)
|
2019-11-15 13:01:20 -08:00
|
|
|
return nil
|
2019-10-30 22:15:27 -07:00
|
|
|
}
|
2020-06-22 22:59:51 -07:00
|
|
|
|
2020-11-12 16:28:32 +01:00
|
|
|
func (c *Aggregator) Subtract(opAgg, resAgg export.Aggregator, descriptor *metric.Descriptor) error {
|
2020-06-22 22:59:51 -07:00
|
|
|
op, _ := opAgg.(*Aggregator)
|
|
|
|
if op == nil {
|
|
|
|
return aggregator.NewInconsistentAggregatorError(c, opAgg)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, _ := resAgg.(*Aggregator)
|
|
|
|
if res == nil {
|
|
|
|
return aggregator.NewInconsistentAggregatorError(c, resAgg)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.value = c.value
|
2020-11-11 16:24:12 +01:00
|
|
|
res.value.AddNumber(descriptor.NumberKind(), number.NewNumberSignChange(descriptor.NumberKind(), op.value))
|
2020-06-22 22:59:51 -07:00
|
|
|
return nil
|
|
|
|
}
|