2019-10-29 22:27:22 +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.
|
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2019-11-15 23:01:20 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
const Magnitude = 1000
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
type Profile struct {
|
|
|
|
NumberKind core.NumberKind
|
|
|
|
Random func(sign int) core.Number
|
|
|
|
}
|
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
func newProfiles() []Profile {
|
|
|
|
rnd := rand.New(rand.NewSource(rand.Int63()))
|
|
|
|
return []Profile{
|
|
|
|
{
|
|
|
|
NumberKind: core.Int64NumberKind,
|
|
|
|
Random: func(sign int) core.Number {
|
|
|
|
return core.NewInt64Number(int64(sign) * int64(rnd.Intn(Magnitude+1)))
|
|
|
|
},
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2019-11-05 00:24:01 +02:00
|
|
|
{
|
|
|
|
NumberKind: core.Float64NumberKind,
|
|
|
|
Random: func(sign int) core.Number {
|
|
|
|
return core.NewFloat64Number(float64(sign) * rnd.Float64() * Magnitude)
|
|
|
|
},
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2019-11-05 00:24:01 +02:00
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func NewAggregatorTest(mkind export.MetricKind, nkind core.NumberKind, alternate bool) *export.Descriptor {
|
2019-10-29 22:27:22 +02:00
|
|
|
desc := export.NewDescriptor("test.name", mkind, nil, "", "", nkind, alternate)
|
2019-11-15 23:01:20 +02:00
|
|
|
return desc
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RunProfiles(t *testing.T, f func(*testing.T, Profile)) {
|
2019-11-05 00:24:01 +02:00
|
|
|
for _, profile := range newProfiles() {
|
2019-10-29 22:27:22 +02:00
|
|
|
t.Run(profile.NumberKind.String(), func(t *testing.T) {
|
|
|
|
f(t, profile)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
type Numbers struct {
|
|
|
|
kind core.NumberKind
|
|
|
|
numbers []core.Number
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNumbers(kind core.NumberKind) Numbers {
|
|
|
|
return Numbers{
|
|
|
|
kind: kind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Numbers) Append(v core.Number) {
|
|
|
|
n.numbers = append(n.numbers, v)
|
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
func (n *Numbers) Sort() {
|
2019-10-31 07:15:27 +02:00
|
|
|
sort.Sort(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Numbers) Less(i, j int) bool {
|
2019-11-05 00:24:01 +02:00
|
|
|
return n.numbers[i].CompareNumber(n.kind, n.numbers[j]) < 0
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Numbers) Len() int {
|
2019-11-05 00:24:01 +02:00
|
|
|
return len(n.numbers)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Numbers) Swap(i, j int) {
|
2019-11-05 00:24:01 +02:00
|
|
|
n.numbers[i], n.numbers[j] = n.numbers[j], n.numbers[i]
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
func (n *Numbers) Sum() core.Number {
|
2019-10-29 22:27:22 +02:00
|
|
|
var sum core.Number
|
2019-11-05 00:24:01 +02:00
|
|
|
for _, num := range n.numbers {
|
|
|
|
sum.AddNumber(n.kind, num)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Numbers) Count() int64 {
|
2019-11-05 00:24:01 +02:00
|
|
|
return int64(len(n.numbers))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
func (n *Numbers) Min() core.Number {
|
|
|
|
return n.numbers[0]
|
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
func (n *Numbers) Max() core.Number {
|
|
|
|
return n.numbers[len(n.numbers)-1]
|
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
// Median() is an alias for Quantile(0.5).
|
|
|
|
func (n *Numbers) Median() core.Number {
|
|
|
|
// Note that len(n.numbers) is 1 greater than the max element
|
|
|
|
// index, so dividing by two rounds up. This gives the
|
|
|
|
// intended definition for Quantile() in tests, which is to
|
|
|
|
// return the smallest element that is at or above the
|
|
|
|
// specified quantile.
|
|
|
|
return n.numbers[len(n.numbers)/2]
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
// Performs the same range test the SDK does on behalf of the aggregator.
|
|
|
|
func CheckedUpdate(t *testing.T, agg export.Aggregator, number core.Number, descriptor *export.Descriptor) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Note: Aggregator tests are written assuming that the SDK
|
|
|
|
// has performed the RangeTest. Therefore we skip errors that
|
|
|
|
// would have been detected by the RangeTest.
|
|
|
|
err := aggregator.RangeTest(number, descriptor)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := agg.Update(ctx, number, descriptor); err != nil {
|
|
|
|
t.Error("Unexpected Update failure", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckedMerge(t *testing.T, aggInto, aggFrom export.Aggregator, descriptor *export.Descriptor) {
|
|
|
|
if err := aggInto.Merge(aggFrom, descriptor); err != nil {
|
|
|
|
t.Error("Unexpected Merge failure", err)
|
|
|
|
}
|
|
|
|
}
|