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-03-19 21:02:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
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-04-29 03:27:13 +02:00
|
|
|
lock sync.Mutex
|
|
|
|
current state
|
|
|
|
checkpoint state
|
2020-05-11 08:44:42 +02:00
|
|
|
kind metric.NumberKind
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state struct {
|
2020-05-11 08:44:42 +02:00
|
|
|
count metric.Number
|
|
|
|
sum metric.Number
|
|
|
|
min metric.Number
|
|
|
|
max metric.Number
|
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
|
|
|
|
// count. It does not compute quantile information other than Min and
|
|
|
|
// Max.
|
2019-11-15 23:01:20 +02:00
|
|
|
//
|
2020-04-29 03:27:13 +02:00
|
|
|
// This type uses a mutex for Update() and Checkpoint() concurrency.
|
2020-03-19 21:02:46 +02:00
|
|
|
func New(desc *metric.Descriptor) *Aggregator {
|
2020-03-13 00:43:19 +02:00
|
|
|
kind := desc.NumberKind()
|
2019-11-25 19:51:49 +02:00
|
|
|
return &Aggregator{
|
2020-03-13 00:43:19 +02:00
|
|
|
kind: kind,
|
2020-04-29 03:27:13 +02:00
|
|
|
current: state{
|
2020-05-11 08:44:42 +02:00
|
|
|
count: metric.NewUint64Number(0),
|
2020-04-29 03:27:13 +02:00
|
|
|
sum: kind.Zero(),
|
|
|
|
min: kind.Maximum(),
|
|
|
|
max: kind.Minimum(),
|
2020-03-13 00:43:19 +02:00
|
|
|
},
|
2019-11-25 19:51:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
// Sum returns the sum of values in the checkpoint.
|
2020-05-11 08:44:42 +02:00
|
|
|
func (c *Aggregator) Sum() (metric.Number, error) {
|
2020-03-13 00:43:19 +02:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2020-04-29 03:27:13 +02:00
|
|
|
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) {
|
2020-03-13 00:43:19 +02:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2020-05-11 08:44:42 +02:00
|
|
|
return c.checkpoint.count.CoerceToInt64(metric.Uint64NumberKind), 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-05-11 08:44:42 +02:00
|
|
|
func (c *Aggregator) Min() (metric.Number, error) {
|
2020-03-13 00:43:19 +02:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2020-05-11 08:44:42 +02:00
|
|
|
if c.checkpoint.count.IsZero(metric.Uint64NumberKind) {
|
2020-06-10 07:53:30 +02:00
|
|
|
return c.kind.Zero(), aggregation.ErrNoData
|
2019-11-27 00:07:58 +02:00
|
|
|
}
|
2020-04-29 03:27:13 +02:00
|
|
|
return c.checkpoint.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-05-11 08:44:42 +02:00
|
|
|
func (c *Aggregator) Max() (metric.Number, error) {
|
2020-03-13 00:43:19 +02:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2020-05-11 08:44:42 +02:00
|
|
|
if c.checkpoint.count.IsZero(metric.Uint64NumberKind) {
|
2020-06-10 07:53:30 +02:00
|
|
|
return c.kind.Zero(), aggregation.ErrNoData
|
2019-11-25 19:51:49 +02:00
|
|
|
}
|
2020-04-29 03:27:13 +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
|
2020-03-13 00:43:19 +02:00
|
|
|
// the empty set.
|
2020-06-09 20:00:50 +02:00
|
|
|
func (c *Aggregator) Checkpoint(desc *metric.Descriptor) {
|
2020-04-29 03:27:13 +02:00
|
|
|
c.lock.Lock()
|
|
|
|
c.checkpoint, c.current = c.current, c.emptyState()
|
|
|
|
c.lock.Unlock()
|
2020-03-13 00:43:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 03:27:13 +02:00
|
|
|
func (c *Aggregator) emptyState() state {
|
|
|
|
kind := c.kind
|
|
|
|
return state{
|
2020-05-11 08:44:42 +02:00
|
|
|
count: metric.NewUint64Number(0),
|
2020-04-29 03:27:13 +02:00
|
|
|
sum: kind.Zero(),
|
|
|
|
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-05-11 08:44:42 +02:00
|
|
|
func (c *Aggregator) Update(_ context.Context, number metric.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()
|
|
|
|
c.current.count.AddInt64(1)
|
|
|
|
c.current.sum.AddNumber(kind, number)
|
|
|
|
if number.CompareNumber(kind, c.current.min) < 0 {
|
|
|
|
c.current.min = number
|
2019-11-27 00:07:58 +02:00
|
|
|
}
|
2020-04-29 03:27:13 +02:00
|
|
|
if number.CompareNumber(kind, c.current.max) > 0 {
|
|
|
|
c.current.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-03-19 21:02:46 +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 {
|
2019-11-15 23:01:20 +02:00
|
|
|
return aggregator.NewInconsistentMergeError(c, oa)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 08:44:42 +02:00
|
|
|
c.checkpoint.count.AddNumber(metric.Uint64NumberKind, o.checkpoint.count)
|
2020-04-29 03:27:13 +02:00
|
|
|
c.checkpoint.sum.AddNumber(desc.NumberKind(), o.checkpoint.sum)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2020-04-29 03:27:13 +02:00
|
|
|
if c.checkpoint.min.CompareNumber(desc.NumberKind(), o.checkpoint.min) > 0 {
|
|
|
|
c.checkpoint.min.SetNumber(o.checkpoint.min)
|
2019-11-27 00:07:58 +02:00
|
|
|
}
|
2020-04-29 03:27:13 +02:00
|
|
|
if c.checkpoint.max.CompareNumber(desc.NumberKind(), o.checkpoint.max) < 0 {
|
|
|
|
c.checkpoint.max.SetNumber(o.checkpoint.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
|
|
|
}
|