2019-10-15 18:28:36 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
package metric_test
|
2019-10-15 18:28:36 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
"go.opentelemetry.io/otel/api/key"
|
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
|
|
"go.opentelemetry.io/otel/api/unit"
|
|
|
|
mock "go.opentelemetry.io/otel/internal/metric"
|
2019-10-23 08:29:24 +02:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-10-15 18:28:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCounterOptions(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
2019-10-30 21:59:34 +02:00
|
|
|
opts []metric.CounterOptionApplier
|
2019-10-15 18:28:36 +02:00
|
|
|
keys []core.Key
|
|
|
|
desc string
|
|
|
|
unit unit.Unit
|
|
|
|
alt bool
|
|
|
|
}
|
|
|
|
testcases := []testcase{
|
|
|
|
{
|
|
|
|
name: "no opts",
|
|
|
|
opts: nil,
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "keys keys keys",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.CounterOptionApplier{
|
|
|
|
metric.WithKeys(key.New("foo"), key.New("foo2")),
|
|
|
|
metric.WithKeys(key.New("bar"), key.New("bar2")),
|
|
|
|
metric.WithKeys(key.New("baz"), key.New("baz2")),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: []core.Key{
|
2019-10-17 07:49:58 +02:00
|
|
|
key.New("foo"), key.New("foo2"),
|
|
|
|
key.New("bar"), key.New("bar2"),
|
|
|
|
key.New("baz"), key.New("baz2"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.CounterOptionApplier{
|
|
|
|
metric.WithDescription("stuff"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "stuff",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description override",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.CounterOptionApplier{
|
|
|
|
metric.WithDescription("stuff"),
|
|
|
|
metric.WithDescription("things"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "things",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.CounterOptionApplier{
|
|
|
|
metric.WithUnit("s"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "s",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit override",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.CounterOptionApplier{
|
|
|
|
metric.WithUnit("s"),
|
|
|
|
metric.WithUnit("h"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "h",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nonmonotonic",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.CounterOptionApplier{
|
|
|
|
metric.WithMonotonic(false),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nonmonotonic, but not really",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.CounterOptionApplier{
|
|
|
|
metric.WithMonotonic(false),
|
|
|
|
metric.WithMonotonic(true),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for idx, tt := range testcases {
|
|
|
|
t.Logf("Testing counter case %s (%d)", tt.name, idx)
|
2019-10-30 21:59:34 +02:00
|
|
|
opts := &metric.Options{}
|
|
|
|
metric.ApplyCounterOptions(opts, tt.opts...)
|
|
|
|
checkOptions(t, opts, &metric.Options{
|
2019-10-23 08:29:24 +02:00
|
|
|
Description: tt.desc,
|
|
|
|
Unit: tt.unit,
|
|
|
|
Keys: tt.keys,
|
|
|
|
Alternate: tt.alt,
|
|
|
|
})
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGaugeOptions(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
2019-10-30 21:59:34 +02:00
|
|
|
opts []metric.GaugeOptionApplier
|
2019-10-15 18:28:36 +02:00
|
|
|
keys []core.Key
|
|
|
|
desc string
|
|
|
|
unit unit.Unit
|
|
|
|
alt bool
|
|
|
|
}
|
|
|
|
testcases := []testcase{
|
|
|
|
{
|
|
|
|
name: "no opts",
|
|
|
|
opts: nil,
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "keys keys keys",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.GaugeOptionApplier{
|
|
|
|
metric.WithKeys(key.New("foo"), key.New("foo2")),
|
|
|
|
metric.WithKeys(key.New("bar"), key.New("bar2")),
|
|
|
|
metric.WithKeys(key.New("baz"), key.New("baz2")),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: []core.Key{
|
2019-10-17 07:49:58 +02:00
|
|
|
key.New("foo"), key.New("foo2"),
|
|
|
|
key.New("bar"), key.New("bar2"),
|
|
|
|
key.New("baz"), key.New("baz2"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.GaugeOptionApplier{
|
|
|
|
metric.WithDescription("stuff"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "stuff",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description override",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.GaugeOptionApplier{
|
|
|
|
metric.WithDescription("stuff"),
|
|
|
|
metric.WithDescription("things"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "things",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.GaugeOptionApplier{
|
|
|
|
metric.WithUnit("s"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "s",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit override",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.GaugeOptionApplier{
|
|
|
|
metric.WithUnit("s"),
|
|
|
|
metric.WithUnit("h"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "h",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "monotonic",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.GaugeOptionApplier{
|
|
|
|
metric.WithMonotonic(true),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "monotonic, but not really",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.GaugeOptionApplier{
|
|
|
|
metric.WithMonotonic(true),
|
|
|
|
metric.WithMonotonic(false),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for idx, tt := range testcases {
|
|
|
|
t.Logf("Testing gauge case %s (%d)", tt.name, idx)
|
2019-10-30 21:59:34 +02:00
|
|
|
opts := &metric.Options{}
|
|
|
|
metric.ApplyGaugeOptions(opts, tt.opts...)
|
|
|
|
checkOptions(t, opts, &metric.Options{
|
2019-10-23 08:29:24 +02:00
|
|
|
Description: tt.desc,
|
|
|
|
Unit: tt.unit,
|
|
|
|
Keys: tt.keys,
|
|
|
|
Alternate: tt.alt,
|
|
|
|
})
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeasureOptions(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
2019-10-30 21:59:34 +02:00
|
|
|
opts []metric.MeasureOptionApplier
|
2019-10-15 18:28:36 +02:00
|
|
|
keys []core.Key
|
|
|
|
desc string
|
|
|
|
unit unit.Unit
|
|
|
|
alt bool
|
|
|
|
}
|
|
|
|
testcases := []testcase{
|
|
|
|
{
|
|
|
|
name: "no opts",
|
|
|
|
opts: nil,
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "keys keys keys",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.MeasureOptionApplier{
|
|
|
|
metric.WithKeys(key.New("foo"), key.New("foo2")),
|
|
|
|
metric.WithKeys(key.New("bar"), key.New("bar2")),
|
|
|
|
metric.WithKeys(key.New("baz"), key.New("baz2")),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: []core.Key{
|
2019-10-17 07:49:58 +02:00
|
|
|
key.New("foo"), key.New("foo2"),
|
|
|
|
key.New("bar"), key.New("bar2"),
|
|
|
|
key.New("baz"), key.New("baz2"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.MeasureOptionApplier{
|
|
|
|
metric.WithDescription("stuff"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "stuff",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description override",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.MeasureOptionApplier{
|
|
|
|
metric.WithDescription("stuff"),
|
|
|
|
metric.WithDescription("things"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "things",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.MeasureOptionApplier{
|
|
|
|
metric.WithUnit("s"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "s",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unit override",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.MeasureOptionApplier{
|
|
|
|
metric.WithUnit("s"),
|
|
|
|
metric.WithUnit("h"),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "h",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
{
|
2019-10-23 08:29:24 +02:00
|
|
|
name: "not absolute",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.MeasureOptionApplier{
|
|
|
|
metric.WithAbsolute(false),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: true,
|
|
|
|
},
|
|
|
|
{
|
2019-10-23 08:29:24 +02:00
|
|
|
name: "not absolute, but not really",
|
2019-10-30 21:59:34 +02:00
|
|
|
opts: []metric.MeasureOptionApplier{
|
|
|
|
metric.WithAbsolute(false),
|
|
|
|
metric.WithAbsolute(true),
|
2019-10-15 18:28:36 +02:00
|
|
|
},
|
|
|
|
keys: nil,
|
|
|
|
desc: "",
|
|
|
|
unit: "",
|
|
|
|
alt: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for idx, tt := range testcases {
|
|
|
|
t.Logf("Testing measure case %s (%d)", tt.name, idx)
|
2019-10-30 21:59:34 +02:00
|
|
|
opts := &metric.Options{}
|
|
|
|
metric.ApplyMeasureOptions(opts, tt.opts...)
|
|
|
|
checkOptions(t, opts, &metric.Options{
|
2019-10-23 08:29:24 +02:00
|
|
|
Description: tt.desc,
|
|
|
|
Unit: tt.unit,
|
|
|
|
Keys: tt.keys,
|
|
|
|
Alternate: tt.alt,
|
|
|
|
})
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
func checkOptions(t *testing.T, got *metric.Options, expected *metric.Options) {
|
2019-10-23 08:29:24 +02:00
|
|
|
if diff := cmp.Diff(got, expected); diff != "" {
|
|
|
|
t.Errorf("Compare options: -got +want %s", diff)
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCounter(t *testing.T) {
|
|
|
|
{
|
2019-10-30 21:59:34 +02:00
|
|
|
meter := mock.NewMeter()
|
2020-03-03 00:50:53 +02:00
|
|
|
c := meter.NewFloat64Counter("test.counter.float")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2019-10-29 22:27:22 +02:00
|
|
|
labels := meter.Labels()
|
2019-10-15 18:28:36 +02:00
|
|
|
c.Add(ctx, 42, labels)
|
2019-12-28 02:30:19 +02:00
|
|
|
boundInstrument := c.Bind(labels)
|
|
|
|
boundInstrument.Add(ctx, 42)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, c.Measurement(42))
|
|
|
|
t.Log("Testing float counter")
|
2019-10-30 21:59:34 +02:00
|
|
|
checkBatches(t, ctx, labels, meter, core.Float64NumberKind, c.Impl())
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
{
|
2019-10-30 21:59:34 +02:00
|
|
|
meter := mock.NewMeter()
|
2020-03-03 00:50:53 +02:00
|
|
|
c := meter.NewInt64Counter("test.counter.int")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2019-10-29 22:27:22 +02:00
|
|
|
labels := meter.Labels()
|
2019-10-15 18:28:36 +02:00
|
|
|
c.Add(ctx, 42, labels)
|
2019-12-28 02:30:19 +02:00
|
|
|
boundInstrument := c.Bind(labels)
|
|
|
|
boundInstrument.Add(ctx, 42)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, c.Measurement(42))
|
|
|
|
t.Log("Testing int counter")
|
2019-10-30 21:59:34 +02:00
|
|
|
checkBatches(t, ctx, labels, meter, core.Int64NumberKind, c.Impl())
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGauge(t *testing.T) {
|
|
|
|
{
|
2019-10-30 21:59:34 +02:00
|
|
|
meter := mock.NewMeter()
|
2020-03-03 00:50:53 +02:00
|
|
|
g := meter.NewFloat64Gauge("test.gauge.float")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2019-10-29 22:27:22 +02:00
|
|
|
labels := meter.Labels()
|
2019-10-15 18:28:36 +02:00
|
|
|
g.Set(ctx, 42, labels)
|
2019-12-28 02:30:19 +02:00
|
|
|
boundInstrument := g.Bind(labels)
|
|
|
|
boundInstrument.Set(ctx, 42)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, g.Measurement(42))
|
|
|
|
t.Log("Testing float gauge")
|
2019-10-30 21:59:34 +02:00
|
|
|
checkBatches(t, ctx, labels, meter, core.Float64NumberKind, g.Impl())
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
{
|
2019-10-30 21:59:34 +02:00
|
|
|
meter := mock.NewMeter()
|
2020-03-03 00:50:53 +02:00
|
|
|
g := meter.NewInt64Gauge("test.gauge.int")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2019-10-29 22:27:22 +02:00
|
|
|
labels := meter.Labels()
|
2019-10-15 18:28:36 +02:00
|
|
|
g.Set(ctx, 42, labels)
|
2019-12-28 02:30:19 +02:00
|
|
|
boundInstrument := g.Bind(labels)
|
|
|
|
boundInstrument.Set(ctx, 42)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, g.Measurement(42))
|
|
|
|
t.Log("Testing int gauge")
|
2019-10-30 21:59:34 +02:00
|
|
|
checkBatches(t, ctx, labels, meter, core.Int64NumberKind, g.Impl())
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMeasure(t *testing.T) {
|
|
|
|
{
|
2019-10-30 21:59:34 +02:00
|
|
|
meter := mock.NewMeter()
|
2020-03-03 00:50:53 +02:00
|
|
|
m := meter.NewFloat64Measure("test.measure.float")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2019-10-29 22:27:22 +02:00
|
|
|
labels := meter.Labels()
|
2019-10-15 18:28:36 +02:00
|
|
|
m.Record(ctx, 42, labels)
|
2019-12-28 02:30:19 +02:00
|
|
|
boundInstrument := m.Bind(labels)
|
|
|
|
boundInstrument.Record(ctx, 42)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, m.Measurement(42))
|
|
|
|
t.Log("Testing float measure")
|
2019-10-30 21:59:34 +02:00
|
|
|
checkBatches(t, ctx, labels, meter, core.Float64NumberKind, m.Impl())
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
{
|
2019-10-30 21:59:34 +02:00
|
|
|
meter := mock.NewMeter()
|
2020-03-03 00:50:53 +02:00
|
|
|
m := meter.NewInt64Measure("test.measure.int")
|
2019-10-15 18:28:36 +02:00
|
|
|
ctx := context.Background()
|
2019-10-29 22:27:22 +02:00
|
|
|
labels := meter.Labels()
|
2019-10-15 18:28:36 +02:00
|
|
|
m.Record(ctx, 42, labels)
|
2019-12-28 02:30:19 +02:00
|
|
|
boundInstrument := m.Bind(labels)
|
|
|
|
boundInstrument.Record(ctx, 42)
|
2019-10-15 18:28:36 +02:00
|
|
|
meter.RecordBatch(ctx, labels, m.Measurement(42))
|
|
|
|
t.Log("Testing int measure")
|
2019-10-30 21:59:34 +02:00
|
|
|
checkBatches(t, ctx, labels, meter, core.Int64NumberKind, m.Impl())
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 21:59:34 +02:00
|
|
|
func checkBatches(t *testing.T, ctx context.Context, labels metric.LabelSet, meter *mock.Meter, kind core.NumberKind, instrument metric.InstrumentImpl) {
|
2019-10-16 19:24:38 +02:00
|
|
|
t.Helper()
|
2019-10-30 21:59:34 +02:00
|
|
|
if len(meter.MeasurementBatches) != 3 {
|
|
|
|
t.Errorf("Expected 3 recorded measurement batches, got %d", len(meter.MeasurementBatches))
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
ourInstrument := instrument.(*mock.Instrument)
|
|
|
|
ourLabelSet := labels.(*mock.LabelSet)
|
2019-10-15 18:28:36 +02:00
|
|
|
minLen := 3
|
2019-10-30 21:59:34 +02:00
|
|
|
if minLen > len(meter.MeasurementBatches) {
|
|
|
|
minLen = len(meter.MeasurementBatches)
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
for i := 0; i < minLen; i++ {
|
2019-10-30 21:59:34 +02:00
|
|
|
got := meter.MeasurementBatches[i]
|
|
|
|
if got.Ctx != ctx {
|
2019-10-15 18:28:36 +02:00
|
|
|
d := func(c context.Context) string {
|
|
|
|
return fmt.Sprintf("(ptr: %p, ctx %#v)", c, c)
|
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
t.Errorf("Wrong recorded context in batch %d, expected %s, got %s", i, d(ctx), d(got.Ctx))
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
if got.LabelSet != ourLabelSet {
|
|
|
|
d := func(l *mock.LabelSet) string {
|
|
|
|
return fmt.Sprintf("(ptr: %p, labels %#v)", l, l.Labels)
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
t.Errorf("Wrong recorded label set in batch %d, expected %s, got %s", i, d(ourLabelSet), d(got.LabelSet))
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
if len(got.Measurements) != 1 {
|
|
|
|
t.Errorf("Expected 1 measurement in batch %d, got %d", i, len(got.Measurements))
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
minMLen := 1
|
2019-10-30 21:59:34 +02:00
|
|
|
if minMLen > len(got.Measurements) {
|
|
|
|
minMLen = len(got.Measurements)
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
for j := 0; j < minMLen; j++ {
|
2019-10-30 21:59:34 +02:00
|
|
|
measurement := got.Measurements[j]
|
|
|
|
if measurement.Instrument != ourInstrument {
|
|
|
|
d := func(i *mock.Instrument) string {
|
2019-10-23 08:29:24 +02:00
|
|
|
return fmt.Sprintf("(ptr: %p, instrument %#v)", i, i)
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
2019-10-30 21:59:34 +02:00
|
|
|
t.Errorf("Wrong recorded instrument in measurement %d in batch %d, expected %s, got %s", j, i, d(ourInstrument), d(measurement.Instrument))
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
2019-10-23 08:29:24 +02:00
|
|
|
ft := fortyTwo(t, kind)
|
2019-10-30 21:59:34 +02:00
|
|
|
if measurement.Number.CompareNumber(kind, ft) != 0 {
|
|
|
|
t.Errorf("Wrong recorded value in measurement %d in batch %d, expected %s, got %s", j, i, ft.Emit(kind), measurement.Number.Emit(kind))
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
func fortyTwo(t *testing.T, kind core.NumberKind) core.Number {
|
2019-10-15 18:28:36 +02:00
|
|
|
switch kind {
|
2019-10-29 22:27:22 +02:00
|
|
|
case core.Int64NumberKind:
|
|
|
|
return core.NewInt64Number(42)
|
|
|
|
case core.Float64NumberKind:
|
|
|
|
return core.NewFloat64Number(42)
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
t.Errorf("Invalid value kind %q", kind)
|
2019-10-29 22:27:22 +02:00
|
|
|
return core.NewInt64Number(0)
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|