2019-11-15 23:01:20 +02: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.
|
|
|
|
|
2020-01-15 16:17:18 +02:00
|
|
|
package aggregator_test // import "go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/export/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(),
|
|
|
|
)
|
|
|
|
require.True(t, errors.Is(err, aggregator.ErrInconsistentType))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRangeNaN(t *testing.T, desc *export.Descriptor) {
|
|
|
|
// If the descriptor uses int64 numbers, this won't register as NaN
|
|
|
|
nan := core.NewFloat64Number(math.NaN())
|
|
|
|
err := aggregator.RangeTest(nan, desc)
|
|
|
|
|
|
|
|
if desc.NumberKind() == core.Float64NumberKind {
|
|
|
|
require.Equal(t, aggregator.ErrNaNInput, err)
|
|
|
|
} else {
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func testRangeNegative(t *testing.T, desc *export.Descriptor) {
|
2019-11-15 23:01:20 +02:00
|
|
|
var neg, pos core.Number
|
|
|
|
|
|
|
|
if desc.NumberKind() == core.Float64NumberKind {
|
|
|
|
pos = core.NewFloat64Number(+1)
|
|
|
|
neg = core.NewFloat64Number(-1)
|
|
|
|
} else {
|
|
|
|
pos = core.NewInt64Number(+1)
|
|
|
|
neg = core.NewInt64Number(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
posErr := aggregator.RangeTest(pos, desc)
|
|
|
|
negErr := aggregator.RangeTest(neg, desc)
|
|
|
|
|
|
|
|
require.Nil(t, posErr)
|
2020-03-12 05:21:34 +02:00
|
|
|
require.Equal(t, negErr, aggregator.ErrNegativeInput)
|
|
|
|
}
|
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.
|
|
|
|
for _, nkind := range []core.NumberKind{core.Float64NumberKind, core.Int64NumberKind} {
|
|
|
|
t.Run(nkind.String(), func(t *testing.T) {
|
|
|
|
desc := export.NewDescriptor(
|
|
|
|
"name",
|
|
|
|
export.CounterKind,
|
|
|
|
nil,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
nkind,
|
|
|
|
)
|
|
|
|
testRangeNegative(t, desc)
|
|
|
|
})
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func TestNaNTest(t *testing.T) {
|
2019-11-15 23:01:20 +02:00
|
|
|
for _, nkind := range []core.NumberKind{core.Float64NumberKind, core.Int64NumberKind} {
|
|
|
|
t.Run(nkind.String(), func(t *testing.T) {
|
2020-01-16 20:05:12 +02:00
|
|
|
for _, mkind := range []export.Kind{
|
2019-11-15 23:01:20 +02:00
|
|
|
export.CounterKind,
|
|
|
|
export.MeasureKind,
|
2020-03-11 01:00:37 +02:00
|
|
|
export.ObserverKind,
|
2019-11-15 23:01:20 +02:00
|
|
|
} {
|
2020-03-12 05:21:34 +02:00
|
|
|
desc := export.NewDescriptor(
|
|
|
|
"name",
|
|
|
|
mkind,
|
|
|
|
nil,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
nkind,
|
|
|
|
)
|
|
|
|
testRangeNaN(t, desc)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|