1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-03-05 15:05:51 +02:00

Update aggregation.go/aggregator.go

This commit is contained in:
jmacd 2020-06-09 22:41:51 -07:00
parent e53841a4b4
commit da2bdb8249
3 changed files with 82 additions and 34 deletions

View File

@ -12,21 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package aggregator // import "go.opentelemetry.io/otel/sdk/export/metric/aggregator"
package aggregation // import "go.opentelemetry.io/otel/sdk/export/metric/aggregation"
import (
"fmt"
"math"
"time"
"go.opentelemetry.io/otel/api/metric"
export "go.opentelemetry.io/otel/sdk/export/metric"
)
// These interfaces describe the various ways to access state from an
// Aggregator.
// Aggregation.
type (
// Aggregation is one of the interfaces below.
Aggregation interface {
// Kind returns the kind of aggregation used.
Kind() Kind
}
// Sum returns an aggregated sum.
Sum interface {
Sum() (metric.Number, error)
@ -100,11 +104,25 @@ type (
}
)
type (
Kind string
)
const (
SumKind Kind = "sum"
MinMaxSumCountKind Kind = "minmaxsumcount"
HistogramKind Kind = "histogram"
LastValueKind Kind = "lastvalue"
SketchKind Kind = "sketch"
ExactKind Kind = "exact"
)
var (
ErrInvalidQuantile = fmt.Errorf("the requested quantile is out of range")
ErrNegativeInput = fmt.Errorf("negative value is out of range for this instrument")
ErrNaNInput = fmt.Errorf("NaN value is an invalid input")
ErrInconsistentType = fmt.Errorf("inconsistent aggregator types")
ErrNoSubtraction = fmt.Errorf("aggregator does not subtract")
// ErrNoData is returned when (due to a race with collection)
// the Aggregator is check-pointed before the first value is set.
@ -112,29 +130,7 @@ var (
ErrNoData = fmt.Errorf("no data collected by this aggregator")
)
// 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 {
return fmt.Errorf("cannot merge %T with %T: %w", a1, a2, ErrInconsistentType)
}
// 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
// monotonic counter metrics and absolute ValueRecorder metrics.
func RangeTest(number metric.Number, descriptor *metric.Descriptor) error {
numberKind := descriptor.NumberKind()
if numberKind == metric.Float64NumberKind && math.IsNaN(number.AsFloat64()) {
return ErrNaNInput
}
switch descriptor.MetricKind() {
case metric.CounterKind, metric.SumObserverKind:
if number.IsNegative(numberKind) {
return ErrNegativeInput
}
}
return nil
// String returns a string representation of the aggregation kind.
func (k Kind) String() string {
return string(k)
}

View File

@ -0,0 +1,51 @@
// Copyright The 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.
package aggregator // import "go.opentelemetry.io/otel/sdk/metric/aggregator"
import (
"fmt"
"math"
"go.opentelemetry.io/otel/api/metric"
export "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
)
// 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 {
return fmt.Errorf("cannot merge %T with %T: %w", a1, a2, aggregation.ErrInconsistentType)
}
// 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
// monotonic counter metrics and absolute ValueRecorder metrics.
func RangeTest(number metric.Number, descriptor *metric.Descriptor) error {
numberKind := descriptor.NumberKind()
if numberKind == metric.Float64NumberKind && math.IsNaN(number.AsFloat64()) {
return aggregation.ErrNaNInput
}
switch descriptor.MetricKind() {
case metric.CounterKind, metric.SumObserverKind:
if number.IsNegative(numberKind) {
return aggregation.ErrNegativeInput
}
}
return nil
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package aggregator_test // import "go.opentelemetry.io/otel/sdk/export/metric/aggregator"
package aggregator_test // import "go.opentelemetry.io/otel/sdk/metric/aggregator"
import (
"errors"
@ -22,7 +22,8 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/aggregator"
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
)
@ -34,7 +35,7 @@ func TestInconsistentMergeErr(t *testing.T) {
"cannot merge *sum.Aggregator with *lastvalue.Aggregator: inconsistent aggregator types",
err.Error(),
)
require.True(t, errors.Is(err, aggregator.ErrInconsistentType))
require.True(t, errors.Is(err, aggregation.ErrInconsistentType))
}
func testRangeNaN(t *testing.T, desc *metric.Descriptor) {
@ -43,7 +44,7 @@ func testRangeNaN(t *testing.T, desc *metric.Descriptor) {
err := aggregator.RangeTest(nan, desc)
if desc.NumberKind() == metric.Float64NumberKind {
require.Equal(t, aggregator.ErrNaNInput, err)
require.Equal(t, aggregation.ErrNaNInput, err)
} else {
require.Nil(t, err)
}
@ -64,7 +65,7 @@ func testRangeNegative(t *testing.T, desc *metric.Descriptor) {
negErr := aggregator.RangeTest(neg, desc)
require.Nil(t, posErr)
require.Equal(t, negErr, aggregator.ErrNegativeInput)
require.Equal(t, negErr, aggregation.ErrNegativeInput)
}
func TestRangeTest(t *testing.T) {