2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-29 22:27:22 +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-03-11 01:00:37 +02:00
|
|
|
package lastvalue
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
import (
|
2020-06-13 09:55:01 +02:00
|
|
|
"errors"
|
2019-10-29 22:27:22 +02:00
|
|
|
"math/rand"
|
2020-01-06 20:08:40 +02:00
|
|
|
"os"
|
2019-10-29 22:27:22 +02:00
|
|
|
"testing"
|
2020-06-13 09:55:01 +02:00
|
|
|
"time"
|
2020-01-06 20:08:40 +02:00
|
|
|
"unsafe"
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-01-06 20:08:40 +02:00
|
|
|
ottest "go.opentelemetry.io/otel/internal/testing"
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-06-10 07:53:30 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
2020-07-28 23:31:56 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/aggregatortest"
|
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
|
|
|
|
2020-01-06 20:08:40 +02:00
|
|
|
// Ensure struct alignment prior to running tests.
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
fields := []ottest.FieldOffset{
|
|
|
|
{
|
2020-03-11 01:00:37 +02:00
|
|
|
Name: "lastValueData.value",
|
|
|
|
Offset: unsafe.Offsetof(lastValueData{}.value),
|
2020-01-06 20:08:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if !ottest.Aligned8Byte(fields, os.Stderr) {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
func new2() (_, _ *Aggregator) {
|
|
|
|
alloc := New(2)
|
|
|
|
return &alloc[0], &alloc[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func new4() (_, _, _, _ *Aggregator) {
|
|
|
|
alloc := New(4)
|
|
|
|
return &alloc[0], &alloc[1], &alloc[2], &alloc[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkZero(t *testing.T, agg *Aggregator) {
|
|
|
|
lv, ts, err := agg.LastValue()
|
|
|
|
require.True(t, errors.Is(err, aggregation.ErrNoData))
|
|
|
|
require.Equal(t, time.Time{}, ts)
|
2020-11-11 17:24:12 +02:00
|
|
|
require.Equal(t, number.Number(0), lv)
|
2020-06-13 09:55:01 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestLastValueUpdate(t *testing.T) {
|
2020-07-28 23:31:56 +02:00
|
|
|
aggregatortest.RunProfiles(t, func(t *testing.T, profile aggregatortest.Profile) {
|
2020-06-13 09:55:01 +02:00
|
|
|
agg, ckpt := new2()
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
record := aggregatortest.NewAggregatorTest(metric.ValueObserverInstrumentKind, profile.NumberKind)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
var last number.Number
|
2019-10-29 22:27:22 +02:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(rand.Intn(1)*2 - 1)
|
|
|
|
last = x
|
2020-07-28 23:31:56 +02:00
|
|
|
aggregatortest.CheckedUpdate(t, agg, x, record)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:41:11 +02:00
|
|
|
err := agg.SynchronizedMove(ckpt, record)
|
2020-06-13 09:55:01 +02:00
|
|
|
require.NoError(t, err)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
lv, _, err := ckpt.LastValue()
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(t, last, lv, "Same last value - non-monotonic")
|
|
|
|
require.Nil(t, err)
|
2019-10-29 22:27:22 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestLastValueMerge(t *testing.T) {
|
2020-07-28 23:31:56 +02:00
|
|
|
aggregatortest.RunProfiles(t, func(t *testing.T, profile aggregatortest.Profile) {
|
2020-06-13 09:55:01 +02:00
|
|
|
agg1, agg2, ckpt1, ckpt2 := new4()
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
descriptor := aggregatortest.NewAggregatorTest(metric.ValueObserverInstrumentKind, profile.NumberKind)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
|
|
|
first1 := profile.Random(+1)
|
|
|
|
first2 := profile.Random(+1)
|
|
|
|
first1.AddNumber(profile.NumberKind, first2)
|
|
|
|
|
2020-07-28 23:31:56 +02:00
|
|
|
aggregatortest.CheckedUpdate(t, agg1, first1, descriptor)
|
|
|
|
aggregatortest.CheckedUpdate(t, agg2, first2, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, agg1.SynchronizedMove(ckpt1, descriptor))
|
|
|
|
require.NoError(t, agg2.SynchronizedMove(ckpt2, descriptor))
|
2020-06-13 09:55:01 +02:00
|
|
|
|
|
|
|
checkZero(t, agg1)
|
|
|
|
checkZero(t, agg2)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
_, t1, err := ckpt1.LastValue()
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Nil(t, err)
|
2020-06-13 09:55:01 +02:00
|
|
|
_, t2, err := ckpt2.LastValue()
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Nil(t, err)
|
2019-10-31 07:15:27 +02:00
|
|
|
require.True(t, t1.Before(t2))
|
|
|
|
|
2020-07-28 23:31:56 +02:00
|
|
|
aggregatortest.CheckedMerge(t, ckpt1, ckpt2, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
lv, ts, err := ckpt1.LastValue()
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, t2, ts, "Merged timestamp - non-monotonic")
|
|
|
|
require.Equal(t, first2, lv, "Merged value - non-monotonic")
|
2019-10-31 07:15:27 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestLastValueNotSet(t *testing.T) {
|
2020-11-12 17:28:32 +02:00
|
|
|
descriptor := aggregatortest.NewAggregatorTest(metric.ValueObserverInstrumentKind, number.Int64Kind)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
g, ckpt := new2()
|
2020-06-23 19:41:11 +02:00
|
|
|
require.NoError(t, g.SynchronizedMove(ckpt, descriptor))
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
checkZero(t, g)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
2020-12-11 04:13:08 +02:00
|
|
|
|
|
|
|
func TestSynchronizedMoveReset(t *testing.T) {
|
|
|
|
aggregatortest.SynchronizedMoveResetTest(
|
|
|
|
t,
|
|
|
|
metric.ValueObserverInstrumentKind,
|
|
|
|
func(desc *metric.Descriptor) export.Aggregator {
|
|
|
|
return &New(1)[0]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|