2019-10-29 22:27:22 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
package maxsumcount // import "go.opentelemetry.io/otel/sdk/metric/aggregator/maxsumcount"
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2019-11-15 23:01:20 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Aggregator aggregates measure events, keeping only the max,
|
|
|
|
// sum, and count.
|
|
|
|
Aggregator struct {
|
2019-10-31 07:15:27 +02:00
|
|
|
current state
|
|
|
|
checkpoint state
|
2019-11-25 19:51:49 +02:00
|
|
|
kind core.NumberKind
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state struct {
|
|
|
|
count core.Number
|
|
|
|
sum core.Number
|
|
|
|
max core.Number
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// TODO: The SDK specification says this type should support Min
|
|
|
|
// values, see #319.
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
var _ export.Aggregator = &Aggregator{}
|
2019-11-15 23:01:20 +02:00
|
|
|
var _ aggregator.MaxSumCount = &Aggregator{}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// New returns a new measure aggregator for computing max, sum, and
|
|
|
|
// count. It does not compute quantile information other than Max.
|
|
|
|
//
|
|
|
|
// Note that this aggregator maintains each value using independent
|
|
|
|
// atomic operations, which introduces the possibility that
|
|
|
|
// checkpoints are inconsistent. For greater consistency and lower
|
|
|
|
// performance, consider using Array or DDSketch aggregators.
|
2019-11-25 19:51:49 +02:00
|
|
|
func New(desc *export.Descriptor) *Aggregator {
|
|
|
|
return &Aggregator{
|
|
|
|
kind: desc.NumberKind(),
|
|
|
|
current: unsetMaxSumCount(desc.NumberKind()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsetMaxSumCount(kind core.NumberKind) state {
|
|
|
|
return state{max: kind.Minimum()}
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Sum returns the sum of values in the checkpoint.
|
|
|
|
func (c *Aggregator) Sum() (core.Number, error) {
|
|
|
|
return c.checkpoint.sum, nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Count returns the number of values in the checkpoint.
|
|
|
|
func (c *Aggregator) Count() (int64, error) {
|
|
|
|
return int64(c.checkpoint.count.AsUint64()), nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Max returns the maximum value in the checkpoint.
|
2019-11-25 19:51:49 +02:00
|
|
|
// The error value aggregator.ErrEmptyDataSet will be returned if
|
|
|
|
// (due to a race condition) the checkpoint was set prior to the
|
|
|
|
// current.max being computed in Update().
|
|
|
|
//
|
|
|
|
// Note: If a measure's recorded values for a given checkpoint are
|
|
|
|
// all equal to NumberKind.Minimum(), Max() will return ErrEmptyDataSet
|
2019-11-05 00:24:01 +02:00
|
|
|
func (c *Aggregator) Max() (core.Number, error) {
|
2019-11-25 19:51:49 +02:00
|
|
|
if c.checkpoint.max == c.kind.Minimum() {
|
|
|
|
return core.Number(0), aggregator.ErrEmptyDataSet
|
|
|
|
}
|
2019-11-05 00:24:01 +02:00
|
|
|
return c.checkpoint.max, nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Checkpoint saves the current state and resets the current state to
|
|
|
|
// the empty set. Since no locks are taken, there is a chance that
|
|
|
|
// the independent Max, Sum, and Count are not consistent with each
|
|
|
|
// other.
|
2019-11-25 19:51:49 +02:00
|
|
|
func (c *Aggregator) Checkpoint(ctx context.Context, desc *export.Descriptor) {
|
2019-10-29 22:27:22 +02:00
|
|
|
// N.B. There is no atomic operation that can update all three
|
2019-10-31 07:15:27 +02:00
|
|
|
// values at once without a memory allocation.
|
|
|
|
//
|
|
|
|
// This aggregator is intended to trade this correctness for
|
|
|
|
// speed.
|
|
|
|
//
|
|
|
|
// Therefore, atomically swap fields independently, knowing
|
|
|
|
// that individually the three parts of this aggregation could
|
|
|
|
// be spread across multiple collections in rare cases.
|
|
|
|
|
|
|
|
c.checkpoint.count.SetUint64(c.current.count.SwapUint64Atomic(0))
|
|
|
|
c.checkpoint.sum = c.current.sum.SwapNumberAtomic(core.Number(0))
|
2019-11-25 19:51:49 +02:00
|
|
|
c.checkpoint.max = c.current.max.SwapNumberAtomic(c.kind.Minimum())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Update adds the recorded measurement to the current data set.
|
|
|
|
func (c *Aggregator) Update(_ context.Context, number core.Number, desc *export.Descriptor) error {
|
2019-10-29 22:27:22 +02:00
|
|
|
kind := desc.NumberKind()
|
|
|
|
|
2019-10-31 07:15:27 +02:00
|
|
|
c.current.count.AddUint64Atomic(1)
|
|
|
|
c.current.sum.AddNumberAtomic(kind, number)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
for {
|
2019-10-31 07:15:27 +02:00
|
|
|
current := c.current.max.AsNumberAtomic()
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
if number.CompareNumber(kind, current) <= 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
if c.current.max.CompareAndSwapNumber(current, number) {
|
2019-10-29 22:27:22 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
return nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Merge combines two data sets into one.
|
|
|
|
func (c *Aggregator) Merge(oa export.Aggregator, desc *export.Descriptor) error {
|
2019-10-31 07:15:27 +02:00
|
|
|
o, _ := oa.(*Aggregator)
|
|
|
|
if o == nil {
|
2019-11-15 23:01:20 +02:00
|
|
|
return aggregator.NewInconsistentMergeError(c, oa)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.checkpoint.sum.AddNumber(desc.NumberKind(), o.checkpoint.sum)
|
|
|
|
c.checkpoint.count.AddNumber(core.Uint64NumberKind, o.checkpoint.count)
|
|
|
|
|
|
|
|
if c.checkpoint.max.CompareNumber(desc.NumberKind(), o.checkpoint.max) < 0 {
|
|
|
|
c.checkpoint.max.SetNumber(o.checkpoint.max)
|
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
return nil
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|