1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-14 10:13:10 +02:00
opentelemetry-go/sdk/metric/aggregator/aggregatortest/test.go

282 lines
6.9 KiB
Go
Raw Normal View History

// Copyright The 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 aggregatortest // import "go.opentelemetry.io/otel/sdk/metric/aggregator/aggregatortest"
import (
"context"
"errors"
"math/rand"
"os"
"sort"
"testing"
"unsafe"
"github.com/stretchr/testify/require"
ottest "go.opentelemetry.io/otel/internal/internaltest"
Separate InstrumentationLibrary from metric.Descriptor (#2197) * factor instrumentation library out of the instrument descriptor * SDK tests pass * checkpoint work * otlp and opencensus tests passing * prometheus * tests pass, working on lint * lint applied: MetricReader->Reader * comments * Changelog * Apply suggestions from code review Co-authored-by: alrex <alrex.boten@gmail.com> * remove an interdependency * fix build * re-indent one * Apply suggestions from code review Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Lint&feedback * update after rename * comment fix * style fix for meter options * remove libraryReader, let Controller implement the reader API directly * rename 'impl' field to 'provider' * remove a type assertion * move metric/registry into internal; move registry.MeterProvider into metric controller * add test for controller registry function * CheckpointSet->Reader everywhere * lint * remove two unnecessary accessor methods; Controller implements MeterProvider and InstrumentationLibraryReader directly, no need to get these * use a sync.Map * ensure the initOnce is always called; handle multiple errors * Apply suggestions from code review Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * cleanup locking in metrictest * Revert "ensure the initOnce is always called; handle multiple errors" This reverts commit 3356eb5ed0c288ac3edcc2bc2e853aecda7f29b3. * Revert "use a sync.Map" This reverts commit ea7bc599bd3a24c4acb4cd9facd13f08cd702237. * restore the TODO about sync.Map Co-authored-by: alrex <alrex.boten@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
2021-09-27 17:51:47 +02:00
"go.opentelemetry.io/otel/metric/metrictest"
"go.opentelemetry.io/otel/metric/number"
"go.opentelemetry.io/otel/metric/sdkapi"
export "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
2020-06-10 07:53:30 +02:00
"go.opentelemetry.io/otel/sdk/metric/aggregator"
)
const Magnitude = 1000
type Profile struct {
NumberKind number.Kind
Random func(sign int) number.Number
}
type NoopAggregator struct{}
type NoopAggregation struct{}
var _ export.Aggregator = NoopAggregator{}
var _ aggregation.Aggregation = NoopAggregation{}
func newProfiles() []Profile {
rnd := rand.New(rand.NewSource(rand.Int63()))
return []Profile{
{
NumberKind: number.Int64Kind,
Random: func(sign int) number.Number {
return number.NewInt64Number(int64(sign) * int64(rnd.Intn(Magnitude+1)))
},
},
{
NumberKind: number.Float64Kind,
Random: func(sign int) number.Number {
return number.NewFloat64Number(float64(sign) * rnd.Float64() * Magnitude)
},
},
}
}
func NewAggregatorTest(mkind sdkapi.InstrumentKind, nkind number.Kind) *sdkapi.Descriptor {
Separate InstrumentationLibrary from metric.Descriptor (#2197) * factor instrumentation library out of the instrument descriptor * SDK tests pass * checkpoint work * otlp and opencensus tests passing * prometheus * tests pass, working on lint * lint applied: MetricReader->Reader * comments * Changelog * Apply suggestions from code review Co-authored-by: alrex <alrex.boten@gmail.com> * remove an interdependency * fix build * re-indent one * Apply suggestions from code review Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Lint&feedback * update after rename * comment fix * style fix for meter options * remove libraryReader, let Controller implement the reader API directly * rename 'impl' field to 'provider' * remove a type assertion * move metric/registry into internal; move registry.MeterProvider into metric controller * add test for controller registry function * CheckpointSet->Reader everywhere * lint * remove two unnecessary accessor methods; Controller implements MeterProvider and InstrumentationLibraryReader directly, no need to get these * use a sync.Map * ensure the initOnce is always called; handle multiple errors * Apply suggestions from code review Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> * cleanup locking in metrictest * Revert "ensure the initOnce is always called; handle multiple errors" This reverts commit 3356eb5ed0c288ac3edcc2bc2e853aecda7f29b3. * Revert "use a sync.Map" This reverts commit ea7bc599bd3a24c4acb4cd9facd13f08cd702237. * restore the TODO about sync.Map Co-authored-by: alrex <alrex.boten@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
2021-09-27 17:51:47 +02:00
desc := metrictest.NewDescriptor("test.name", mkind, nkind)
return &desc
}
func RunProfiles(t *testing.T, f func(*testing.T, Profile)) {
for _, profile := range newProfiles() {
t.Run(profile.NumberKind.String(), func(t *testing.T) {
f(t, profile)
})
}
}
// TestMain ensures local struct alignment prior to running tests.
func TestMain(m *testing.M) {
fields := []ottest.FieldOffset{
{
Name: "Numbers.numbers",
Offset: unsafe.Offsetof(Numbers{}.numbers),
},
}
if !ottest.Aligned8Byte(fields, os.Stderr) {
os.Exit(1)
}
os.Exit(m.Run())
}
type Numbers struct {
// numbers has to be aligned for 64-bit atomic operations.
numbers []number.Number
kind number.Kind
}
func NewNumbers(kind number.Kind) Numbers {
return Numbers{
kind: kind,
}
}
func (n *Numbers) Append(v number.Number) {
n.numbers = append(n.numbers, v)
}
func (n *Numbers) Sort() {
sort.Sort(n)
}
func (n *Numbers) Less(i, j int) bool {
return n.numbers[i].CompareNumber(n.kind, n.numbers[j]) < 0
}
func (n *Numbers) Len() int {
return len(n.numbers)
}
func (n *Numbers) Swap(i, j int) {
n.numbers[i], n.numbers[j] = n.numbers[j], n.numbers[i]
}
func (n *Numbers) Sum() number.Number {
var sum number.Number
for _, num := range n.numbers {
sum.AddNumber(n.kind, num)
}
return sum
}
func (n *Numbers) Count() uint64 {
return uint64(len(n.numbers))
}
func (n *Numbers) Min() number.Number {
return n.numbers[0]
}
func (n *Numbers) Max() number.Number {
return n.numbers[len(n.numbers)-1]
}
func (n *Numbers) Points() []number.Number {
return n.numbers
}
// CheckedUpdate performs the same range test the SDK does on behalf of the aggregator.
func CheckedUpdate(t *testing.T, agg export.Aggregator, number number.Number, descriptor *sdkapi.Descriptor) {
Metrics stdout export pipeline (#265) * Add MetricAggregator.Merge() implementations * Update from feedback * Type * Ckpt * Ckpt * Add push controller * Ckpt * Add aggregator interfaces, stdout encoder * Modify basic main.go * Main is working * Batch stdout output * Sum udpate * Rename stdout * Add stateless/stateful Batcher options * Undo a for-loop in the example, remove a done TODO * Update imports * Add note * Rename defaultkeys * Support variable label encoder to speed OpenMetrics/Statsd export * Lint * Doc * Precommit/lint * Simplify Aggregator API * Record->Identifier * Remove export.Record a.k.a. Identifier * Checkpoint * Propagate errors to the SDK, remove a bunch of 'TODO warn' * Checkpoint * Introduce export.Labels * Comments in export/metric.go * Comment * More merge * More doc * Complete example * Lint fixes * Add a testable example * Lint * Let Export return an error * add a basic stdout exporter test * Add measure test; fix aggregator APIs * Use JSON numbers, not strings * Test stdout exporter error * Add a test for the call to RangeTest * Add error handler API to improve correctness test; return errors from RecordOne * Undo the previous -- do not expose errors * Add simple selector variations, test * Repair examples * Test push controller error handling * Add SDK label encoder tests * Add a defaultkeys batcher test * Add an ungrouped batcher test * Lint new tests * Respond to krnowak's feedback * Undo comment * Use concrete receivers for export records and labels, since the constructors return structs not pointers * Bug fix for stateful batchers; clone an aggregator for long term storage * Remove TODO addressed in #318 * Add errors to all aggregator interfaces * Handle ErrNoLastValue case in stdout exporter * Move aggregator API into sdk/export/metric/aggregator * Update all aggregator exported-method comments * Document the aggregator APIs * More aggregator comments * Add multiple updates to the ungrouped test * Fixes for feedback from Gustavo and Liz * Producer->CheckpointSet; add FinishedCollection * Process takes an export.Record * ReadCheckpoint->CheckpointSet * EncodeLabels->Encode * Format a better inconsistent type error; add more aggregator API tests * More RangeTest test coverage * Make benbjohnson/clock a test-only dependency * Handle ErrNoLastValue in stress_test
2019-11-15 23:01:20 +02:00
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 *sdkapi.Descriptor) {
Metrics stdout export pipeline (#265) * Add MetricAggregator.Merge() implementations * Update from feedback * Type * Ckpt * Ckpt * Add push controller * Ckpt * Add aggregator interfaces, stdout encoder * Modify basic main.go * Main is working * Batch stdout output * Sum udpate * Rename stdout * Add stateless/stateful Batcher options * Undo a for-loop in the example, remove a done TODO * Update imports * Add note * Rename defaultkeys * Support variable label encoder to speed OpenMetrics/Statsd export * Lint * Doc * Precommit/lint * Simplify Aggregator API * Record->Identifier * Remove export.Record a.k.a. Identifier * Checkpoint * Propagate errors to the SDK, remove a bunch of 'TODO warn' * Checkpoint * Introduce export.Labels * Comments in export/metric.go * Comment * More merge * More doc * Complete example * Lint fixes * Add a testable example * Lint * Let Export return an error * add a basic stdout exporter test * Add measure test; fix aggregator APIs * Use JSON numbers, not strings * Test stdout exporter error * Add a test for the call to RangeTest * Add error handler API to improve correctness test; return errors from RecordOne * Undo the previous -- do not expose errors * Add simple selector variations, test * Repair examples * Test push controller error handling * Add SDK label encoder tests * Add a defaultkeys batcher test * Add an ungrouped batcher test * Lint new tests * Respond to krnowak's feedback * Undo comment * Use concrete receivers for export records and labels, since the constructors return structs not pointers * Bug fix for stateful batchers; clone an aggregator for long term storage * Remove TODO addressed in #318 * Add errors to all aggregator interfaces * Handle ErrNoLastValue case in stdout exporter * Move aggregator API into sdk/export/metric/aggregator * Update all aggregator exported-method comments * Document the aggregator APIs * More aggregator comments * Add multiple updates to the ungrouped test * Fixes for feedback from Gustavo and Liz * Producer->CheckpointSet; add FinishedCollection * Process takes an export.Record * ReadCheckpoint->CheckpointSet * EncodeLabels->Encode * Format a better inconsistent type error; add more aggregator API tests * More RangeTest test coverage * Make benbjohnson/clock a test-only dependency * Handle ErrNoLastValue in stress_test
2019-11-15 23:01:20 +02:00
if err := aggInto.Merge(aggFrom, descriptor); err != nil {
t.Error("Unexpected Merge failure", err)
}
}
func (NoopAggregation) Kind() aggregation.Kind {
return aggregation.Kind("Noop")
}
func (NoopAggregator) Aggregation() aggregation.Aggregation {
return NoopAggregation{}
}
func (NoopAggregator) Update(context.Context, number.Number, *sdkapi.Descriptor) error {
return nil
}
func (NoopAggregator) SynchronizedMove(export.Aggregator, *sdkapi.Descriptor) error {
return nil
}
func (NoopAggregator) Merge(export.Aggregator, *sdkapi.Descriptor) error {
return nil
}
func SynchronizedMoveResetTest(t *testing.T, mkind sdkapi.InstrumentKind, nf func(*sdkapi.Descriptor) export.Aggregator) {
t.Run("reset on nil", func(t *testing.T) {
// Ensures that SynchronizedMove(nil, descriptor) discards and
// resets the aggregator.
RunProfiles(t, func(t *testing.T, profile Profile) {
descriptor := NewAggregatorTest(
mkind,
profile.NumberKind,
)
agg := nf(descriptor)
for i := 0; i < 10; i++ {
x1 := profile.Random(+1)
CheckedUpdate(t, agg, x1, descriptor)
}
require.NoError(t, agg.SynchronizedMove(nil, descriptor))
if count, ok := agg.(aggregation.Count); ok {
c, err := count.Count()
require.Equal(t, uint64(0), c)
require.NoError(t, err)
}
if sum, ok := agg.(aggregation.Sum); ok {
s, err := sum.Sum()
require.Equal(t, number.Number(0), s)
require.NoError(t, err)
}
if lv, ok := agg.(aggregation.LastValue); ok {
v, _, err := lv.LastValue()
require.Equal(t, number.Number(0), v)
require.Error(t, err)
require.True(t, errors.Is(err, aggregation.ErrNoData))
}
})
})
t.Run("no reset on incorrect type", func(t *testing.T) {
// Ensures that SynchronizedMove(wrong_type, descriptor) does not
// reset the aggregator.
RunProfiles(t, func(t *testing.T, profile Profile) {
descriptor := NewAggregatorTest(
mkind,
profile.NumberKind,
)
agg := nf(descriptor)
var input number.Number
const inval = 100
if profile.NumberKind == number.Int64Kind {
input = number.NewInt64Number(inval)
} else {
input = number.NewFloat64Number(inval)
}
CheckedUpdate(t, agg, input, descriptor)
err := agg.SynchronizedMove(NoopAggregator{}, descriptor)
require.Error(t, err)
require.True(t, errors.Is(err, aggregation.ErrInconsistentType))
// Test that the aggregator was not reset
if count, ok := agg.(aggregation.Count); ok {
c, err := count.Count()
require.Equal(t, uint64(1), c)
require.NoError(t, err)
}
if sum, ok := agg.(aggregation.Sum); ok {
s, err := sum.Sum()
require.Equal(t, input, s)
require.NoError(t, err)
}
if lv, ok := agg.(aggregation.LastValue); ok {
v, _, err := lv.LastValue()
require.Equal(t, input, v)
require.NoError(t, err)
}
})
})
}