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-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-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state struct {
|
|
|
|
count core.Number
|
|
|
|
sum core.Number
|
|
|
|
max core.Number
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
var _ export.Aggregator = &Aggregator{}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
// New returns a new measure aggregator for computing max, sum, and count.
|
|
|
|
func New() *Aggregator {
|
|
|
|
return &Aggregator{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sum returns the accumulated sum as a Number.
|
|
|
|
func (c *Aggregator) Sum() core.Number {
|
2019-10-31 07:15:27 +02:00
|
|
|
return c.checkpoint.sum
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the accumulated count.
|
|
|
|
func (c *Aggregator) Count() int64 {
|
2019-10-31 07:15:27 +02:00
|
|
|
return int64(c.checkpoint.count.AsUint64())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Max returns the accumulated max as a Number.
|
2019-11-05 00:24:01 +02:00
|
|
|
func (c *Aggregator) Max() (core.Number, error) {
|
|
|
|
return c.checkpoint.max, nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:15:27 +02:00
|
|
|
// Collect checkpoints the current value (atomically) and exports it.
|
2019-11-05 23:08:55 +02:00
|
|
|
func (c *Aggregator) Collect(ctx context.Context, rec export.Record, exp export.Batcher) {
|
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))
|
|
|
|
c.checkpoint.max = c.current.max.SwapNumberAtomic(core.Number(0))
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
exp.Export(ctx, rec, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update modifies the current value (atomically) for later export.
|
2019-11-05 23:08:55 +02:00
|
|
|
func (c *Aggregator) Update(_ context.Context, number core.Number, rec export.Record) {
|
2019-10-29 22:27:22 +02:00
|
|
|
desc := rec.Descriptor()
|
|
|
|
kind := desc.NumberKind()
|
|
|
|
|
|
|
|
if !desc.Alternate() && number.IsNegative(kind) {
|
|
|
|
// TODO warn
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
func (c *Aggregator) Merge(oa export.Aggregator, desc *export.Descriptor) {
|
2019-10-31 07:15:27 +02:00
|
|
|
o, _ := oa.(*Aggregator)
|
|
|
|
if o == nil {
|
|
|
|
// TODO warn
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|