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.
|
|
|
|
|
2019-11-27 00:07:58 +02:00
|
|
|
package minmaxsumcount // import "go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-29 03:27:13 +02:00
|
|
|
"sync"
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-06-10 07:53:30 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-05-16 07:11:12 +02:00
|
|
|
// Aggregator aggregates events that form a distribution,
|
|
|
|
// keeping only the min, max, sum, and count.
|
2019-10-29 22:27:22 +02:00
|
|
|
Aggregator struct {
|
2020-06-13 09:55:01 +02:00
|
|
|
lock sync.Mutex
|
2020-11-11 17:24:12 +02:00
|
|
|
kind number.Kind
|
2020-06-13 09:55:01 +02:00
|
|
|
state
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state struct {
|
2020-11-11 17:24:12 +02:00
|
|
|
sum number.Number
|
|
|
|
min number.Number
|
|
|
|
max number.Number
|
2021-01-06 09:17:20 +02:00
|
|
|
count uint64
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
var _ export.Aggregator = &Aggregator{}
|
2020-06-10 07:53:30 +02:00
|
|
|
var _ aggregation.MinMaxSumCount = &Aggregator{}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
// New returns a new aggregator for computing the min, max, sum, and
|
2021-01-12 20:19:13 +02:00
|
|
|
// count.
|
2019-11-15 23:01:20 +02:00
|
|
|
//
|
2020-06-23 19:41:11 +02:00
|
|
|
// This type uses a mutex for Update() and SynchronizedMove() concurrency.
|
2020-11-12 17:28:32 +02:00
|
|
|
func New(cnt int, desc *metric.Descriptor) []Aggregator {
|
2020-03-13 00:43:19 +02:00
|
|
|
kind := desc.NumberKind()
|
2020-06-13 09:55:01 +02:00
|
|
|
aggs := make([]Aggregator, cnt)
|
|
|
|
for i := range aggs {
|
|
|
|
aggs[i] = Aggregator{
|
|
|
|
kind: kind,
|
|
|
|
state: emptyState(kind),
|
|
|
|
}
|
2019-11-25 19:51:49 +02:00
|
|
|
}
|
2020-06-13 09:55:01 +02:00
|
|
|
return aggs
|
2019-11-25 19:51:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 19:16:33 +02:00
|
|
|
// Aggregation returns an interface for reading the state of this aggregator.
|
|
|
|
func (c *Aggregator) Aggregation() aggregation.Aggregation {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-06-10 09:32:14 +02:00
|
|
|
// Kind returns aggregation.MinMaxSumCountKind.
|
|
|
|
func (c *Aggregator) Kind() aggregation.Kind {
|
|
|
|
return aggregation.MinMaxSumCountKind
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Sum returns the sum of values in the checkpoint.
|
2020-11-11 17:24:12 +02:00
|
|
|
func (c *Aggregator) Sum() (number.Number, error) {
|
2020-06-13 09:55:01 +02:00
|
|
|
return c.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.
|
2021-01-06 09:17:20 +02:00
|
|
|
func (c *Aggregator) Count() (uint64, error) {
|
2020-06-13 09:55:01 +02:00
|
|
|
return c.count, nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 00:07:58 +02:00
|
|
|
// Min returns the minimum value in the checkpoint.
|
2020-06-10 07:53:30 +02:00
|
|
|
// The error value aggregation.ErrNoData will be returned
|
2020-03-13 00:43:19 +02:00
|
|
|
// if there were no measurements recorded during the checkpoint.
|
2020-11-11 17:24:12 +02:00
|
|
|
func (c *Aggregator) Min() (number.Number, error) {
|
2020-06-13 09:55:01 +02:00
|
|
|
if c.count == 0 {
|
|
|
|
return 0, aggregation.ErrNoData
|
2019-11-27 00:07:58 +02:00
|
|
|
}
|
2020-06-13 09:55:01 +02:00
|
|
|
return c.min, nil
|
2019-11-27 00:07:58 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Max returns the maximum value in the checkpoint.
|
2020-06-10 07:53:30 +02:00
|
|
|
// The error value aggregation.ErrNoData will be returned
|
2020-03-13 00:43:19 +02:00
|
|
|
// if there were no measurements recorded during the checkpoint.
|
2020-11-11 17:24:12 +02:00
|
|
|
func (c *Aggregator) Max() (number.Number, error) {
|
2020-06-13 09:55:01 +02:00
|
|
|
if c.count == 0 {
|
|
|
|
return 0, aggregation.ErrNoData
|
2019-11-25 19:51:49 +02:00
|
|
|
}
|
2020-06-13 09:55:01 +02:00
|
|
|
return c.max, nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:41:11 +02:00
|
|
|
// SynchronizedMove saves the current state into oa and resets the current state to
|
2020-03-13 00:43:19 +02:00
|
|
|
// the empty set.
|
2020-11-12 17:28:32 +02:00
|
|
|
func (c *Aggregator) SynchronizedMove(oa export.Aggregator, desc *metric.Descriptor) error {
|
2020-06-13 09:55:01 +02:00
|
|
|
o, _ := oa.(*Aggregator)
|
2020-12-11 04:13:08 +02:00
|
|
|
|
|
|
|
if oa != nil && o == nil {
|
2020-06-13 09:55:01 +02:00
|
|
|
return aggregator.NewInconsistentAggregatorError(c, oa)
|
|
|
|
}
|
2020-04-29 03:27:13 +02:00
|
|
|
c.lock.Lock()
|
2020-12-11 04:13:08 +02:00
|
|
|
if o != nil {
|
|
|
|
o.state = c.state
|
|
|
|
}
|
|
|
|
c.state = emptyState(c.kind)
|
2020-04-29 03:27:13 +02:00
|
|
|
c.lock.Unlock()
|
2020-06-13 09:55:01 +02:00
|
|
|
|
|
|
|
return nil
|
2020-03-13 00:43:19 +02:00
|
|
|
}
|
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
func emptyState(kind number.Kind) state {
|
2020-04-29 03:27:13 +02:00
|
|
|
return state{
|
2020-06-13 09:55:01 +02:00
|
|
|
count: 0,
|
|
|
|
sum: 0,
|
2020-04-29 03:27:13 +02:00
|
|
|
min: kind.Maximum(),
|
|
|
|
max: 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.
|
2020-11-12 17:28:32 +02:00
|
|
|
func (c *Aggregator) Update(_ context.Context, number number.Number, desc *metric.Descriptor) error {
|
2019-10-29 22:27:22 +02:00
|
|
|
kind := desc.NumberKind()
|
|
|
|
|
2020-04-29 03:27:13 +02:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2020-06-13 09:55:01 +02:00
|
|
|
c.count++
|
|
|
|
c.sum.AddNumber(kind, number)
|
|
|
|
if number.CompareNumber(kind, c.min) < 0 {
|
|
|
|
c.min = number
|
2019-11-27 00:07:58 +02:00
|
|
|
}
|
2020-06-13 09:55:01 +02:00
|
|
|
if number.CompareNumber(kind, c.max) > 0 {
|
|
|
|
c.max = number
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
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.
|
2020-11-12 17:28:32 +02:00
|
|
|
func (c *Aggregator) Merge(oa export.Aggregator, desc *metric.Descriptor) error {
|
2019-10-31 07:15:27 +02:00
|
|
|
o, _ := oa.(*Aggregator)
|
|
|
|
if o == nil {
|
2020-06-13 09:55:01 +02:00
|
|
|
return aggregator.NewInconsistentAggregatorError(c, oa)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
c.count += o.count
|
|
|
|
c.sum.AddNumber(desc.NumberKind(), o.sum)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
if c.min.CompareNumber(desc.NumberKind(), o.min) > 0 {
|
|
|
|
c.min.SetNumber(o.min)
|
2019-11-27 00:07:58 +02:00
|
|
|
}
|
2020-06-13 09:55:01 +02:00
|
|
|
if c.max.CompareNumber(desc.NumberKind(), o.max) < 0 {
|
|
|
|
c.max.SetNumber(o.max)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
return nil
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|