2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-15 13:01:20 -08: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.
|
|
|
|
|
2020-01-07 15:30:41 +00:00
|
|
|
package aggregator // import "go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2019-11-15 13:01:20 -08:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These interfaces describe the various ways to access state from an
|
|
|
|
// Aggregator.
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Sum returns an aggregated sum.
|
|
|
|
Sum interface {
|
2020-05-11 14:44:42 +08:00
|
|
|
Sum() (metric.Number, error)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sum returns the number of values that were aggregated.
|
|
|
|
Count interface {
|
|
|
|
Count() (int64, error)
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:07:58 -08:00
|
|
|
// Min returns the minimum value over the set of values that were aggregated.
|
|
|
|
Min interface {
|
2020-05-11 14:44:42 +08:00
|
|
|
Min() (metric.Number, error)
|
2019-11-26 14:07:58 -08:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:01:20 -08:00
|
|
|
// Max returns the maximum value over the set of values that were aggregated.
|
|
|
|
Max interface {
|
2020-05-11 14:44:42 +08:00
|
|
|
Max() (metric.Number, error)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quantile returns an exact or estimated quantile over the
|
|
|
|
// set of values that were aggregated.
|
|
|
|
Quantile interface {
|
2020-05-11 14:44:42 +08:00
|
|
|
Quantile(float64) (metric.Number, error)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LastValue returns the latest value that was aggregated.
|
|
|
|
LastValue interface {
|
2020-05-11 14:44:42 +08:00
|
|
|
LastValue() (metric.Number, time.Time, error)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:46:05 -08:00
|
|
|
// Points returns the raw set of values that were aggregated.
|
|
|
|
Points interface {
|
2020-05-11 14:44:42 +08:00
|
|
|
Points() ([]metric.Number, error)
|
2019-11-21 20:46:05 -08:00
|
|
|
}
|
|
|
|
|
2020-01-21 14:15:09 -03:00
|
|
|
// Buckets represents histogram buckets boundaries and counts.
|
|
|
|
//
|
|
|
|
// For a Histogram with N defined boundaries, e.g, [x, y, z].
|
|
|
|
// There are N+1 counts: [-inf, x), [x, y), [y, z), [z, +inf]
|
|
|
|
Buckets struct {
|
2020-05-21 10:29:03 -07:00
|
|
|
Boundaries []float64
|
|
|
|
Counts []float64
|
2020-01-21 14:15:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Histogram returns the count of events in pre-determined buckets.
|
|
|
|
Histogram interface {
|
2020-04-01 23:36:37 +02:00
|
|
|
Sum
|
2020-01-21 14:15:09 -03:00
|
|
|
Histogram() (Buckets, error)
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:07:58 -08:00
|
|
|
// MinMaxSumCount supports the Min, Max, Sum, and Count interfaces.
|
|
|
|
MinMaxSumCount interface {
|
|
|
|
Min
|
|
|
|
Max
|
2019-11-15 13:01:20 -08:00
|
|
|
Sum
|
|
|
|
Count
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:07:58 -08:00
|
|
|
// Distribution supports the Min, Max, Sum, Count, and Quantile
|
2019-11-15 13:01:20 -08:00
|
|
|
// interfaces.
|
|
|
|
Distribution interface {
|
2019-11-26 14:07:58 -08:00
|
|
|
MinMaxSumCount
|
2019-11-15 13:01:20 -08:00
|
|
|
Quantile
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-12-09 13:03:11 -08:00
|
|
|
ErrInvalidQuantile = fmt.Errorf("the requested quantile is out of range")
|
|
|
|
ErrNegativeInput = fmt.Errorf("negative value is out of range for this instrument")
|
2019-11-15 13:01:20 -08:00
|
|
|
ErrNaNInput = fmt.Errorf("NaN value is an invalid input")
|
2019-12-09 13:03:11 -08:00
|
|
|
ErrInconsistentType = fmt.Errorf("inconsistent aggregator types")
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-03-16 16:28:33 -07:00
|
|
|
// ErrNoData is returned when (due to a race with collection)
|
|
|
|
// the Aggregator is check-pointed before the first value is set.
|
|
|
|
// The aggregator should simply be skipped in this case.
|
|
|
|
ErrNoData = fmt.Errorf("no data collected by this aggregator")
|
2019-11-15 13:01:20 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewInconsistentMergeError formats an error describing an attempt to
|
|
|
|
// merge different-type aggregators. The result can be unwrapped as
|
|
|
|
// an ErrInconsistentType.
|
|
|
|
func NewInconsistentMergeError(a1, a2 export.Aggregator) error {
|
2019-12-09 13:03:11 -08:00
|
|
|
return fmt.Errorf("cannot merge %T with %T: %w", a1, a2, ErrInconsistentType)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RangeTest is a commmon routine for testing for valid input values.
|
|
|
|
// This rejects NaN values. This rejects negative values when the
|
|
|
|
// metric instrument does not support negative values, including
|
2020-05-15 22:11:12 -07:00
|
|
|
// monotonic counter metrics and absolute ValueRecorder metrics.
|
2020-05-11 14:44:42 +08:00
|
|
|
func RangeTest(number metric.Number, descriptor *metric.Descriptor) error {
|
2019-11-15 13:01:20 -08:00
|
|
|
numberKind := descriptor.NumberKind()
|
|
|
|
|
2020-05-11 14:44:42 +08:00
|
|
|
if numberKind == metric.Float64NumberKind && math.IsNaN(number.AsFloat64()) {
|
2019-11-15 13:01:20 -08:00
|
|
|
return ErrNaNInput
|
|
|
|
}
|
|
|
|
|
|
|
|
switch descriptor.MetricKind() {
|
2020-05-19 11:49:24 -07:00
|
|
|
case metric.CounterKind, metric.SumObserverKind:
|
2020-03-11 20:21:34 -07:00
|
|
|
if number.IsNegative(numberKind) {
|
2019-11-15 13:01:20 -08:00
|
|
|
return ErrNegativeInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|