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"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
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"
|
2020-05-19 02:44:28 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
var Must = metric.Must
|
2021-02-18 19:59:37 +02:00
|
|
|
var testResource = resource.NewWithAttributes(attribute.String("R", "V"))
|
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-08-13 22:12:32 +02:00
|
|
|
// correctnessProcessor could be replaced with processortest.Processor
|
|
|
|
// with a non-default aggregator selector. TODO(#872) use the
|
|
|
|
// processortest code here.
|
2020-06-23 21:00:15 +02:00
|
|
|
type correctnessProcessor struct {
|
2020-03-11 18:11:27 +02:00
|
|
|
t *testing.T
|
2020-06-13 09:55:01 +02:00
|
|
|
*testSelector
|
2020-03-11 18:11:27 +02:00
|
|
|
|
2020-06-18 19:16:33 +02:00
|
|
|
accumulations []export.Accumulation
|
2019-11-15 23:01:20 +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
|
|
|
}
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
func newSDK(t *testing.T) (metric.Meter, *metricsdk.Accumulator, *correctnessProcessor) {
|
2020-06-02 20:30:09 +02:00
|
|
|
testHandler.Reset()
|
2020-06-23 21:00:15 +02:00
|
|
|
processor := &correctnessProcessor{
|
2020-06-13 09:55:01 +02:00
|
|
|
t: t,
|
2020-08-10 22:35:15 +02:00
|
|
|
testSelector: &testSelector{selector: processortest.AggregatorSelector()},
|
2020-05-19 02:44:28 +02:00
|
|
|
}
|
2020-05-19 20:49:24 +02:00
|
|
|
accum := metricsdk.NewAccumulator(
|
2020-06-23 21:00:15 +02:00
|
|
|
processor,
|
2020-10-31 20:16:55 +02:00
|
|
|
testResource,
|
2020-05-19 20:49:24 +02:00
|
|
|
)
|
2020-11-12 17:28:32 +02:00
|
|
|
meter := metric.WrapMeterImpl(accum, "test")
|
2020-06-23 21:00:15 +02:00
|
|
|
return meter, accum, processor
|
2020-05-19 02:44:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:00:15 +02:00
|
|
|
func (ci *correctnessProcessor) Process(accumulation export.Accumulation) error {
|
2020-06-18 19:16:33 +02:00
|
|
|
ci.accumulations = append(ci.accumulations, accumulation)
|
2019-11-15 23:01:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:00:22 +02:00
|
|
|
func TestInputRangeCounter(t *testing.T) {
|
2019-11-15 23:01:20 +02:00
|
|
|
ctx := context.Background()
|
2020-06-23 21:00:15 +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
|
|
|
|
2020-06-23 21:00:15 +02:00
|
|
|
processor.accumulations = nil
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, 1)
|
2020-03-11 18:11:27 +02:00
|
|
|
checkpointed = sdk.Collect(ctx)
|
2020-06-23 21:00:15 +02:00
|
|
|
sum, err := processor.accumulations[0].Aggregator().(aggregation.Sum).Sum()
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(t, int64(1), sum.AsInt64())
|
|
|
|
require.Equal(t, 1, checkpointed)
|
|
|
|
require.Nil(t, err)
|
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()
|
2020-06-23 21:00:15 +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)
|
2020-06-23 21:00:15 +02:00
|
|
|
sum, err := processor.accumulations[0].Aggregator().(aggregation.Sum).Sum()
|
2020-05-19 19:00:22 +02:00
|
|
|
require.Equal(t, int64(1), sum.AsInt64())
|
|
|
|
require.Equal(t, 1, checkpointed)
|
|
|
|
require.Nil(t, err)
|
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()
|
2020-06-23 21:00:15 +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
|
|
|
|
2020-06-23 21:00:15 +02:00
|
|
|
processor.accumulations = nil
|
2020-03-11 18:11:27 +02:00
|
|
|
checkpointed = sdk.Collect(ctx)
|
|
|
|
|
2021-01-12 20:19:13 +02:00
|
|
|
count, err := processor.accumulations[0].Aggregator().(aggregation.Count).Count()
|
2021-01-06 09:17:20 +02:00
|
|
|
require.Equal(t, uint64(2), count)
|
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
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisabledInstrument(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-23 21:00:15 +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)
|
2020-06-23 21:00:15 +02:00
|
|
|
require.Equal(t, 0, len(processor.accumulations))
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordNaN(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-02 20:30:09 +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()
|
2020-06-23 21:00:15 +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-02-18 19:59:37 +02:00
|
|
|
var allExpect [][]attribute.KeyValue
|
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...)
|
2020-03-11 18:11:27 +02:00
|
|
|
allExpect = append(allExpect, expectA)
|
|
|
|
|
|
|
|
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...)
|
2020-03-11 18:11:27 +02:00
|
|
|
allExpect = append(allExpect, expectB)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sdk.Collect(ctx)
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
var actual [][]attribute.KeyValue
|
2020-06-23 21:00:15 +02:00
|
|
|
for _, rec := range processor.accumulations {
|
2020-06-10 07:53:30 +02:00
|
|
|
sum, _ := rec.Aggregator().(aggregation.Sum).Sum()
|
2020-11-11 17:24:12 +02:00
|
|
|
require.Equal(t, sum, number.NewInt64Number(2))
|
2020-03-11 18:11:27 +02:00
|
|
|
|
2020-04-23 21:10:58 +02:00
|
|
|
kvs := rec.Labels().ToSlice()
|
Replace `Ordered` with an iterator in `export.Labels`. (#567)
* Do not expose a slice of labels in export.Record
This is really an inconvenient implementation detail leak - we may
want to store labels in a different way. Replace it with an iterator -
it does not force us to use slice of key values as a storage in the
long run.
* Add Len to LabelIterator
It may come in handy in several situations, where we don't have access
to export.Labels object, but only to the label iterator.
* Use reflect value label iterator for the fixed labels
* add reset operation to iterator
Makes my life easier when writing a benchmark. Might also be an
alternative to cloning the iterator.
* Add benchmarks for iterators
* Add import comment
* Add clone operation to label iterator
* Move iterator tests to a separate package
* Add tests for cloning iterators
* Pass label iterator to export labels
* Use non-addressable array reflect values
By not using the value created by `reflect.New()`, but rather by
`reflect.ValueOf()`, we get a non-addressable array in the value,
which does not infer an allocation cost when getting an element from
the array.
* Drop zero iterator
This can be substituted by a reflect value iterator that goes over a
value with a zero-sized array.
* Add a simple iterator that implements label iterator
In the long run this will completely replace the LabelIterator
interface.
* Replace reflect value iterator with simple iterator
* Pass label storage to new export labels, not label iterator
* Drop label iterator interface, rename storage iterator to label iterator
* Drop clone operation from iterator
It's a leftover from interface times and now it's pointless - the
iterator is a simple struct, so cloning it is a simple copy.
* Drop Reset from label iterator
The sole existence of Reset was actually for benchmarking convenience.
Now we can just copy the iterator cheaply, so a need for Reset is no
more.
* Drop noop iterator tests
* Move back iterator tests to export package
* Eagerly get the reflect value of ordered labels
So we won't get into problems when several goroutines want to iterate
the same labels at the same time. Not sure if this would be a big
deal, since every goroutine would compute the same reflect.Value, but
concurrent write to the same memory is bad anyway. And it doesn't cost
us any extra allocations anyway.
* Replace NewSliceLabelIterator() with a method of LabelSlice
* Add some documentation
* Documentation fixes
2020-03-20 00:01:34 +02:00
|
|
|
actual = append(actual, kvs)
|
2020-03-11 18:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
require.ElementsMatch(t, allExpect, actual)
|
|
|
|
}
|
|
|
|
|
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()
|
2020-06-23 21:00:15 +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++ {
|
|
|
|
processor.accumulations = nil
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2020-12-11 04:13:08 +02:00
|
|
|
collected := sdk.Collect(ctx)
|
|
|
|
require.Equal(t, collected, len(processor.accumulations))
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
out := processortest.NewOutput(attribute.DefaultEncoder())
|
2020-12-11 04:13:08 +02:00
|
|
|
for _, rec := range processor.accumulations {
|
|
|
|
require.NoError(t, out.AddAccumulation(rec))
|
|
|
|
}
|
|
|
|
mult := float64(mult)
|
|
|
|
require.EqualValues(t, map[string]float64{
|
|
|
|
"float.valueobserver.lastvalue/A=B/R=V": -mult,
|
|
|
|
"float.valueobserver.lastvalue/C=D/R=V": -mult,
|
|
|
|
"int.valueobserver.lastvalue//R=V": mult,
|
|
|
|
"int.valueobserver.lastvalue/A=B/R=V": mult,
|
|
|
|
|
|
|
|
"float.sumobserver.sum/A=B/R=V": 2 * mult,
|
|
|
|
"float.sumobserver.sum/C=D/R=V": mult,
|
|
|
|
"int.sumobserver.sum//R=V": mult,
|
|
|
|
"int.sumobserver.sum/A=B/R=V": mult,
|
|
|
|
|
|
|
|
"float.updownsumobserver.sum/A=B/R=V": -2 * mult,
|
|
|
|
"float.updownsumobserver.sum/C=D/R=V": mult,
|
|
|
|
"int.updownsumobserver.sum//R=V": -mult,
|
|
|
|
"int.updownsumobserver.sum/A=B/R=V": mult,
|
|
|
|
}, out.Map())
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:49:24 +02:00
|
|
|
func TestSumObserverInputRange(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-23 21:00:15 +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)
|
2020-06-23 21:00:15 +02:00
|
|
|
require.Equal(t, 0, len(processor.accumulations))
|
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()
|
2020-06-23 21:00:15 +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
|
|
|
|
2020-06-23 21:00:15 +02:00
|
|
|
require.Equal(t, collected, len(processor.accumulations))
|
2020-03-19 21:02:46 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
out := processortest.NewOutput(attribute.DefaultEncoder())
|
2020-06-23 21:00:15 +02:00
|
|
|
for _, rec := range processor.accumulations {
|
2020-06-18 19:16:33 +02:00
|
|
|
require.NoError(t, out.AddAccumulation(rec))
|
2020-03-19 21:02:46 +02:00
|
|
|
}
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-06-13 09:55:01 +02:00
|
|
|
"float.sumobserver.sum//R=V": 1.1,
|
|
|
|
"float.sumobserver.sum/A=B/R=V": 1000,
|
|
|
|
"int.sumobserver.sum//R=V": 10,
|
|
|
|
"int.sumobserver.sum/A=B/R=V": 100,
|
|
|
|
|
|
|
|
"int.updownsumobserver.sum/A=B/R=V": -100,
|
|
|
|
"float.updownsumobserver.sum/A=B/R=V": -1000,
|
|
|
|
"int.updownsumobserver.sum//R=V": 10,
|
|
|
|
"float.updownsumobserver.sum/C=D/R=V": -1,
|
|
|
|
|
|
|
|
"float.valueobserver.lastvalue/A=B/R=V": -1,
|
|
|
|
"float.valueobserver.lastvalue/C=D/R=V": -1,
|
|
|
|
"int.valueobserver.lastvalue//R=V": 1,
|
|
|
|
"int.valueobserver.lastvalue/A=B/R=V": 1,
|
2020-08-11 19:25:47 +02:00
|
|
|
}, out.Map())
|
2020-03-27 23:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordBatch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-23 21:00:15 +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)
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
out := processortest.NewOutput(attribute.DefaultEncoder())
|
2020-06-23 21:00:15 +02:00
|
|
|
for _, rec := range processor.accumulations {
|
2020-06-18 19:16:33 +02:00
|
|
|
require.NoError(t, out.AddAccumulation(rec))
|
2020-03-27 23:06:48 +02:00
|
|
|
}
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-06-13 09:55:01 +02:00
|
|
|
"int64.sum/A=B,C=D/R=V": 1,
|
|
|
|
"float64.sum/A=B,C=D/R=V": 2,
|
|
|
|
"int64.exact/A=B,C=D/R=V": 3,
|
|
|
|
"float64.exact/A=B,C=D/R=V": 4,
|
2020-08-11 19:25:47 +02:00
|
|
|
}, out.Map())
|
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()
|
2020-06-23 21:00:15 +02:00
|
|
|
meter, sdk, processor := 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)
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:00:15 +02:00
|
|
|
require.Equal(t, 4, processor.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()
|
2020-06-02 20:30:09 +02:00
|
|
|
meter, sdk, _ := 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)
|
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()
|
2020-06-23 21:00:15 +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)
|
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
out := processortest.NewOutput(attribute.DefaultEncoder())
|
2020-06-23 21:00:15 +02:00
|
|
|
for _, rec := range processor.accumulations {
|
2020-06-18 19:16:33 +02:00
|
|
|
require.NoError(t, out.AddAccumulation(rec))
|
2020-05-20 06:33:10 +02:00
|
|
|
}
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-06-13 09:55:01 +02:00
|
|
|
"counter.sum//R=V": 100,
|
|
|
|
"observer.lastvalue//R=V": 10,
|
2020-08-11 19:25:47 +02:00
|
|
|
}, out.Map())
|
2020-05-20 06:33:10 +02:00
|
|
|
}
|