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-03-11 18:11:27 +02:00
|
|
|
"strings"
|
2020-04-22 05:23:15 +02:00
|
|
|
"sync/atomic"
|
2019-11-15 23:01:20 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2020-04-23 21:10:58 +02:00
|
|
|
"go.opentelemetry.io/otel/api/label"
|
2019-11-15 23:01:20 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
2020-03-24 18:30:12 +02:00
|
|
|
metricsdk "go.opentelemetry.io/otel/sdk/metric"
|
2019-11-15 23:01:20 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/array"
|
2020-03-12 05:21:34 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
2020-05-11 19:23:06 +02:00
|
|
|
batchTest "go.opentelemetry.io/otel/sdk/metric/integrator/test"
|
2020-05-19 02:44:28 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
var Must = metric.Must
|
2020-05-19 02:44:28 +02:00
|
|
|
var testResource = resource.New(kv.String("R", "V"))
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
type correctnessIntegrator struct {
|
2020-04-22 05:23:15 +02:00
|
|
|
newAggCount int64
|
|
|
|
|
2020-03-11 18:11:27 +02:00
|
|
|
t *testing.T
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
records []export.Record
|
|
|
|
}
|
|
|
|
|
2020-05-19 02:44:28 +02:00
|
|
|
func newSDK(t *testing.T) (metric.Meter, *metricsdk.Accumulator, *correctnessIntegrator) {
|
|
|
|
integrator := &correctnessIntegrator{
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
accum := metricsdk.NewAccumulator(integrator, metricsdk.WithResource(testResource))
|
|
|
|
meter := metric.WrapMeterImpl(accum, "test")
|
|
|
|
return meter, accum, integrator
|
|
|
|
}
|
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
func (cb *correctnessIntegrator) AggregatorFor(descriptor *metric.Descriptor) (agg export.Aggregator) {
|
2020-03-11 18:11:27 +02:00
|
|
|
name := descriptor.Name()
|
2020-04-22 05:23:15 +02:00
|
|
|
|
2020-03-11 18:11:27 +02:00
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(name, ".counter"):
|
2020-04-22 05:23:15 +02:00
|
|
|
agg = sum.New()
|
2020-03-11 18:11:27 +02:00
|
|
|
case strings.HasSuffix(name, ".disabled"):
|
2020-04-22 05:23:15 +02:00
|
|
|
agg = nil
|
2020-03-11 18:11:27 +02:00
|
|
|
default:
|
2020-04-22 05:23:15 +02:00
|
|
|
agg = array.New()
|
2020-03-11 18:11:27 +02:00
|
|
|
}
|
2020-04-22 05:23:15 +02:00
|
|
|
if agg != nil {
|
|
|
|
atomic.AddInt64(&cb.newAggCount, 1)
|
|
|
|
}
|
|
|
|
return
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
func (cb *correctnessIntegrator) CheckpointSet() export.CheckpointSet {
|
2019-11-15 23:01:20 +02:00
|
|
|
cb.t.Fatal("Should not be called")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
func (*correctnessIntegrator) FinishedCollection() {
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
func (cb *correctnessIntegrator) Process(_ context.Context, record export.Record) error {
|
2019-11-15 23:01:20 +02:00
|
|
|
cb.records = append(cb.records, record)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputRangeTestCounter(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := newSDK(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
var sdkErr error
|
|
|
|
sdk.SetErrorHandler(func(handleErr error) {
|
|
|
|
sdkErr = handleErr
|
|
|
|
})
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
counter := Must(meter).NewInt64Counter("name.counter")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, -1)
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(t, aggregator.ErrNegativeInput, sdkErr)
|
|
|
|
sdkErr = nil
|
|
|
|
|
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-11 19:23:06 +02:00
|
|
|
integrator.records = 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-05-11 19:23:06 +02:00
|
|
|
sum, err := integrator.records[0].Aggregator().(aggregator.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)
|
|
|
|
require.Nil(t, sdkErr)
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
func TestInputRangeTestValueRecorder(t *testing.T) {
|
2019-11-15 23:01:20 +02:00
|
|
|
ctx := context.Background()
|
2020-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := newSDK(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
var sdkErr error
|
|
|
|
sdk.SetErrorHandler(func(handleErr error) {
|
|
|
|
sdkErr = handleErr
|
|
|
|
})
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder := Must(meter).NewFloat64ValueRecorder("name.valuerecorder")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, math.NaN())
|
2020-03-12 05:21:34 +02:00
|
|
|
require.Equal(t, aggregator.ErrNaNInput, sdkErr)
|
2019-11-15 23:01:20 +02:00
|
|
|
sdkErr = nil
|
|
|
|
|
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-05-11 19:23:06 +02:00
|
|
|
integrator.records = nil
|
2020-03-11 18:11:27 +02:00
|
|
|
checkpointed = sdk.Collect(ctx)
|
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
count, err := integrator.records[0].Aggregator().(aggregator.Distribution).Count()
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Equal(t, int64(2), count)
|
|
|
|
require.Equal(t, 1, checkpointed)
|
|
|
|
require.Nil(t, sdkErr)
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisabledInstrument(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := 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-05-11 19:23:06 +02:00
|
|
|
require.Equal(t, 0, len(integrator.records))
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordNaN(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-05-19 02:44:28 +02:00
|
|
|
meter, sdk, _ := newSDK(t)
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
var sdkErr error
|
|
|
|
sdk.SetErrorHandler(func(handleErr error) {
|
|
|
|
sdkErr = handleErr
|
|
|
|
})
|
2020-03-19 21:02:46 +02:00
|
|
|
c := Must(meter).NewFloat64Counter("sum.name")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
require.Nil(t, sdkErr)
|
2020-03-27 23:06:48 +02:00
|
|
|
c.Add(ctx, math.NaN())
|
2019-11-15 23:01:20 +02:00
|
|
|
require.Error(t, sdkErr)
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:11:27 +02:00
|
|
|
func TestSDKLabelsDeduplication(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := newSDK(t)
|
2020-03-11 18:11:27 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
counter := Must(meter).NewInt64Counter("counter")
|
2020-03-11 18:11:27 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
maxKeys = 21
|
|
|
|
keySets = 2
|
|
|
|
repeats = 3
|
|
|
|
)
|
2020-05-14 01:06:03 +02:00
|
|
|
var keysA []kv.Key
|
|
|
|
var keysB []kv.Key
|
2020-03-11 18:11:27 +02:00
|
|
|
|
|
|
|
for i := 0; i < maxKeys; i++ {
|
2020-05-14 01:06:03 +02:00
|
|
|
keysA = append(keysA, kv.Key(fmt.Sprintf("A%03d", i)))
|
|
|
|
keysB = append(keysB, kv.Key(fmt.Sprintf("B%03d", i)))
|
2020-03-11 18:11:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
var allExpect [][]kv.KeyValue
|
2020-03-11 18:11:27 +02:00
|
|
|
for numKeys := 0; numKeys < maxKeys; numKeys++ {
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
var kvsA []kv.KeyValue
|
|
|
|
var kvsB []kv.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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
var expectA []kv.KeyValue
|
|
|
|
var expectB []kv.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)
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
var actual [][]kv.KeyValue
|
2020-05-11 19:23:06 +02:00
|
|
|
for _, rec := range integrator.records {
|
2020-03-11 18:11:27 +02:00
|
|
|
sum, _ := rec.Aggregator().(aggregator.Sum).Sum()
|
2020-05-11 08:44:42 +02:00
|
|
|
require.Equal(t, sum, metric.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)
|
|
|
|
}
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
func newSetIter(kvs ...kv.KeyValue) label.Iterator {
|
2020-04-23 21:10:58 +02:00
|
|
|
labels := label.NewSet(kvs...)
|
|
|
|
return labels.Iter()
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func TestDefaultLabelEncoder(t *testing.T) {
|
2020-04-23 21:10:58 +02:00
|
|
|
encoder := label.DefaultEncoder()
|
2020-03-05 00:19:25 +02:00
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
encoded := encoder.Encode(newSetIter(kv.String("A", "B"), kv.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
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
encoded = encoder.Encode(newSetIter(kv.String("A", "B,c=d"), kv.String(`C\`, "D")))
|
2020-03-05 00:19:25 +02:00
|
|
|
require.Equal(t, `A=B\,c\=d,C\\=D`, encoded)
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
encoded = encoder.Encode(newSetIter(kv.String(`\`, `=`), kv.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(
|
2020-05-14 01:06:03 +02:00
|
|
|
kv.Int("I", 1),
|
|
|
|
kv.Uint("U", 1),
|
|
|
|
kv.Int32("I32", 1),
|
|
|
|
kv.Uint32("U32", 1),
|
|
|
|
kv.Int64("I64", 1),
|
|
|
|
kv.Uint64("U64", 1),
|
|
|
|
kv.Float64("F64", 1),
|
|
|
|
kv.Float64("F64", 1),
|
|
|
|
kv.String("S", "1"),
|
|
|
|
kv.Bool("B", true),
|
2020-04-23 21:10:58 +02:00
|
|
|
))
|
|
|
|
require.Equal(t, "B=true,F64=1,I=1,I32=1,I64=1,S=1,U=1,U32=1,U64=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-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := newSDK(t)
|
2020-03-19 21:02:46 +02:00
|
|
|
|
2020-05-18 20:03:43 +02:00
|
|
|
_ = Must(meter).RegisterFloat64ValueObserver("float.valueobserver", func(result metric.Float64ObserverResult) {
|
2020-05-14 01:06:03 +02:00
|
|
|
result.Observe(1, kv.String("A", "B"))
|
2020-04-03 01:51:37 +02:00
|
|
|
// last value wins
|
2020-05-14 01:06:03 +02:00
|
|
|
result.Observe(-1, kv.String("A", "B"))
|
|
|
|
result.Observe(-1, kv.String("C", "D"))
|
2020-03-19 21:02:46 +02:00
|
|
|
})
|
2020-05-18 20:03:43 +02:00
|
|
|
_ = Must(meter).RegisterInt64ValueObserver("int.valueobserver", func(result metric.Int64ObserverResult) {
|
2020-05-14 01:06:03 +02:00
|
|
|
result.Observe(-1, kv.String("A", "B"))
|
2020-04-03 01:51:37 +02:00
|
|
|
result.Observe(1)
|
|
|
|
// last value wins
|
2020-05-14 01:06:03 +02:00
|
|
|
result.Observe(1, kv.String("A", "B"))
|
2020-03-27 23:06:48 +02:00
|
|
|
result.Observe(1)
|
2020-03-19 21:02:46 +02:00
|
|
|
})
|
2020-05-18 20:03:43 +02:00
|
|
|
_ = Must(meter).RegisterInt64ValueObserver("empty.valueobserver", func(result metric.Int64ObserverResult) {
|
2020-03-19 21:02:46 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
collected := sdk.Collect(ctx)
|
2020-05-14 01:27:52 +02:00
|
|
|
|
|
|
|
require.Equal(t, 4, collected)
|
|
|
|
require.Equal(t, 4, len(integrator.records))
|
|
|
|
|
|
|
|
out := batchTest.NewOutput(label.DefaultEncoder())
|
|
|
|
for _, rec := range integrator.records {
|
|
|
|
_ = out.AddTo(rec)
|
|
|
|
}
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-05-19 02:44:28 +02:00
|
|
|
"float.valueobserver/A=B/R=V": -1,
|
|
|
|
"float.valueobserver/C=D/R=V": -1,
|
|
|
|
"int.valueobserver//R=V": 1,
|
|
|
|
"int.valueobserver/A=B/R=V": 1,
|
2020-05-14 01:27:52 +02:00
|
|
|
}, out.Map)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObserverBatch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := newSDK(t)
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2020-05-18 20:03:43 +02:00
|
|
|
var floatObs metric.Float64ValueObserver
|
|
|
|
var intObs metric.Int64ValueObserver
|
2020-05-14 01:27:52 +02:00
|
|
|
var batch = Must(meter).NewBatchObserver(
|
|
|
|
func(result metric.BatchObserverResult) {
|
|
|
|
result.Observe(
|
|
|
|
[]kv.KeyValue{
|
|
|
|
kv.String("A", "B"),
|
|
|
|
},
|
|
|
|
floatObs.Observation(1),
|
|
|
|
floatObs.Observation(-1),
|
|
|
|
intObs.Observation(-1),
|
|
|
|
intObs.Observation(1),
|
|
|
|
)
|
|
|
|
result.Observe(
|
|
|
|
[]kv.KeyValue{
|
|
|
|
kv.String("C", "D"),
|
|
|
|
},
|
|
|
|
floatObs.Observation(-1),
|
|
|
|
)
|
|
|
|
result.Observe(
|
|
|
|
nil,
|
|
|
|
intObs.Observation(1),
|
|
|
|
intObs.Observation(1),
|
|
|
|
)
|
|
|
|
})
|
2020-05-18 20:03:43 +02:00
|
|
|
floatObs = batch.RegisterFloat64ValueObserver("float.valueobserver")
|
|
|
|
intObs = batch.RegisterInt64ValueObserver("int.valueobserver")
|
2020-05-14 01:27:52 +02:00
|
|
|
|
|
|
|
collected := sdk.Collect(ctx)
|
2020-03-19 21:02:46 +02:00
|
|
|
|
|
|
|
require.Equal(t, 4, collected)
|
2020-05-11 19:23:06 +02:00
|
|
|
require.Equal(t, 4, len(integrator.records))
|
2020-03-19 21:02:46 +02:00
|
|
|
|
2020-04-23 21:10:58 +02:00
|
|
|
out := batchTest.NewOutput(label.DefaultEncoder())
|
2020-05-11 19:23:06 +02:00
|
|
|
for _, rec := range integrator.records {
|
2020-03-19 21:02:46 +02:00
|
|
|
_ = out.AddTo(rec)
|
|
|
|
}
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-05-19 02:44:28 +02:00
|
|
|
"float.valueobserver/A=B/R=V": -1,
|
|
|
|
"float.valueobserver/C=D/R=V": -1,
|
|
|
|
"int.valueobserver//R=V": 1,
|
|
|
|
"int.valueobserver/A=B/R=V": 1,
|
2020-03-24 18:30:12 +02:00
|
|
|
}, out.Map)
|
2020-03-27 23:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecordBatch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := newSDK(t)
|
2020-03-27 23:06:48 +02:00
|
|
|
|
|
|
|
counter1 := Must(meter).NewInt64Counter("int64.counter")
|
|
|
|
counter2 := Must(meter).NewFloat64Counter("float64.counter")
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder1 := Must(meter).NewInt64ValueRecorder("int64.valuerecorder")
|
|
|
|
valuerecorder2 := Must(meter).NewFloat64ValueRecorder("float64.valuerecorder")
|
2020-03-27 23:06:48 +02:00
|
|
|
|
|
|
|
sdk.RecordBatch(
|
|
|
|
ctx,
|
2020-05-14 01:06:03 +02:00
|
|
|
[]kv.KeyValue{
|
|
|
|
kv.String("A", "B"),
|
|
|
|
kv.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)
|
|
|
|
|
2020-04-23 21:10:58 +02:00
|
|
|
out := batchTest.NewOutput(label.DefaultEncoder())
|
2020-05-11 19:23:06 +02:00
|
|
|
for _, rec := range integrator.records {
|
2020-03-27 23:06:48 +02:00
|
|
|
_ = out.AddTo(rec)
|
|
|
|
}
|
|
|
|
require.EqualValues(t, map[string]float64{
|
2020-05-19 02:44:28 +02:00
|
|
|
"int64.counter/A=B,C=D/R=V": 1,
|
|
|
|
"float64.counter/A=B,C=D/R=V": 2,
|
|
|
|
"int64.valuerecorder/A=B,C=D/R=V": 3,
|
|
|
|
"float64.valuerecorder/A=B,C=D/R=V": 4,
|
2020-03-27 23:06:48 +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-05-19 02:44:28 +02:00
|
|
|
meter, sdk, integrator := newSDK(t)
|
2020-04-22 05:23:15 +02:00
|
|
|
|
|
|
|
c := Must(meter).NewFloat64Counter("sum.name")
|
2020-05-14 01:06:03 +02:00
|
|
|
b := c.Bind(kv.String("bound", "true"))
|
|
|
|
uk := kv.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-05-11 19:23:06 +02:00
|
|
|
require.Equal(t, int64(2), integrator.newAggCount)
|
2020-04-22 05:23:15 +02:00
|
|
|
}
|