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.
|
|
|
|
|
2019-11-04 14:24:01 -08:00
|
|
|
package counter // import "go.opentelemetry.io/otel/sdk/metric/aggregator/counter"
|
2019-10-29 13:27:22 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-01 11:40:29 -07:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
"go.opentelemetry.io/otel/sdk/export"
|
2019-10-29 13:27:22 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Aggregator aggregates counter events.
|
|
|
|
type Aggregator struct {
|
|
|
|
// current holds current increments to this counter record
|
|
|
|
current core.Number
|
|
|
|
|
|
|
|
// checkpoint is a temporary used during Collect()
|
|
|
|
checkpoint core.Number
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ export.MetricAggregator = &Aggregator{}
|
|
|
|
|
|
|
|
// New returns a new counter aggregator. This aggregator computes an
|
|
|
|
// atomic sum.
|
|
|
|
func New() *Aggregator {
|
|
|
|
return &Aggregator{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsNumber returns the accumulated count as an int64.
|
|
|
|
func (c *Aggregator) AsNumber() core.Number {
|
|
|
|
return c.checkpoint.AsNumber()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect checkpoints the current value (atomically) and exports it.
|
|
|
|
func (c *Aggregator) Collect(ctx context.Context, rec export.MetricRecord, exp export.MetricBatcher) {
|
2019-10-30 11:22:14 -07:00
|
|
|
c.checkpoint = c.current.SwapNumberAtomic(core.Number(0))
|
2019-10-29 13:27:22 -07:00
|
|
|
|
|
|
|
exp.Export(ctx, rec, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update modifies the current value (atomically) for later export.
|
|
|
|
func (c *Aggregator) Update(_ context.Context, number core.Number, rec export.MetricRecord) {
|
|
|
|
desc := rec.Descriptor()
|
|
|
|
kind := desc.NumberKind()
|
|
|
|
if !desc.Alternate() && number.IsNegative(kind) {
|
|
|
|
// TODO warn
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.current.AddNumberAtomic(kind, number)
|
|
|
|
}
|
2019-10-30 22:15:27 -07:00
|
|
|
|
|
|
|
func (c *Aggregator) Merge(oa export.MetricAggregator, desc *export.Descriptor) {
|
|
|
|
o, _ := oa.(*Aggregator)
|
|
|
|
if o == nil {
|
|
|
|
// TODO warn
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.checkpoint.AddNumber(desc.NumberKind(), o.checkpoint)
|
|
|
|
}
|