2019-11-15 13:01:20 -08: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 ungrouped_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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/metric/batcher/test"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/batcher/ungrouped"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These tests use the ../test label encoding.
|
|
|
|
|
|
|
|
func TestUngroupedStateless(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
b := ungrouped.New(test.NewAggregationSelector(), false)
|
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
// Set initial lastValue values
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueADesc, test.Labels1, 10))
|
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueADesc, test.Labels2, 20))
|
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueADesc, test.Labels3, 30))
|
2019-11-19 13:25:24 -08:00
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueBDesc, test.Labels1, 10))
|
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueBDesc, test.Labels2, 20))
|
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueBDesc, test.Labels3, 30))
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
// Another lastValue Set for Labels1
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueADesc, test.Labels1, 50))
|
|
|
|
_ = b.Process(ctx, test.NewLastValueRecord(&test.LastValueBDesc, test.Labels1, 50))
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
// Set initial counter values
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterADesc, test.Labels1, 10))
|
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterADesc, test.Labels2, 20))
|
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterADesc, test.Labels3, 40))
|
2019-11-19 13:25:24 -08:00
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterBDesc, test.Labels1, 10))
|
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterBDesc, test.Labels2, 20))
|
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterBDesc, test.Labels3, 40))
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
// Another counter Add for Labels1
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterADesc, test.Labels1, 50))
|
|
|
|
_ = b.Process(ctx, test.NewCounterRecord(&test.CounterBDesc, test.Labels1, 50))
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
checkpointSet := b.CheckpointSet()
|
|
|
|
b.FinishedCollection()
|
|
|
|
|
|
|
|
records := test.Output{}
|
2020-03-16 16:28:33 -07:00
|
|
|
_ = checkpointSet.ForEach(records.AddTo)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
// Output lastvalue should have only the "G=H" and "G=" keys.
|
2019-11-15 13:01:20 -08:00
|
|
|
// Output counter should have only the "C=D" and "C=" keys.
|
2020-03-19 12:02:46 -07:00
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-03-11 20:21:34 -07:00
|
|
|
"sum.a/G~H&C~D": 60, // labels1
|
|
|
|
"sum.a/C~D&E~F": 20, // labels2
|
|
|
|
"sum.a/": 40, // labels3
|
|
|
|
"sum.b/G~H&C~D": 60, // labels1
|
|
|
|
"sum.b/C~D&E~F": 20, // labels2
|
|
|
|
"sum.b/": 40, // labels3
|
2020-03-10 16:00:37 -07:00
|
|
|
"lastvalue.a/G~H&C~D": 50, // labels1
|
|
|
|
"lastvalue.a/C~D&E~F": 20, // labels2
|
|
|
|
"lastvalue.a/": 30, // labels3
|
|
|
|
"lastvalue.b/G~H&C~D": 50, // labels1
|
|
|
|
"lastvalue.b/C~D&E~F": 20, // labels2
|
|
|
|
"lastvalue.b/": 30, // labels3
|
2019-11-15 13:01:20 -08:00
|
|
|
}, records)
|
|
|
|
|
|
|
|
// Verify that state was reset
|
|
|
|
checkpointSet = b.CheckpointSet()
|
|
|
|
b.FinishedCollection()
|
2020-03-16 16:28:33 -07:00
|
|
|
_ = checkpointSet.ForEach(func(rec export.Record) error {
|
2019-11-15 13:01:20 -08:00
|
|
|
t.Fatal("Unexpected call")
|
2020-03-16 16:28:33 -07:00
|
|
|
return nil
|
2019-11-15 13:01:20 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUngroupedStateful(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
b := ungrouped.New(test.NewAggregationSelector(), true)
|
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
counterA := test.NewCounterRecord(&test.CounterADesc, test.Labels1, 10)
|
2019-11-19 13:25:24 -08:00
|
|
|
caggA := counterA.Aggregator()
|
|
|
|
_ = b.Process(ctx, counterA)
|
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
counterB := test.NewCounterRecord(&test.CounterBDesc, test.Labels1, 10)
|
2019-11-19 13:25:24 -08:00
|
|
|
caggB := counterB.Aggregator()
|
|
|
|
_ = b.Process(ctx, counterB)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
checkpointSet := b.CheckpointSet()
|
|
|
|
b.FinishedCollection()
|
|
|
|
|
|
|
|
records1 := test.Output{}
|
2020-03-16 16:28:33 -07:00
|
|
|
_ = checkpointSet.ForEach(records1.AddTo)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-03-11 20:21:34 -07:00
|
|
|
"sum.a/G~H&C~D": 10, // labels1
|
|
|
|
"sum.b/G~H&C~D": 10, // labels1
|
2019-11-15 13:01:20 -08:00
|
|
|
}, records1)
|
|
|
|
|
|
|
|
// Test that state was NOT reset
|
|
|
|
checkpointSet = b.CheckpointSet()
|
|
|
|
b.FinishedCollection()
|
|
|
|
|
|
|
|
records2 := test.Output{}
|
2020-03-16 16:28:33 -07:00
|
|
|
_ = checkpointSet.ForEach(records2.AddTo)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
require.EqualValues(t, records1, records2)
|
|
|
|
|
|
|
|
// Update and re-checkpoint the original record.
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = caggA.Update(ctx, core.NewInt64Number(20), &test.CounterADesc)
|
|
|
|
_ = caggB.Update(ctx, core.NewInt64Number(20), &test.CounterBDesc)
|
|
|
|
caggA.Checkpoint(ctx, &test.CounterADesc)
|
|
|
|
caggB.Checkpoint(ctx, &test.CounterBDesc)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
// As yet cagg has not been passed to Batcher.Process. Should
|
|
|
|
// not see an update.
|
|
|
|
checkpointSet = b.CheckpointSet()
|
|
|
|
b.FinishedCollection()
|
|
|
|
|
|
|
|
records3 := test.Output{}
|
2020-03-16 16:28:33 -07:00
|
|
|
_ = checkpointSet.ForEach(records3.AddTo)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
require.EqualValues(t, records1, records3)
|
|
|
|
|
|
|
|
// Now process the second update
|
2020-03-19 12:02:46 -07:00
|
|
|
_ = b.Process(ctx, export.NewRecord(&test.CounterADesc, test.Labels1, caggA))
|
|
|
|
_ = b.Process(ctx, export.NewRecord(&test.CounterBDesc, test.Labels1, caggB))
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
checkpointSet = b.CheckpointSet()
|
|
|
|
b.FinishedCollection()
|
|
|
|
|
|
|
|
records4 := test.Output{}
|
2020-03-16 16:28:33 -07:00
|
|
|
_ = checkpointSet.ForEach(records4.AddTo)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-03-11 20:21:34 -07:00
|
|
|
"sum.a/G~H&C~D": 30,
|
|
|
|
"sum.b/G~H&C~D": 30,
|
2019-11-15 13:01:20 -08:00
|
|
|
}, records4)
|
|
|
|
}
|