2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-15 18:28:36 +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.
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
package metric_test
|
2019-10-15 18:28:36 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-11 20:57:57 +02:00
|
|
|
"errors"
|
2019-10-15 18:28:36 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-11-13 18:35:04 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
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"
|
2021-06-10 21:05:25 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/metrictest"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2021-08-12 01:02:28 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/sdkapi"
|
2021-05-12 20:51:26 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/unit"
|
2019-10-15 18:28:36 +02:00
|
|
|
)
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
var Must = metric.Must
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
var (
|
2021-08-12 01:02:28 +02:00
|
|
|
syncKinds = []sdkapi.InstrumentKind{
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.HistogramInstrumentKind,
|
2021-08-12 01:02:28 +02:00
|
|
|
sdkapi.CounterInstrumentKind,
|
|
|
|
sdkapi.UpDownCounterInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
2021-08-12 01:02:28 +02:00
|
|
|
asyncKinds = []sdkapi.InstrumentKind{
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.GaugeObserverInstrumentKind,
|
|
|
|
sdkapi.CounterObserverInstrumentKind,
|
|
|
|
sdkapi.UpDownCounterObserverInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
2021-08-12 01:02:28 +02:00
|
|
|
addingKinds = []sdkapi.InstrumentKind{
|
|
|
|
sdkapi.CounterInstrumentKind,
|
|
|
|
sdkapi.UpDownCounterInstrumentKind,
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.CounterObserverInstrumentKind,
|
|
|
|
sdkapi.UpDownCounterObserverInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
2021-08-12 01:02:28 +02:00
|
|
|
groupingKinds = []sdkapi.InstrumentKind{
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.HistogramInstrumentKind,
|
|
|
|
sdkapi.GaugeObserverInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 01:02:28 +02:00
|
|
|
monotonicKinds = []sdkapi.InstrumentKind{
|
|
|
|
sdkapi.CounterInstrumentKind,
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.CounterObserverInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 01:02:28 +02:00
|
|
|
nonMonotonicKinds = []sdkapi.InstrumentKind{
|
|
|
|
sdkapi.UpDownCounterInstrumentKind,
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.UpDownCounterObserverInstrumentKind,
|
|
|
|
sdkapi.HistogramInstrumentKind,
|
|
|
|
sdkapi.GaugeObserverInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 01:02:28 +02:00
|
|
|
precomputedSumKinds = []sdkapi.InstrumentKind{
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.CounterObserverInstrumentKind,
|
|
|
|
sdkapi.UpDownCounterObserverInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 01:02:28 +02:00
|
|
|
nonPrecomputedSumKinds = []sdkapi.InstrumentKind{
|
|
|
|
sdkapi.CounterInstrumentKind,
|
|
|
|
sdkapi.UpDownCounterInstrumentKind,
|
2021-09-01 22:38:37 +02:00
|
|
|
sdkapi.HistogramInstrumentKind,
|
|
|
|
sdkapi.GaugeObserverInstrumentKind,
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSynchronous(t *testing.T) {
|
|
|
|
for _, k := range syncKinds {
|
|
|
|
require.True(t, k.Synchronous())
|
|
|
|
require.False(t, k.Asynchronous())
|
|
|
|
}
|
|
|
|
for _, k := range asyncKinds {
|
|
|
|
require.True(t, k.Asynchronous())
|
|
|
|
require.False(t, k.Synchronous())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGrouping(t *testing.T) {
|
|
|
|
for _, k := range groupingKinds {
|
|
|
|
require.True(t, k.Grouping())
|
|
|
|
require.False(t, k.Adding())
|
|
|
|
}
|
|
|
|
for _, k := range addingKinds {
|
|
|
|
require.True(t, k.Adding())
|
|
|
|
require.False(t, k.Grouping())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMonotonic(t *testing.T) {
|
|
|
|
for _, k := range monotonicKinds {
|
|
|
|
require.True(t, k.Monotonic())
|
|
|
|
}
|
|
|
|
for _, k := range nonMonotonicKinds {
|
|
|
|
require.False(t, k.Monotonic())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrecomputedSum(t *testing.T) {
|
|
|
|
for _, k := range precomputedSumKinds {
|
|
|
|
require.True(t, k.PrecomputedSum())
|
|
|
|
}
|
|
|
|
for _, k := range nonPrecomputedSumKinds {
|
|
|
|
require.False(t, k.PrecomputedSum())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func checkSyncBatches(ctx context.Context, t *testing.T, labels []attribute.KeyValue, provider *metrictest.MeterProvider, nkind number.Kind, mkind sdkapi.InstrumentKind, instrument sdkapi.InstrumentImpl, expected ...float64) {
|
2020-08-27 22:03:56 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
batchesCount := len(provider.MeasurementBatches)
|
|
|
|
if len(provider.MeasurementBatches) != len(expected) {
|
|
|
|
t.Errorf("Expected %d recorded measurement batches, got %d", batchesCount, len(provider.MeasurementBatches))
|
2020-08-27 22:03:56 +02:00
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
recorded := metrictest.AsStructs(provider.MeasurementBatches)
|
2020-08-27 22:03:56 +02:00
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
for i, batch := range provider.MeasurementBatches {
|
2020-08-27 22:03:56 +02:00
|
|
|
if len(batch.Measurements) != 1 {
|
|
|
|
t.Errorf("Expected 1 measurement in batch %d, got %d", i, len(batch.Measurements))
|
|
|
|
}
|
|
|
|
|
|
|
|
measurement := batch.Measurements[0]
|
|
|
|
descriptor := measurement.Instrument.Descriptor()
|
|
|
|
|
2021-06-10 21:05:25 +02:00
|
|
|
expected := metrictest.Measured{
|
2021-09-27 17:51:47 +02:00
|
|
|
Name: descriptor.Name(),
|
|
|
|
Library: metrictest.Library{
|
|
|
|
InstrumentationName: "apitest",
|
|
|
|
},
|
|
|
|
Labels: metrictest.LabelsToMap(labels...),
|
|
|
|
Number: metrictest.ResolveNumberByKind(t, nkind, expected[i]),
|
2020-08-27 22:03:56 +02:00
|
|
|
}
|
|
|
|
require.Equal(t, expected, recorded[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func TestOptions(t *testing.T) {
|
2019-10-15 18:28:36 +02:00
|
|
|
type testcase struct {
|
2021-09-27 17:51:47 +02:00
|
|
|
name string
|
|
|
|
opts []metric.InstrumentOption
|
|
|
|
desc string
|
|
|
|
unit unit.Unit
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
testcases := []testcase{
|
|
|
|
{
|
2021-09-27 17:51:47 +02:00
|
|
|
name: "no opts",
|
|
|
|
opts: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
2020-11-12 17:28:32 +02:00
|
|
|
opts: []metric.InstrumentOption{
|
|
|
|
metric.WithDescription("stuff"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
desc: "stuff",
|
|
|
|
unit: "",
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description override",
|
2020-11-12 17:28:32 +02:00
|
|
|
opts: []metric.InstrumentOption{
|
|
|
|
metric.WithDescription("stuff"),
|
|
|
|
metric.WithDescription("things"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
desc: "things",
|
|
|
|
unit: "",
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit",
|
2020-11-12 17:28:32 +02:00
|
|
|
opts: []metric.InstrumentOption{
|
|
|
|
metric.WithUnit("s"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
desc: "",
|
|
|
|
unit: "s",
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
2021-05-27 16:53:56 +02:00
|
|
|
{
|
|
|
|
name: "description override",
|
|
|
|
opts: []metric.InstrumentOption{
|
|
|
|
metric.WithDescription("stuff"),
|
|
|
|
metric.WithDescription("things"),
|
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
desc: "things",
|
|
|
|
unit: "",
|
2021-05-27 16:53:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit",
|
|
|
|
opts: []metric.InstrumentOption{
|
|
|
|
metric.WithUnit("s"),
|
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
desc: "",
|
|
|
|
unit: "s",
|
2021-05-27 16:53:56 +02:00
|
|
|
},
|
|
|
|
|
2019-10-15 18:28:36 +02:00
|
|
|
{
|
|
|
|
name: "unit override",
|
2020-11-12 17:28:32 +02:00
|
|
|
opts: []metric.InstrumentOption{
|
|
|
|
metric.WithUnit("s"),
|
|
|
|
metric.WithUnit("h"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
desc: "",
|
|
|
|
unit: "h",
|
2021-05-27 16:53:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all",
|
|
|
|
opts: []metric.InstrumentOption{
|
|
|
|
metric.WithDescription("stuff"),
|
|
|
|
metric.WithUnit("s"),
|
|
|
|
},
|
2021-09-27 17:51:47 +02:00
|
|
|
desc: "stuff",
|
|
|
|
unit: "s",
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for idx, tt := range testcases {
|
|
|
|
t.Logf("Testing counter case %s (%d)", tt.name, idx)
|
2021-05-27 16:53:56 +02:00
|
|
|
cfg := metric.NewInstrumentConfig(tt.opts...)
|
|
|
|
if diff := cmp.Diff(cfg.Description(), tt.desc); diff != "" {
|
|
|
|
t.Errorf("Compare Description: -got +want %s", diff)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(cfg.Unit(), tt.unit); diff != "" {
|
|
|
|
t.Errorf("Compare Unit: -got +want %s", diff)
|
|
|
|
}
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
func testPair() (*metrictest.MeterProvider, metric.Meter) {
|
|
|
|
provider := metrictest.NewMeterProvider()
|
|
|
|
return provider, provider.Meter("apitest")
|
|
|
|
}
|
2019-10-15 18:28:36 +02:00
|
|
|
|
|
|
|
func TestCounter(t *testing.T) {
|
2020-05-19 19:00:22 +02:00
|
|
|
// N.B. the API does not check for negative
|
|
|
|
// values, that's the SDK's responsibility.
|
|
|
|
t.Run("float64 counter", func(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2020-03-11 20:57:57 +02:00
|
|
|
c := Must(meter).NewFloat64Counter("test.counter.float")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.String("A", "B")}
|
2020-05-19 19:00:22 +02:00
|
|
|
c.Add(ctx, 1994.1, labels...)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, c.Measurement(42))
|
2021-09-27 17:51:47 +02:00
|
|
|
checkSyncBatches(ctx, t, labels, provider, number.Float64Kind, sdkapi.CounterInstrumentKind, c.SyncImpl(),
|
2021-11-17 17:51:04 +02:00
|
|
|
1994.1, 42,
|
2020-05-19 19:00:22 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
t.Run("int64 counter", func(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2020-03-11 20:57:57 +02:00
|
|
|
c := Must(meter).NewInt64Counter("test.counter.int")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.String("A", "B"), attribute.String("C", "D")}
|
2020-03-27 23:06:48 +02:00
|
|
|
c.Add(ctx, 42, labels...)
|
2020-05-19 19:00:22 +02:00
|
|
|
meter.RecordBatch(ctx, labels, c.Measurement(420000))
|
2021-09-27 17:51:47 +02:00
|
|
|
checkSyncBatches(ctx, t, labels, provider, number.Int64Kind, sdkapi.CounterInstrumentKind, c.SyncImpl(),
|
2021-11-17 17:51:04 +02:00
|
|
|
42, 420000,
|
2020-05-19 19:00:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("int64 updowncounter", func(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2020-05-19 19:00:22 +02:00
|
|
|
c := Must(meter).NewInt64UpDownCounter("test.updowncounter.int")
|
|
|
|
ctx := context.Background()
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.String("A", "B"), attribute.String("C", "D")}
|
2020-05-19 19:00:22 +02:00
|
|
|
c.Add(ctx, 100, labels...)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, c.Measurement(42))
|
2021-09-27 17:51:47 +02:00
|
|
|
checkSyncBatches(ctx, t, labels, provider, number.Int64Kind, sdkapi.UpDownCounterInstrumentKind, c.SyncImpl(),
|
2021-11-17 17:51:04 +02:00
|
|
|
100, 42,
|
2020-05-19 19:00:22 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
t.Run("float64 updowncounter", func(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2020-05-19 19:00:22 +02:00
|
|
|
c := Must(meter).NewFloat64UpDownCounter("test.updowncounter.float")
|
|
|
|
ctx := context.Background()
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.String("A", "B"), attribute.String("C", "D")}
|
2020-05-19 19:00:22 +02:00
|
|
|
c.Add(ctx, 100.1, labels...)
|
|
|
|
meter.RecordBatch(ctx, labels, c.Measurement(-100.1))
|
2021-09-27 17:51:47 +02:00
|
|
|
checkSyncBatches(ctx, t, labels, provider, number.Float64Kind, sdkapi.UpDownCounterInstrumentKind, c.SyncImpl(),
|
2021-11-17 17:51:04 +02:00
|
|
|
100.1, -100.1,
|
2020-05-19 19:00:22 +02:00
|
|
|
)
|
|
|
|
})
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 22:38:37 +02:00
|
|
|
func TestHistogram(t *testing.T) {
|
|
|
|
t.Run("float64 histogram", func(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2021-09-01 22:38:37 +02:00
|
|
|
m := Must(meter).NewFloat64Histogram("test.histogram.float")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{}
|
2020-03-27 23:06:48 +02:00
|
|
|
m.Record(ctx, 42, labels...)
|
2020-05-19 19:00:22 +02:00
|
|
|
meter.RecordBatch(ctx, labels, m.Measurement(-100.5))
|
2021-09-27 17:51:47 +02:00
|
|
|
checkSyncBatches(ctx, t, labels, provider, number.Float64Kind, sdkapi.HistogramInstrumentKind, m.SyncImpl(),
|
2021-11-17 17:51:04 +02:00
|
|
|
42, -100.5,
|
2020-05-19 19:00:22 +02:00
|
|
|
)
|
|
|
|
})
|
2021-09-01 22:38:37 +02:00
|
|
|
t.Run("int64 histogram", func(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2021-09-01 22:38:37 +02:00
|
|
|
m := Must(meter).NewInt64Histogram("test.histogram.int")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.Int("I", 1)}
|
2020-05-19 19:00:22 +02:00
|
|
|
m.Record(ctx, 173, labels...)
|
|
|
|
meter.RecordBatch(ctx, labels, m.Measurement(0))
|
2021-09-27 17:51:47 +02:00
|
|
|
checkSyncBatches(ctx, t, labels, provider, number.Int64Kind, sdkapi.HistogramInstrumentKind, m.SyncImpl(),
|
2021-11-17 17:51:04 +02:00
|
|
|
173, 0,
|
2020-05-19 19:00:22 +02:00
|
|
|
)
|
|
|
|
})
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 20:03:43 +02:00
|
|
|
func TestObserverInstruments(t *testing.T) {
|
2021-09-01 22:38:37 +02:00
|
|
|
t.Run("float gauge", func(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.String("O", "P")}
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2021-09-01 22:38:37 +02:00
|
|
|
o := Must(meter).NewFloat64GaugeObserver("test.gauge.float", func(_ context.Context, result metric.Float64ObserverResult) {
|
2020-05-19 20:49:24 +02:00
|
|
|
result.Observe(42.1, labels...)
|
2020-03-05 22:15:30 +02:00
|
|
|
})
|
2021-09-27 17:51:47 +02:00
|
|
|
provider.RunAsyncInstruments()
|
|
|
|
checkObserverBatch(t, labels, provider, number.Float64Kind, sdkapi.GaugeObserverInstrumentKind, o.AsyncImpl(),
|
2020-05-19 20:49:24 +02:00
|
|
|
42.1,
|
|
|
|
)
|
|
|
|
})
|
2021-09-01 22:38:37 +02:00
|
|
|
t.Run("int gauge", func(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{}
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
|
|
|
o := Must(meter).NewInt64GaugeObserver("test.gauge.int", func(_ context.Context, result metric.Int64ObserverResult) {
|
2020-05-19 20:49:24 +02:00
|
|
|
result.Observe(-142, labels...)
|
2020-03-05 22:15:30 +02:00
|
|
|
})
|
2021-09-27 17:51:47 +02:00
|
|
|
provider.RunAsyncInstruments()
|
|
|
|
checkObserverBatch(t, labels, provider, number.Int64Kind, sdkapi.GaugeObserverInstrumentKind, o.AsyncImpl(),
|
2020-05-19 20:49:24 +02:00
|
|
|
-142,
|
|
|
|
)
|
|
|
|
})
|
2021-09-01 22:38:37 +02:00
|
|
|
t.Run("float counterobserver", func(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.String("O", "P")}
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
|
|
|
o := Must(meter).NewFloat64CounterObserver("test.counter.float", func(_ context.Context, result metric.Float64ObserverResult) {
|
2020-05-19 20:49:24 +02:00
|
|
|
result.Observe(42.1, labels...)
|
|
|
|
})
|
2021-09-27 17:51:47 +02:00
|
|
|
provider.RunAsyncInstruments()
|
|
|
|
checkObserverBatch(t, labels, provider, number.Float64Kind, sdkapi.CounterObserverInstrumentKind, o.AsyncImpl(),
|
2020-05-19 20:49:24 +02:00
|
|
|
42.1,
|
|
|
|
)
|
|
|
|
})
|
2021-09-01 22:38:37 +02:00
|
|
|
t.Run("int counterobserver", func(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{}
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
|
|
|
o := Must(meter).NewInt64CounterObserver("test.counter.int", func(_ context.Context, result metric.Int64ObserverResult) {
|
2020-05-19 20:49:24 +02:00
|
|
|
result.Observe(-142, labels...)
|
|
|
|
})
|
2021-09-27 17:51:47 +02:00
|
|
|
provider.RunAsyncInstruments()
|
|
|
|
checkObserverBatch(t, labels, provider, number.Int64Kind, sdkapi.CounterObserverInstrumentKind, o.AsyncImpl(),
|
2020-05-19 20:49:24 +02:00
|
|
|
-142,
|
|
|
|
)
|
|
|
|
})
|
2021-09-01 22:38:37 +02:00
|
|
|
t.Run("float updowncounterobserver", func(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{attribute.String("O", "P")}
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
|
|
|
o := Must(meter).NewFloat64UpDownCounterObserver("test.updowncounter.float", func(_ context.Context, result metric.Float64ObserverResult) {
|
2020-05-20 19:19:51 +02:00
|
|
|
result.Observe(42.1, labels...)
|
|
|
|
})
|
2021-09-27 17:51:47 +02:00
|
|
|
provider.RunAsyncInstruments()
|
|
|
|
checkObserverBatch(t, labels, provider, number.Float64Kind, sdkapi.UpDownCounterObserverInstrumentKind, o.AsyncImpl(),
|
2020-05-20 19:19:51 +02:00
|
|
|
42.1,
|
|
|
|
)
|
|
|
|
})
|
2021-09-01 22:38:37 +02:00
|
|
|
t.Run("int updowncounterobserver", func(t *testing.T) {
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{}
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
|
|
|
o := Must(meter).NewInt64UpDownCounterObserver("test..int", func(_ context.Context, result metric.Int64ObserverResult) {
|
2020-05-20 19:19:51 +02:00
|
|
|
result.Observe(-142, labels...)
|
|
|
|
})
|
2021-09-27 17:51:47 +02:00
|
|
|
provider.RunAsyncInstruments()
|
|
|
|
checkObserverBatch(t, labels, provider, number.Int64Kind, sdkapi.UpDownCounterObserverInstrumentKind, o.AsyncImpl(),
|
2020-05-20 19:19:51 +02:00
|
|
|
-142,
|
|
|
|
)
|
|
|
|
})
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 20:03:43 +02:00
|
|
|
func TestBatchObserverInstruments(t *testing.T) {
|
2021-09-27 17:51:47 +02:00
|
|
|
provider, meter := testPair()
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2021-09-01 22:38:37 +02:00
|
|
|
var obs1 metric.Int64GaugeObserver
|
|
|
|
var obs2 metric.Float64GaugeObserver
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2021-02-18 19:59:37 +02:00
|
|
|
labels := []attribute.KeyValue{
|
|
|
|
attribute.String("A", "B"),
|
|
|
|
attribute.String("C", "D"),
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cb := 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(labels,
|
|
|
|
obs1.Observation(42),
|
|
|
|
obs2.Observation(42.0),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2021-09-27 17:51:47 +02:00
|
|
|
obs1 = cb.NewInt64GaugeObserver("test.gauge.int")
|
|
|
|
obs2 = cb.NewFloat64GaugeObserver("test.gauge.float")
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
provider.RunAsyncInstruments()
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
require.Len(t, provider.MeasurementBatches, 1)
|
2020-05-14 01:27:52 +02:00
|
|
|
|
2021-06-10 21:05:25 +02:00
|
|
|
impl1 := obs1.AsyncImpl().Implementation().(*metrictest.Async)
|
|
|
|
impl2 := obs2.AsyncImpl().Implementation().(*metrictest.Async)
|
2020-05-14 01:27:52 +02:00
|
|
|
|
|
|
|
require.NotNil(t, impl1)
|
|
|
|
require.NotNil(t, impl2)
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
got := provider.MeasurementBatches[0]
|
2020-05-14 01:27:52 +02:00
|
|
|
require.Equal(t, labels, got.Labels)
|
|
|
|
require.Len(t, got.Measurements, 2)
|
|
|
|
|
|
|
|
m1 := got.Measurements[0]
|
2021-06-10 21:05:25 +02:00
|
|
|
require.Equal(t, impl1, m1.Instrument.Implementation().(*metrictest.Async))
|
|
|
|
require.Equal(t, 0, m1.Number.CompareNumber(number.Int64Kind, metrictest.ResolveNumberByKind(t, number.Int64Kind, 42)))
|
2020-05-14 01:27:52 +02:00
|
|
|
|
|
|
|
m2 := got.Measurements[1]
|
2021-06-10 21:05:25 +02:00
|
|
|
require.Equal(t, impl2, m2.Instrument.Implementation().(*metrictest.Async))
|
|
|
|
require.Equal(t, 0, m2.Number.CompareNumber(number.Float64Kind, metrictest.ResolveNumberByKind(t, number.Float64Kind, 42)))
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func checkObserverBatch(t *testing.T, labels []attribute.KeyValue, provider *metrictest.MeterProvider, nkind number.Kind, mkind sdkapi.InstrumentKind, observer sdkapi.AsyncImpl, expected float64) {
|
2020-03-05 22:15:30 +02:00
|
|
|
t.Helper()
|
2021-09-27 17:51:47 +02:00
|
|
|
assert.Len(t, provider.MeasurementBatches, 1)
|
|
|
|
if len(provider.MeasurementBatches) < 1 {
|
2020-03-05 22:15:30 +02:00
|
|
|
return
|
|
|
|
}
|
2021-06-10 21:05:25 +02:00
|
|
|
o := observer.Implementation().(*metrictest.Async)
|
2020-03-05 22:15:30 +02:00
|
|
|
if !assert.NotNil(t, o) {
|
|
|
|
return
|
|
|
|
}
|
2021-09-27 17:51:47 +02:00
|
|
|
got := provider.MeasurementBatches[0]
|
2020-03-27 23:06:48 +02:00
|
|
|
assert.Equal(t, labels, got.Labels)
|
2020-03-05 22:15:30 +02:00
|
|
|
assert.Len(t, got.Measurements, 1)
|
|
|
|
if len(got.Measurements) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
measurement := got.Measurements[0]
|
2020-10-13 16:55:31 +02:00
|
|
|
require.Equal(t, mkind, measurement.Instrument.Descriptor().InstrumentKind())
|
2021-06-10 21:05:25 +02:00
|
|
|
assert.Equal(t, o, measurement.Instrument.Implementation().(*metrictest.Async))
|
|
|
|
ft := metrictest.ResolveNumberByKind(t, nkind, expected)
|
2020-05-19 20:49:24 +02:00
|
|
|
assert.Equal(t, 0, measurement.Number.CompareNumber(nkind, ft))
|
2020-03-05 22:15:30 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
type testWrappedMeter struct {
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
var _ sdkapi.MeterImpl = testWrappedMeter{}
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (testWrappedMeter) RecordBatch(context.Context, []attribute.KeyValue, ...sdkapi.Measurement) {
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (testWrappedMeter) NewSyncInstrument(_ sdkapi.Descriptor) (sdkapi.SyncImpl, error) {
|
2020-03-19 21:02:46 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (testWrappedMeter) NewAsyncInstrument(_ sdkapi.Descriptor, _ sdkapi.AsyncRunner) (sdkapi.AsyncImpl, error) {
|
2020-03-19 21:02:46 +02:00
|
|
|
return nil, errors.New("Test wrap error")
|
|
|
|
}
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
func TestWrappedInstrumentError(t *testing.T) {
|
|
|
|
impl := &testWrappedMeter{}
|
2021-09-27 17:51:47 +02:00
|
|
|
meter := metric.WrapMeterImpl(impl)
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2021-09-01 22:38:37 +02:00
|
|
|
histogram, err := meter.NewInt64Histogram("test.histogram")
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
require.Equal(t, err, metric.ErrSDKReturnedNilImpl)
|
2021-09-01 22:38:37 +02:00
|
|
|
require.NotNil(t, histogram.SyncImpl())
|
2020-03-11 20:57:57 +02:00
|
|
|
|
2021-09-01 22:38:37 +02:00
|
|
|
observer, err := meter.NewInt64GaugeObserver("test.observer", func(_ context.Context, result metric.Int64ObserverResult) {})
|
2020-03-11 20:57:57 +02:00
|
|
|
|
|
|
|
require.NotNil(t, err)
|
2020-03-19 21:02:46 +02:00
|
|
|
require.NotNil(t, observer.AsyncImpl())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNilCallbackObserverNoop(t *testing.T) {
|
|
|
|
// Tests that a nil callback yields a no-op observer without error.
|
2021-09-27 17:51:47 +02:00
|
|
|
_, meter := testPair()
|
2020-03-19 21:02:46 +02:00
|
|
|
|
2021-09-01 22:38:37 +02:00
|
|
|
observer := Must(meter).NewInt64GaugeObserver("test.observer", nil)
|
2020-03-19 21:02:46 +02:00
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
impl := observer.AsyncImpl().Implementation()
|
|
|
|
desc := observer.AsyncImpl().Descriptor()
|
|
|
|
require.Equal(t, nil, impl)
|
|
|
|
require.Equal(t, "", desc.Name())
|
2020-03-11 20:57:57 +02:00
|
|
|
}
|