2019-10-29 13:27:22 -07: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.
|
|
|
|
|
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"
|
|
|
|
|
2019-11-01 11:40:29 -07:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2020-03-19 12:02:46 -07:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2019-11-05 13:08:55 -08:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2019-11-15 13:01:20 -08:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/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.
|
2019-10-29 13:27:22 -07:00
|
|
|
current core.Number
|
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// checkpoint is a temporary used during Checkpoint()
|
2020-01-06 10:08:40 -08:00
|
|
|
// checkpoint needs to be aligned for 64-bit atomic operations.
|
2019-10-29 13:27:22 -07:00
|
|
|
checkpoint core.Number
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:08:55 -08:00
|
|
|
var _ export.Aggregator = &Aggregator{}
|
2019-11-15 13:01:20 -08:00
|
|
|
var _ aggregator.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
|
|
|
|
// operations. This aggregator implements the aggregator.Sum
|
|
|
|
// export interface.
|
2019-10-29 13:27:22 -07:00
|
|
|
func New() *Aggregator {
|
|
|
|
return &Aggregator{}
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// Sum returns the last-checkpointed sum. This will never return an
|
|
|
|
// error.
|
|
|
|
func (c *Aggregator) Sum() (core.Number, error) {
|
|
|
|
return c.checkpoint, nil
|
2019-10-29 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// Checkpoint atomically saves the current value and resets the
|
|
|
|
// current sum to zero.
|
2020-03-19 12:02:46 -07:00
|
|
|
func (c *Aggregator) Checkpoint(ctx context.Context, _ *metric.Descriptor) {
|
2019-10-30 11:22:14 -07:00
|
|
|
c.checkpoint = c.current.SwapNumberAtomic(core.Number(0))
|
2019-10-29 13:27:22 -07:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// Update atomically adds to the current value.
|
2020-03-19 12:02:46 -07:00
|
|
|
func (c *Aggregator) Update(_ context.Context, number core.Number, desc *metric.Descriptor) error {
|
2019-11-15 13:01:20 -08:00
|
|
|
c.current.AddNumberAtomic(desc.NumberKind(), number)
|
|
|
|
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-03-19 12:02:46 -07: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 {
|
2019-11-15 13:01:20 -08:00
|
|
|
return aggregator.NewInconsistentMergeError(c, oa)
|
2019-10-30 22:15:27 -07:00
|
|
|
}
|
|
|
|
c.checkpoint.AddNumber(desc.NumberKind(), o.checkpoint)
|
2019-11-15 13:01:20 -08:00
|
|
|
return nil
|
2019-10-30 22:15:27 -07:00
|
|
|
}
|