2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-15 23:01:20 +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.
|
|
|
|
|
|
|
|
package metric_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2020-06-02 22:03:02 +02:00
|
|
|
"sync"
|
2019-11-15 23:01:20 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2021-02-18 19:59:37 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2020-11-12 17:28:32 +02:00
|
|
|
"go.opentelemetry.io/otel/metric"
|
2019-11-15 23:01:20 +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-03-24 18:30:12 +02:00
|
|
|
metricsdk "go.opentelemetry.io/otel/sdk/metric"
|
2020-08-10 22:35:15 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
var Must = metric.Must
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2020-06-02 22:03:02 +02:00
|
|
|
type handler struct {
|
|
|
|
sync.Mutex
|
|
|
|
err error
|
|
|
|
}
|
2020-06-02 20:30:09 +02:00
|
|
|
|
|
|
|
func (h *handler) Handle(err error) {
|
2020-06-02 22:03:02 +02:00
|
|
|
h.Lock()
|
2020-06-02 20:30:09 +02:00
|
|
|
h.err = err
|
2020-06-02 22:03:02 +02:00
|
|
|
h.Unlock()
|
2020-06-02 20:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Reset() {
|
2020-06-02 22:03:02 +02:00
|
|
|
h.Lock()
|
2020-06-02 20:30:09 +02:00
|
|
|
h.err = nil
|
2020-06-02 22:03:02 +02:00
|
|
|
h.Unlock()
|
2020-06-02 20:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Flush() error {
|
2020-06-02 22:03:02 +02:00
|
|
|
h.Lock()
|
2020-06-02 20:30:09 +02:00
|
|
|
err := h.err
|
2020-06-02 22:03:02 +02:00
|
|
|
h.err = nil
|
|
|
|
h.Unlock()
|
2020-06-02 20:30:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var testHandler *handler
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
testHandler = new(handler)
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.SetErrorHandler(testHandler)
|
2020-06-02 20:30:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
type testSelector struct {
|
2020-06-23 19:51:15 +02:00
|
|
|
selector export.AggregatorSelector
|
2020-06-13 09:55:01 +02:00
|
|
|
newAggCount int
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
func (ts *testSelector) AggregatorFor(desc *metric.Descriptor, aggPtrs ...*export.Aggregator) {
|
2020-06-13 09:55:01 +02:00
|
|
|
ts.newAggCount += len(aggPtrs)
|
2020-08-10 22:35:15 +02:00
|
|
|
processortest.AggregatorSelector().AggregatorFor(desc, aggPtrs...)
|
2020-06-13 09:55:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
func newSDK(t *testing.T) (metric.Meter, *metricsdk.Accumulator, *testSelector, *processortest.Processor) {
|
2020-06-02 20:30:09 +02:00
|
|
|
testHandler.Reset()
|
2021-07-21 19:06:38 +02:00
|
|
|
testSelector := &testSelector{selector: processortest.AggregatorSelector()}
|
|
|
|
processor := processortest.NewProcessor(
|
|
|
|
testSelector,
|
|
|
|
attribute.DefaultEncoder(),
|
|
|
|
)
|
2020-05-19 20:49:24 +02:00
|
|
|
accum := metricsdk.NewAccumulator(
|
2020-06-23 21:00:15 +02:00
|
|
|
processor,
|
2020-05-19 20:49:24 +02:00
|
|
|
)
|
2020-11-12 17:28:32 +02:00
|
|
|
meter := metric.WrapMeterImpl(accum, "test")
|
2021-07-21 19:06:38 +02:00
|
|
|
return meter, accum, testSelector, processor
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:00:22 +02:00
|
|
|
func TestInputRangeCounter(t *testing.T) {
|
2019-11-15 23:01:20 +02:00
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
counter := Must(meter).NewInt64Counter("name.sum")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, -1)
|
2020-06-10 07:53:30 +02:00
|
|
|
require.Equal(t, aggregation.ErrNegativeInput, testHandler.Flush())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 18:11:27 +02:00
|
|
|
checkpointed := sdk.Collect(ctx)
|
2020-04-22 05:23:15 +02:00
|
|
|
require.Equal(t, 0, checkpointed)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
processor.Reset()
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, 1)
|
2020-03-11 18:11:27 +02:00
|
|
|
checkpointed = sdk.Collect(ctx)
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, map[string]float64{
|
2021-08-13 00:44:58 +02:00
|
|
|
"name.sum//": 1,
|
2021-07-21 19:06:38 +02:00
|
|
|
}, processor.Values())
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(t, 1, checkpointed)
|
2020-06-02 20:30:09 +02:00
|
|
|
require.Nil(t, testHandler.Flush())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:00:22 +02:00
|
|
|
func TestInputRangeUpDownCounter(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-05-19 19:00:22 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
counter := Must(meter).NewInt64UpDownCounter("name.sum")
|
2020-05-19 19:00:22 +02:00
|
|
|
|
|
|
|
counter.Add(ctx, -1)
|
|
|
|
counter.Add(ctx, -1)
|
|
|
|
counter.Add(ctx, 2)
|
|
|
|
counter.Add(ctx, 1)
|
|
|
|
|
|
|
|
checkpointed := sdk.Collect(ctx)
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, map[string]float64{
|
2021-08-13 00:44:58 +02:00
|
|
|
"name.sum//": 1,
|
2021-07-21 19:06:38 +02:00
|
|
|
}, processor.Values())
|
2020-05-19 19:00:22 +02:00
|
|
|
require.Equal(t, 1, checkpointed)
|
2020-06-02 20:30:09 +02:00
|
|
|
require.Nil(t, testHandler.Flush())
|
2020-05-19 19:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputRangeValueRecorder(t *testing.T) {
|
2019-11-15 23:01:20 +02:00
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
valuerecorder := Must(meter).NewFloat64ValueRecorder("name.exact")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, math.NaN())
|
2020-06-10 07:53:30 +02:00
|
|
|
require.Equal(t, aggregation.ErrNaNInput, testHandler.Flush())
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-11 18:11:27 +02:00
|
|
|
checkpointed := sdk.Collect(ctx)
|
2020-04-22 05:23:15 +02:00
|
|
|
require.Equal(t, 0, checkpointed)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, 1)
|
|
|
|
valuerecorder.Record(ctx, 2)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
processor.Reset()
|
2020-03-11 18:11:27 +02:00
|
|
|
checkpointed = sdk.Collect(ctx)
|
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, map[string]float64{
|
2021-08-13 00:44:58 +02:00
|
|
|
"name.exact//": 3,
|
2021-07-21 19:06:38 +02:00
|
|
|
}, processor.Values())
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(t, 1, checkpointed)
|
2020-06-02 20:30:09 +02:00
|
|
|
require.Nil(t, testHandler.Flush())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisabledInstrument(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-03-24 19:54:08 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder := Must(meter).NewFloat64ValueRecorder("name.disabled")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, -1)
|
2019-11-15 23:01:20 +02:00
|
|
|
checkpointed := sdk.Collect(ctx)
|
|
|
|
|
|
|
|
require.Equal(t, 0, checkpointed)
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, map[string]float64{}, processor.Values())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordNaN(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, _, _, _ := newSDK(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
c := Must(meter).NewFloat64Counter("name.sum")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-06-02 20:30:09 +02:00
|
|
|
require.Nil(t, testHandler.Flush())
|
2020-03-27 23:06:48 +02:00
|
|
|
c.Add(ctx, math.NaN())
|
2020-06-02 20:30:09 +02:00
|
|
|
require.Error(t, testHandler.Flush())
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 18:11:27 +02:00
|
|
|
func TestSDKLabelsDeduplication(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-03-11 18:11:27 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
counter := Must(meter).NewInt64Counter("name.sum")
|
2020-03-11 18:11:27 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
maxKeys = 21
|
|
|
|
keySets = 2
|
|
|
|
repeats = 3
|
|
|
|
)
|
2021-02-18 19:59:37 +02:00
|
|
|
var keysA []attribute.Key
|
|
|
|
var keysB []attribute.Key
|
2020-03-11 18:11:27 +02:00
|
|
|
|
|
|
|
for i := 0; i < maxKeys; i++ {
|
2021-02-18 19:59:37 +02:00
|
|
|
keysA = append(keysA, attribute.Key(fmt.Sprintf("A%03d", i)))
|
|
|
|
keysB = append(keysB, attribute.Key(fmt.Sprintf("B%03d", i)))
|
2020-03-11 18:11:27 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
allExpect := map[string]float64{}
|
2020-03-11 18:11:27 +02:00
|
|
|
for numKeys := 0; numKeys < maxKeys; numKeys++ {
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
var kvsA []attribute.KeyValue
|
|
|
|
var kvsB []attribute.KeyValue
|
2020-03-11 18:11:27 +02:00
|
|
|
for r := 0; r < repeats; r++ {
|
|
|
|
for i := 0; i < numKeys; i++ {
|
|
|
|
kvsA = append(kvsA, keysA[i].Int(r))
|
|
|
|
kvsB = append(kvsB, keysB[i].Int(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
var expectA []attribute.KeyValue
|
|
|
|
var expectB []attribute.KeyValue
|
2020-03-11 18:11:27 +02:00
|
|
|
for i := 0; i < numKeys; i++ {
|
|
|
|
expectA = append(expectA, keysA[i].Int(repeats-1))
|
|
|
|
expectB = append(expectB, keysB[i].Int(repeats-1))
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, 1, kvsA...)
|
|
|
|
counter.Add(ctx, 1, kvsA...)
|
2021-07-21 19:06:38 +02:00
|
|
|
format := func(attrs []attribute.KeyValue) string {
|
|
|
|
str := attribute.DefaultEncoder().Encode(newSetIter(attrs...))
|
2021-08-13 00:44:58 +02:00
|
|
|
return fmt.Sprint("name.sum/", str, "/")
|
2021-07-21 19:06:38 +02:00
|
|
|
}
|
|
|
|
allExpect[format(expectA)] += 2
|
2020-03-11 18:11:27 +02:00
|
|
|
|
|
|
|
if numKeys != 0 {
|
|
|
|
// In this case A and B sets are the same.
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, 1, kvsB...)
|
|
|
|
counter.Add(ctx, 1, kvsB...)
|
2021-07-21 19:06:38 +02:00
|
|
|
allExpect[format(expectB)] += 2
|
2020-03-11 18:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sdk.Collect(ctx)
|
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.EqualValues(t, allExpect, processor.Values())
|
2020-03-11 18:11:27 +02:00
|
|
|
}
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
func newSetIter(kvs ...attribute.KeyValue) attribute.Iterator {
|
|
|
|
labels := attribute.NewSet(kvs...)
|
2020-04-23 21:10:58 +02:00
|
|
|
return labels.Iter()
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func TestDefaultLabelEncoder(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
encoder := attribute.DefaultEncoder()
|
2020-03-05 00:19:25 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
encoded := encoder.Encode(newSetIter(attribute.String("A", "B"), attribute.String("C", "D")))
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(t, `A=B,C=D`, encoded)
|
2020-03-05 00:19:25 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
encoded = encoder.Encode(newSetIter(attribute.String("A", "B,c=d"), attribute.String(`C\`, "D")))
|
2020-03-05 00:19:25 +02:00
|
|
|
require.Equal(t, `A=B\,c\=d,C\\=D`, encoded)
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
encoded = encoder.Encode(newSetIter(attribute.String(`\`, `=`), attribute.String(`,`, `\`)))
|
2020-04-23 21:10:58 +02:00
|
|
|
require.Equal(t, `\,=\\,\\=\=`, encoded)
|
2020-03-05 00:19:25 +02:00
|
|
|
|
|
|
|
// Note: the label encoder does not sort or de-dup values,
|
|
|
|
// that is done in Labels(...).
|
2020-04-23 21:10:58 +02:00
|
|
|
encoded = encoder.Encode(newSetIter(
|
2021-02-18 19:59:37 +02:00
|
|
|
attribute.Int("I", 1),
|
|
|
|
attribute.Int64("I64", 1),
|
|
|
|
attribute.Float64("F64", 1),
|
|
|
|
attribute.Float64("F64", 1),
|
|
|
|
attribute.String("S", "1"),
|
|
|
|
attribute.Bool("B", true),
|
2020-04-23 21:10:58 +02:00
|
|
|
))
|
2021-02-17 02:23:58 +02:00
|
|
|
require.Equal(t, "B=true,F64=1,I=1,I64=1,S=1", encoded)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
func TestObserverCollection(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-12-11 04:13:08 +02:00
|
|
|
mult := 1
|
2020-03-19 21:02:46 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewFloat64ValueObserver("float.valueobserver.lastvalue", func(_ context.Context, result metric.Float64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(float64(mult), attribute.String("A", "B"))
|
2020-04-03 01:51:37 +02:00
|
|
|
// last value wins
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(float64(-mult), attribute.String("A", "B"))
|
|
|
|
result.Observe(float64(-mult), attribute.String("C", "D"))
|
2020-03-19 21:02:46 +02:00
|
|
|
})
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewInt64ValueObserver("int.valueobserver.lastvalue", func(_ context.Context, result metric.Int64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(int64(-mult), attribute.String("A", "B"))
|
2020-12-11 04:13:08 +02:00
|
|
|
result.Observe(int64(mult))
|
2020-04-03 01:51:37 +02:00
|
|
|
// last value wins
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(int64(mult), attribute.String("A", "B"))
|
2020-12-11 04:13:08 +02:00
|
|
|
result.Observe(int64(mult))
|
2020-03-19 21:02:46 +02:00
|
|
|
})
|
2020-05-19 20:49:24 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewFloat64SumObserver("float.sumobserver.sum", func(_ context.Context, result metric.Float64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(float64(mult), attribute.String("A", "B"))
|
|
|
|
result.Observe(float64(2*mult), attribute.String("A", "B"))
|
|
|
|
result.Observe(float64(mult), attribute.String("C", "D"))
|
2020-05-19 20:49:24 +02:00
|
|
|
})
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewInt64SumObserver("int.sumobserver.sum", func(_ context.Context, result metric.Int64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(int64(2*mult), attribute.String("A", "B"))
|
2020-12-11 04:13:08 +02:00
|
|
|
result.Observe(int64(mult))
|
2020-05-19 20:49:24 +02:00
|
|
|
// last value wins
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(int64(mult), attribute.String("A", "B"))
|
2020-12-11 04:13:08 +02:00
|
|
|
result.Observe(int64(mult))
|
2020-05-19 20:49:24 +02:00
|
|
|
})
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewFloat64UpDownSumObserver("float.updownsumobserver.sum", func(_ context.Context, result metric.Float64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(float64(mult), attribute.String("A", "B"))
|
|
|
|
result.Observe(float64(-2*mult), attribute.String("A", "B"))
|
|
|
|
result.Observe(float64(mult), attribute.String("C", "D"))
|
2020-05-20 19:19:51 +02:00
|
|
|
})
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewInt64UpDownSumObserver("int.updownsumobserver.sum", func(_ context.Context, result metric.Int64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(int64(2*mult), attribute.String("A", "B"))
|
2020-12-11 04:13:08 +02:00
|
|
|
result.Observe(int64(mult))
|
2020-05-20 19:19:51 +02:00
|
|
|
// last value wins
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(int64(mult), attribute.String("A", "B"))
|
2020-12-11 04:13:08 +02:00
|
|
|
result.Observe(int64(-mult))
|
2020-05-20 19:19:51 +02:00
|
|
|
})
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewInt64ValueObserver("empty.valueobserver.sum", func(_ context.Context, result metric.Int64ObserverResult) {
|
2020-03-19 21:02:46 +02:00
|
|
|
})
|
|
|
|
|
2020-12-11 04:13:08 +02:00
|
|
|
for mult = 0; mult < 3; mult++ {
|
2021-07-21 19:06:38 +02:00
|
|
|
processor.Reset()
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2020-12-11 04:13:08 +02:00
|
|
|
collected := sdk.Collect(ctx)
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, collected, len(processor.Values()))
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2020-12-11 04:13:08 +02:00
|
|
|
mult := float64(mult)
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2021-08-13 00:44:58 +02:00
|
|
|
"float.valueobserver.lastvalue/A=B/": -mult,
|
|
|
|
"float.valueobserver.lastvalue/C=D/": -mult,
|
|
|
|
"int.valueobserver.lastvalue//": mult,
|
|
|
|
"int.valueobserver.lastvalue/A=B/": mult,
|
|
|
|
|
|
|
|
"float.sumobserver.sum/A=B/": 2 * mult,
|
|
|
|
"float.sumobserver.sum/C=D/": mult,
|
|
|
|
"int.sumobserver.sum//": mult,
|
|
|
|
"int.sumobserver.sum/A=B/": mult,
|
|
|
|
|
|
|
|
"float.updownsumobserver.sum/A=B/": -2 * mult,
|
|
|
|
"float.updownsumobserver.sum/C=D/": mult,
|
|
|
|
"int.updownsumobserver.sum//": -mult,
|
|
|
|
"int.updownsumobserver.sum/A=B/": mult,
|
2021-07-21 19:06:38 +02:00
|
|
|
}, processor.Values())
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:49:24 +02:00
|
|
|
func TestSumObserverInputRange(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-05-19 20:49:24 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
// TODO: these tests are testing for negative values, not for _descending values_. Fix.
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewFloat64SumObserver("float.sumobserver.sum", func(_ context.Context, result metric.Float64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(-2, attribute.String("A", "B"))
|
2020-06-10 07:53:30 +02:00
|
|
|
require.Equal(t, aggregation.ErrNegativeInput, testHandler.Flush())
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(-1, attribute.String("C", "D"))
|
2020-06-10 07:53:30 +02:00
|
|
|
require.Equal(t, aggregation.ErrNegativeInput, testHandler.Flush())
|
2020-05-19 20:49:24 +02:00
|
|
|
})
|
2020-11-12 17:28:32 +02:00
|
|
|
_ = Must(meter).NewInt64SumObserver("int.sumobserver.sum", func(_ context.Context, result metric.Int64ObserverResult) {
|
2021-02-18 19:59:37 +02:00
|
|
|
result.Observe(-1, attribute.String("A", "B"))
|
2020-06-10 07:53:30 +02:00
|
|
|
require.Equal(t, aggregation.ErrNegativeInput, testHandler.Flush())
|
2020-05-19 20:49:24 +02:00
|
|
|
result.Observe(-1)
|
2020-06-10 07:53:30 +02:00
|
|
|
require.Equal(t, aggregation.ErrNegativeInput, testHandler.Flush())
|
2020-05-19 20:49:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
collected := sdk.Collect(ctx)
|
|
|
|
|
|
|
|
require.Equal(t, 0, collected)
|
2021-07-21 19:06:38 +02:00
|
|
|
require.EqualValues(t, map[string]float64{}, processor.Values())
|
2020-05-19 20:49:24 +02:00
|
|
|
|
|
|
|
// check that the error condition was reset
|
2020-06-02 20:30:09 +02:00
|
|
|
require.NoError(t, testHandler.Flush())
|
2020-05-19 20:49:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:27:52 +02:00
|
|
|
func TestObserverBatch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
var floatValueObs metric.Float64ValueObserver
|
|
|
|
var intValueObs metric.Int64ValueObserver
|
|
|
|
var floatSumObs metric.Float64SumObserver
|
|
|
|
var intSumObs metric.Int64SumObserver
|
|
|
|
var floatUpDownSumObs metric.Float64UpDownSumObserver
|
|
|
|
var intUpDownSumObs metric.Int64UpDownSumObserver
|
2020-05-19 20:49:24 +02:00
|
|
|
|
2020-05-14 01:27:52 +02:00
|
|
|
var batch = Must(meter).NewBatchObserver(
|
2020-11-12 17:28:32 +02:00
|
|
|
func(_ context.Context, result metric.BatchObserverResult) {
|
2020-05-14 01:27:52 +02:00
|
|
|
result.Observe(
|
2021-02-18 19:59:37 +02:00
|
|
|
[]attribute.KeyValue{
|
|
|
|
attribute.String("A", "B"),
|
2020-05-14 01:27:52 +02:00
|
|
|
},
|
2020-05-19 20:49:24 +02:00
|
|
|
floatValueObs.Observation(1),
|
|
|
|
floatValueObs.Observation(-1),
|
|
|
|
intValueObs.Observation(-1),
|
|
|
|
intValueObs.Observation(1),
|
|
|
|
floatSumObs.Observation(1000),
|
|
|
|
intSumObs.Observation(100),
|
2020-05-20 19:19:51 +02:00
|
|
|
floatUpDownSumObs.Observation(-1000),
|
|
|
|
intUpDownSumObs.Observation(-100),
|
2020-05-14 01:27:52 +02:00
|
|
|
)
|
|
|
|
result.Observe(
|
2021-02-18 19:59:37 +02:00
|
|
|
[]attribute.KeyValue{
|
|
|
|
attribute.String("C", "D"),
|
2020-05-14 01:27:52 +02:00
|
|
|
},
|
2020-05-19 20:49:24 +02:00
|
|
|
floatValueObs.Observation(-1),
|
|
|
|
floatSumObs.Observation(-1),
|
2020-05-20 19:19:51 +02:00
|
|
|
floatUpDownSumObs.Observation(-1),
|
2020-05-14 01:27:52 +02:00
|
|
|
)
|
|
|
|
result.Observe(
|
|
|
|
nil,
|
2020-05-19 20:49:24 +02:00
|
|
|
intValueObs.Observation(1),
|
|
|
|
intValueObs.Observation(1),
|
|
|
|
intSumObs.Observation(10),
|
|
|
|
floatSumObs.Observation(1.1),
|
2020-05-20 19:19:51 +02:00
|
|
|
intUpDownSumObs.Observation(10),
|
2020-05-14 01:27:52 +02:00
|
|
|
)
|
|
|
|
})
|
2020-06-13 09:55:01 +02:00
|
|
|
floatValueObs = batch.NewFloat64ValueObserver("float.valueobserver.lastvalue")
|
|
|
|
intValueObs = batch.NewInt64ValueObserver("int.valueobserver.lastvalue")
|
|
|
|
floatSumObs = batch.NewFloat64SumObserver("float.sumobserver.sum")
|
|
|
|
intSumObs = batch.NewInt64SumObserver("int.sumobserver.sum")
|
|
|
|
floatUpDownSumObs = batch.NewFloat64UpDownSumObserver("float.updownsumobserver.sum")
|
|
|
|
intUpDownSumObs = batch.NewInt64UpDownSumObserver("int.updownsumobserver.sum")
|
2020-05-14 01:27:52 +02:00
|
|
|
|
|
|
|
collected := sdk.Collect(ctx)
|
2020-03-19 21:02:46 +02:00
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, collected, len(processor.Values()))
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2021-08-13 00:44:58 +02:00
|
|
|
"float.sumobserver.sum//": 1.1,
|
|
|
|
"float.sumobserver.sum/A=B/": 1000,
|
|
|
|
"int.sumobserver.sum//": 10,
|
|
|
|
"int.sumobserver.sum/A=B/": 100,
|
|
|
|
|
|
|
|
"int.updownsumobserver.sum/A=B/": -100,
|
|
|
|
"float.updownsumobserver.sum/A=B/": -1000,
|
|
|
|
"int.updownsumobserver.sum//": 10,
|
|
|
|
"float.updownsumobserver.sum/C=D/": -1,
|
|
|
|
|
|
|
|
"float.valueobserver.lastvalue/A=B/": -1,
|
|
|
|
"float.valueobserver.lastvalue/C=D/": -1,
|
|
|
|
"int.valueobserver.lastvalue//": 1,
|
|
|
|
"int.valueobserver.lastvalue/A=B/": 1,
|
2021-07-21 19:06:38 +02:00
|
|
|
}, processor.Values())
|
2020-03-27 23:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordBatch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-03-27 23:06:48 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
counter1 := Must(meter).NewInt64Counter("int64.sum")
|
|
|
|
counter2 := Must(meter).NewFloat64Counter("float64.sum")
|
|
|
|
valuerecorder1 := Must(meter).NewInt64ValueRecorder("int64.exact")
|
|
|
|
valuerecorder2 := Must(meter).NewFloat64ValueRecorder("float64.exact")
|
2020-03-27 23:06:48 +02:00
|
|
|
|
|
|
|
sdk.RecordBatch(
|
|
|
|
ctx,
|
2021-02-18 19:59:37 +02:00
|
|
|
[]attribute.KeyValue{
|
|
|
|
attribute.String("A", "B"),
|
|
|
|
attribute.String("C", "D"),
|
2020-03-27 23:06:48 +02:00
|
|
|
},
|
|
|
|
counter1.Measurement(1),
|
|
|
|
counter2.Measurement(2),
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder1.Measurement(3),
|
|
|
|
valuerecorder2.Measurement(4),
|
2020-03-27 23:06:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
sdk.Collect(ctx)
|
|
|
|
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2021-08-13 00:44:58 +02:00
|
|
|
"int64.sum/A=B,C=D/": 1,
|
|
|
|
"float64.sum/A=B,C=D/": 2,
|
|
|
|
"int64.exact/A=B,C=D/": 3,
|
|
|
|
"float64.exact/A=B,C=D/": 4,
|
2021-07-21 19:06:38 +02:00
|
|
|
}, processor.Values())
|
2020-03-19 21:02:46 +02:00
|
|
|
}
|
2020-04-22 05:23:15 +02:00
|
|
|
|
|
|
|
// TestRecordPersistence ensures that a direct-called instrument that
|
|
|
|
// is repeatedly used each interval results in a persistent record, so
|
|
|
|
// that its encoded labels will be cached across collection intervals.
|
|
|
|
func TestRecordPersistence(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, selector, _ := newSDK(t)
|
2020-04-22 05:23:15 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
c := Must(meter).NewFloat64Counter("name.sum")
|
2021-02-18 19:59:37 +02:00
|
|
|
b := c.Bind(attribute.String("bound", "true"))
|
|
|
|
uk := attribute.String("bound", "false")
|
2020-04-22 05:23:15 +02:00
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
c.Add(ctx, 1, uk)
|
|
|
|
b.Add(ctx, 1)
|
|
|
|
sdk.Collect(ctx)
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:06:38 +02:00
|
|
|
require.Equal(t, 4, selector.newAggCount)
|
2020-04-22 05:23:15 +02:00
|
|
|
}
|
2020-05-20 06:33:10 +02:00
|
|
|
|
2020-05-20 19:19:51 +02:00
|
|
|
func TestIncorrectInstruments(t *testing.T) {
|
|
|
|
// The Batch observe/record APIs are susceptible to
|
|
|
|
// uninitialized instruments.
|
2020-11-12 17:28:32 +02:00
|
|
|
var counter metric.Int64Counter
|
|
|
|
var observer metric.Int64ValueObserver
|
2020-05-20 19:19:51 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-05-20 19:19:51 +02:00
|
|
|
|
|
|
|
// Now try with uninitialized instruments.
|
|
|
|
meter.RecordBatch(ctx, nil, counter.Measurement(1))
|
2020-11-12 17:28:32 +02:00
|
|
|
meter.NewBatchObserver(func(_ context.Context, result metric.BatchObserverResult) {
|
2020-05-20 19:19:51 +02:00
|
|
|
result.Observe(nil, observer.Observation(1))
|
|
|
|
})
|
|
|
|
|
|
|
|
collected := sdk.Collect(ctx)
|
2020-06-02 20:30:09 +02:00
|
|
|
require.Equal(t, metricsdk.ErrUninitializedInstrument, testHandler.Flush())
|
2020-05-20 19:19:51 +02:00
|
|
|
require.Equal(t, 0, collected)
|
|
|
|
|
|
|
|
// Now try with instruments from another SDK.
|
2020-11-12 17:28:32 +02:00
|
|
|
var noopMeter metric.Meter
|
|
|
|
counter = metric.Must(noopMeter).NewInt64Counter("name.sum")
|
|
|
|
observer = metric.Must(noopMeter).NewBatchObserver(
|
|
|
|
func(context.Context, metric.BatchObserverResult) {},
|
2020-05-22 00:42:14 +02:00
|
|
|
).NewInt64ValueObserver("observer")
|
2020-05-20 19:19:51 +02:00
|
|
|
|
|
|
|
meter.RecordBatch(ctx, nil, counter.Measurement(1))
|
2020-11-12 17:28:32 +02:00
|
|
|
meter.NewBatchObserver(func(_ context.Context, result metric.BatchObserverResult) {
|
2020-05-20 19:19:51 +02:00
|
|
|
result.Observe(nil, observer.Observation(1))
|
|
|
|
})
|
|
|
|
|
|
|
|
collected = sdk.Collect(ctx)
|
|
|
|
require.Equal(t, 0, collected)
|
2021-07-21 19:06:38 +02:00
|
|
|
require.EqualValues(t, map[string]float64{}, processor.Values())
|
2020-06-02 20:30:09 +02:00
|
|
|
require.Equal(t, metricsdk.ErrUninitializedInstrument, testHandler.Flush())
|
2020-05-20 19:19:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 06:33:10 +02:00
|
|
|
func TestSyncInAsync(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2021-07-21 19:06:38 +02:00
|
|
|
meter, sdk, _, processor := newSDK(t)
|
2020-05-20 06:33:10 +02:00
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
counter := Must(meter).NewFloat64Counter("counter.sum")
|
|
|
|
_ = Must(meter).NewInt64ValueObserver("observer.lastvalue",
|
2020-11-12 17:28:32 +02:00
|
|
|
func(ctx context.Context, result metric.Int64ObserverResult) {
|
2020-05-20 06:33:10 +02:00
|
|
|
result.Observe(10)
|
|
|
|
counter.Add(ctx, 100)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
sdk.Collect(ctx)
|
|
|
|
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2021-08-13 00:44:58 +02:00
|
|
|
"counter.sum//": 100,
|
|
|
|
"observer.lastvalue//": 10,
|
2021-07-21 19:06:38 +02:00
|
|
|
}, processor.Values())
|
2020-05-20 06:33:10 +02:00
|
|
|
}
|