2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-15 23:01:20 +02: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-06-10 07:41:51 +02:00
|
|
|
package aggregator_test // import "go.opentelemetry.io/otel/sdk/metric/aggregator"
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2021-09-27 17:51:47 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/metrictest"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2021-08-12 01:02:28 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/sdkapi"
|
2020-06-10 07:41:51 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator"
|
2020-03-11 01:00:37 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
|
2020-03-12 05:21:34 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
func TestInconsistentAggregatorErr(t *testing.T) {
|
|
|
|
err := aggregator.NewInconsistentAggregatorError(&sum.New(1)[0], &lastvalue.New(1)[0])
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(
|
|
|
|
t,
|
2020-06-13 09:55:01 +02:00
|
|
|
"inconsistent aggregator types: *sum.Aggregator and *lastvalue.Aggregator",
|
2019-11-15 23:01:20 +02:00
|
|
|
err.Error(),
|
|
|
|
)
|
2020-06-10 07:41:51 +02:00
|
|
|
require.True(t, errors.Is(err, aggregation.ErrInconsistentType))
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
func testRangeNaN(t *testing.T, desc *metric.Descriptor) {
|
2019-11-15 23:01:20 +02:00
|
|
|
// If the descriptor uses int64 numbers, this won't register as NaN
|
2020-11-11 17:24:12 +02:00
|
|
|
nan := number.NewFloat64Number(math.NaN())
|
2019-11-15 23:01:20 +02:00
|
|
|
err := aggregator.RangeTest(nan, desc)
|
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
if desc.NumberKind() == number.Float64Kind {
|
2020-06-10 07:41:51 +02:00
|
|
|
require.Equal(t, aggregation.ErrNaNInput, err)
|
2019-11-15 23:01:20 +02:00
|
|
|
} else {
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
func testRangeNegative(t *testing.T, desc *metric.Descriptor) {
|
2020-11-11 17:24:12 +02:00
|
|
|
var neg, pos number.Number
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
if desc.NumberKind() == number.Float64Kind {
|
|
|
|
pos = number.NewFloat64Number(+1)
|
|
|
|
neg = number.NewFloat64Number(-1)
|
2019-11-15 23:01:20 +02:00
|
|
|
} else {
|
2020-11-11 17:24:12 +02:00
|
|
|
pos = number.NewInt64Number(+1)
|
|
|
|
neg = number.NewInt64Number(-1)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
posErr := aggregator.RangeTest(pos, desc)
|
|
|
|
negErr := aggregator.RangeTest(neg, desc)
|
|
|
|
|
|
|
|
require.Nil(t, posErr)
|
2020-06-10 07:41:51 +02:00
|
|
|
require.Equal(t, negErr, aggregation.ErrNegativeInput)
|
2020-03-12 05:21:34 +02:00
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func TestRangeTest(t *testing.T) {
|
|
|
|
// Only Counters implement a range test.
|
2020-11-11 17:24:12 +02:00
|
|
|
for _, nkind := range []number.Kind{number.Float64Kind, number.Int64Kind} {
|
2020-03-12 05:21:34 +02:00
|
|
|
t.Run(nkind.String(), func(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
desc := metrictest.NewDescriptor(
|
2020-03-12 05:21:34 +02:00
|
|
|
"name",
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
2020-03-12 05:21:34 +02:00
|
|
|
nkind,
|
|
|
|
)
|
2020-03-19 21:02:46 +02:00
|
|
|
testRangeNegative(t, &desc)
|
2020-03-12 05:21:34 +02:00
|
|
|
})
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func TestNaNTest(t *testing.T) {
|
2020-11-11 17:24:12 +02:00
|
|
|
for _, nkind := range []number.Kind{number.Float64Kind, number.Int64Kind} {
|
2019-11-15 23:01:20 +02:00
|
|
|
t.Run(nkind.String(), func(t *testing.T) {
|
2021-08-12 01:02:28 +02:00
|
|
|
for _, mkind := range []sdkapi.InstrumentKind{
|
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.HistogramInstrumentKind,
|
|
|
|
sdkapi.GaugeObserverInstrumentKind,
|
2019-11-15 23:01:20 +02:00
|
|
|
} {
|
2021-09-27 17:51:47 +02:00
|
|
|
desc := metrictest.NewDescriptor(
|
2020-03-12 05:21:34 +02:00
|
|
|
"name",
|
|
|
|
mkind,
|
|
|
|
nkind,
|
|
|
|
)
|
2020-03-19 21:02:46 +02:00
|
|
|
testRangeNaN(t, &desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|