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-03-19 21:02:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func TestInconsistentMergeErr(t *testing.T) {
|
2020-03-12 05:21:34 +02:00
|
|
|
err := aggregator.NewInconsistentMergeError(sum.New(), lastvalue.New())
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(
|
|
|
|
t,
|
2020-03-12 05:21:34 +02:00
|
|
|
"cannot merge *sum.Aggregator with *lastvalue.Aggregator: inconsistent aggregator types",
|
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-03-19 21:02:46 +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-05-11 08:44:42 +02:00
|
|
|
nan := metric.NewFloat64Number(math.NaN())
|
2019-11-15 23:01:20 +02:00
|
|
|
err := aggregator.RangeTest(nan, desc)
|
|
|
|
|
2020-05-11 08:44:42 +02:00
|
|
|
if desc.NumberKind() == metric.Float64NumberKind {
|
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-03-19 21:02:46 +02:00
|
|
|
func testRangeNegative(t *testing.T, desc *metric.Descriptor) {
|
2020-05-11 08:44:42 +02:00
|
|
|
var neg, pos metric.Number
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-05-11 08:44:42 +02:00
|
|
|
if desc.NumberKind() == metric.Float64NumberKind {
|
|
|
|
pos = metric.NewFloat64Number(+1)
|
|
|
|
neg = metric.NewFloat64Number(-1)
|
2019-11-15 23:01:20 +02:00
|
|
|
} else {
|
2020-05-11 08:44:42 +02:00
|
|
|
pos = metric.NewInt64Number(+1)
|
|
|
|
neg = metric.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-05-11 08:44:42 +02:00
|
|
|
for _, nkind := range []metric.NumberKind{metric.Float64NumberKind, metric.Int64NumberKind} {
|
2020-03-12 05:21:34 +02:00
|
|
|
t.Run(nkind.String(), func(t *testing.T) {
|
2020-03-19 21:02:46 +02:00
|
|
|
desc := metric.NewDescriptor(
|
2020-03-12 05:21:34 +02:00
|
|
|
"name",
|
2020-03-19 21:02:46 +02:00
|
|
|
metric.CounterKind,
|
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-05-11 08:44:42 +02:00
|
|
|
for _, nkind := range []metric.NumberKind{metric.Float64NumberKind, metric.Int64NumberKind} {
|
2019-11-15 23:01:20 +02:00
|
|
|
t.Run(nkind.String(), func(t *testing.T) {
|
2020-03-19 21:02:46 +02:00
|
|
|
for _, mkind := range []metric.Kind{
|
|
|
|
metric.CounterKind,
|
2020-05-16 07:11:12 +02:00
|
|
|
metric.ValueRecorderKind,
|
2020-05-18 20:03:43 +02:00
|
|
|
metric.ValueObserverKind,
|
2019-11-15 23:01:20 +02:00
|
|
|
} {
|
2020-03-19 21:02:46 +02:00
|
|
|
desc := metric.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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|