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 gauge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
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-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/test"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const count = 100
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
var _ export.Aggregator = &Aggregator{}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
func TestGaugeNonMonotonic(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg := New()
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
batcher, record := test.NewAggregatorTest(export.GaugeKind, profile.NumberKind, false)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
var last core.Number
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(rand.Intn(1)*2 - 1)
|
|
|
|
last = x
|
|
|
|
agg.Update(ctx, x, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
agg.Collect(ctx, record, batcher)
|
|
|
|
|
|
|
|
require.Equal(t, last, agg.AsNumber(), "Same last value - non-monotonic")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGaugeMonotonic(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg := New()
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
batcher, record := test.NewAggregatorTest(export.GaugeKind, profile.NumberKind, true)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
small := profile.Random(+1)
|
|
|
|
last := small
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
last.AddNumber(profile.NumberKind, x)
|
|
|
|
agg.Update(ctx, last, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
agg.Collect(ctx, record, batcher)
|
|
|
|
|
|
|
|
require.Equal(t, last, agg.AsNumber(), "Same last value - monotonic")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGaugeMonotonicDescending(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg := New()
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
batcher, record := test.NewAggregatorTest(export.GaugeKind, profile.NumberKind, true)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
first := profile.Random(+1)
|
|
|
|
agg.Update(ctx, first, record)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(-1)
|
|
|
|
agg.Update(ctx, x, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
agg.Collect(ctx, record, batcher)
|
|
|
|
|
|
|
|
require.Equal(t, first, agg.AsNumber(), "Same last value - monotonic")
|
|
|
|
})
|
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
|
|
|
func TestGaugeNormalMerge(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg1 := New()
|
|
|
|
agg2 := New()
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
batcher, record := test.NewAggregatorTest(export.GaugeKind, profile.NumberKind, false)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
|
|
|
first1 := profile.Random(+1)
|
|
|
|
first2 := profile.Random(+1)
|
|
|
|
first1.AddNumber(profile.NumberKind, first2)
|
|
|
|
|
|
|
|
agg1.Update(ctx, first1, record)
|
|
|
|
agg2.Update(ctx, first2, record)
|
|
|
|
|
|
|
|
agg1.Collect(ctx, record, batcher)
|
|
|
|
agg2.Collect(ctx, record, batcher)
|
|
|
|
|
|
|
|
t1 := agg1.Timestamp()
|
|
|
|
t2 := agg2.Timestamp()
|
|
|
|
require.True(t, t1.Before(t2))
|
|
|
|
|
|
|
|
agg1.Merge(agg2, record.Descriptor())
|
|
|
|
|
|
|
|
require.Equal(t, t2, agg1.Timestamp(), "Merged timestamp - non-monotonic")
|
|
|
|
require.Equal(t, first2, agg1.AsNumber(), "Merged value - non-monotonic")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGaugeMonotonicMerge(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg1 := New()
|
|
|
|
agg2 := New()
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
batcher, record := test.NewAggregatorTest(export.GaugeKind, profile.NumberKind, true)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
|
|
|
first1 := profile.Random(+1)
|
|
|
|
agg1.Update(ctx, first1, record)
|
|
|
|
|
|
|
|
first2 := profile.Random(+1)
|
|
|
|
first2.AddNumber(profile.NumberKind, first1)
|
|
|
|
agg2.Update(ctx, first2, record)
|
|
|
|
|
|
|
|
agg1.Collect(ctx, record, batcher)
|
|
|
|
agg2.Collect(ctx, record, batcher)
|
|
|
|
|
|
|
|
agg1.Merge(agg2, record.Descriptor())
|
|
|
|
|
|
|
|
require.Equal(t, first2, agg1.AsNumber(), "Merged value - monotonic")
|
|
|
|
require.Equal(t, agg2.Timestamp(), agg1.Timestamp(), "Merged timestamp - monotonic")
|
|
|
|
})
|
|
|
|
}
|