Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krnowak committed Feb 24, 2020
1 parent 7fec05b commit 4668a7b
Show file tree
Hide file tree
Showing 5 changed files with 318 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/global/internal/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ func (m *meter) RegisterInt64Observer(name string, callback metric.Int64Observer
name: name,
nkind: core.Int64NumberKind,
opts: oos,
meter: m,
callback: callback,
}
m.addObserver(obs)
Expand All @@ -478,6 +479,7 @@ func (m *meter) RegisterFloat64Observer(name string, callback metric.Float64Obse
name: name,
nkind: core.Float64NumberKind,
opts: oos,
meter: m,
callback: callback,
}
m.addObserver(obs)
Expand Down
59 changes: 56 additions & 3 deletions api/global/internal/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/global/internal"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/exporter/metric/stdout"
metrictest "go.opentelemetry.io/otel/internal/metric"
)
Expand Down Expand Up @@ -41,6 +42,16 @@ func TestDirect(t *testing.T) {
measure.Record(ctx, 1, labels1)
measure.Record(ctx, 2, labels1)

_ = meter1.RegisterFloat64Observer("test.observer.float", func(result metric.Float64ObserverResult) {
result.Observe(1, labels1)
result.Observe(2, labels2)
})

_ = meter1.RegisterInt64Observer("test.observer.int", func(result metric.Int64ObserverResult) {
result.Observe(1, labels1)
result.Observe(2, labels2)
})

second := meter2.NewFloat64Measure("test.second")
second.Record(ctx, 1, labels3)
second.Record(ctx, 2, labels3)
Expand All @@ -54,7 +65,8 @@ func TestDirect(t *testing.T) {
second.Record(ctx, 3, labels3)

mock := sdk.Meter("test1").(*metrictest.Meter)
require.Equal(t, 3, len(mock.MeasurementBatches))
mock.RunObservers()
require.Len(t, mock.MeasurementBatches, 7)

require.Equal(t, map[core.Key]core.Value{
lvals1.Key: lvals1.Value,
Expand All @@ -68,7 +80,7 @@ func TestDirect(t *testing.T) {
require.Equal(t, map[core.Key]core.Value{
lvals2.Key: lvals2.Value,
}, mock.MeasurementBatches[1].LabelSet.Labels)
require.Equal(t, 1, len(mock.MeasurementBatches[1].Measurements))
require.Len(t, mock.MeasurementBatches[1].Measurements, 1)
require.Equal(t, core.NewInt64Number(3),
mock.MeasurementBatches[1].Measurements[0].Number)
require.Equal(t, "test.gauge",
Expand All @@ -77,12 +89,48 @@ func TestDirect(t *testing.T) {
require.Equal(t, map[core.Key]core.Value{
lvals1.Key: lvals1.Value,
}, mock.MeasurementBatches[2].LabelSet.Labels)
require.Equal(t, 1, len(mock.MeasurementBatches[2].Measurements))
require.Len(t, mock.MeasurementBatches[2].Measurements, 1)
require.Equal(t, core.NewFloat64Number(3),
mock.MeasurementBatches[2].Measurements[0].Number)
require.Equal(t, "test.measure",
mock.MeasurementBatches[2].Measurements[0].Instrument.Name)

require.Equal(t, map[core.Key]core.Value{
lvals1.Key: lvals1.Value,
}, mock.MeasurementBatches[3].LabelSet.Labels)
require.Len(t, mock.MeasurementBatches[3].Measurements, 1)
require.Equal(t, core.NewFloat64Number(1),
mock.MeasurementBatches[3].Measurements[0].Number)
require.Equal(t, "test.observer.float",
mock.MeasurementBatches[3].Measurements[0].Instrument.Name)

require.Equal(t, map[core.Key]core.Value{
lvals2.Key: lvals2.Value,
}, mock.MeasurementBatches[4].LabelSet.Labels)
require.Len(t, mock.MeasurementBatches[4].Measurements, 1)
require.Equal(t, core.NewFloat64Number(2),
mock.MeasurementBatches[4].Measurements[0].Number)
require.Equal(t, "test.observer.float",
mock.MeasurementBatches[4].Measurements[0].Instrument.Name)

require.Equal(t, map[core.Key]core.Value{
lvals1.Key: lvals1.Value,
}, mock.MeasurementBatches[5].LabelSet.Labels)
require.Len(t, mock.MeasurementBatches[5].Measurements, 1)
require.Equal(t, core.NewInt64Number(1),
mock.MeasurementBatches[5].Measurements[0].Number)
require.Equal(t, "test.observer.int",
mock.MeasurementBatches[5].Measurements[0].Instrument.Name)

require.Equal(t, map[core.Key]core.Value{
lvals2.Key: lvals2.Value,
}, mock.MeasurementBatches[6].LabelSet.Labels)
require.Len(t, mock.MeasurementBatches[6].Measurements, 1)
require.Equal(t, core.NewInt64Number(2),
mock.MeasurementBatches[6].Measurements[0].Number)
require.Equal(t, "test.observer.int",
mock.MeasurementBatches[6].Measurements[0].Instrument.Name)

// This tests the second Meter instance
mock = sdk.Meter("test2").(*metrictest.Meter)
require.Equal(t, 1, len(mock.MeasurementBatches))
Expand Down Expand Up @@ -185,9 +233,14 @@ func TestUnbind(t *testing.T) {
measure := glob.NewInt64Measure("test.measure")
boundM := measure.Bind(labels1)

observerInt := glob.RegisterInt64Observer("test.observer.int", nil)
observerFloat := glob.RegisterFloat64Observer("test.observer.float", nil)

boundC.Unbind()
boundG.Unbind()
boundM.Unbind()
observerInt.Unregister()
observerFloat.Unregister()
}

func TestDefaultSDK(t *testing.T) {
Expand Down
171 changes: 171 additions & 0 deletions api/metric/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
mock "go.opentelemetry.io/otel/internal/metric"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)

func TestCounterOptions(t *testing.T) {
Expand Down Expand Up @@ -361,6 +362,117 @@ func TestMeasureOptions(t *testing.T) {
}
}

func TestObserverOptions(t *testing.T) {
type testcase struct {
name string
opts []metric.ObserverOptionApplier
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",
opts: []metric.ObserverOptionApplier{
metric.WithKeys(key.New("foo"), key.New("foo2")),
metric.WithKeys(key.New("bar"), key.New("bar2")),
metric.WithKeys(key.New("baz"), key.New("baz2")),
},
keys: []core.Key{
key.New("foo"), key.New("foo2"),
key.New("bar"), key.New("bar2"),
key.New("baz"), key.New("baz2"),
},
desc: "",
unit: "",
alt: false,
},
{
name: "description",
opts: []metric.ObserverOptionApplier{
metric.WithDescription("stuff"),
},
keys: nil,
desc: "stuff",
unit: "",
alt: false,
},
{
name: "description override",
opts: []metric.ObserverOptionApplier{
metric.WithDescription("stuff"),
metric.WithDescription("things"),
},
keys: nil,
desc: "things",
unit: "",
alt: false,
},
{
name: "unit",
opts: []metric.ObserverOptionApplier{
metric.WithUnit("s"),
},
keys: nil,
desc: "",
unit: "s",
alt: false,
},
{
name: "unit override",
opts: []metric.ObserverOptionApplier{
metric.WithUnit("s"),
metric.WithUnit("h"),
},
keys: nil,
desc: "",
unit: "h",
alt: false,
},
{
name: "monotonic",
opts: []metric.ObserverOptionApplier{
metric.WithMonotonic(true),
},
keys: nil,
desc: "",
unit: "",
alt: true,
},
{
name: "monotonic, but not really",
opts: []metric.ObserverOptionApplier{
metric.WithMonotonic(true),
metric.WithMonotonic(false),
},
keys: nil,
desc: "",
unit: "",
alt: false,
},
}
for idx, tt := range testcases {
t.Logf("Testing observer case %s (%d)", tt.name, idx)
opts := &metric.Options{}
metric.ApplyObserverOptions(opts, tt.opts...)
checkOptions(t, opts, &metric.Options{
Description: tt.desc,
Unit: tt.unit,
Keys: tt.keys,
Alternate: tt.alt,
})
}
}

func checkOptions(t *testing.T, got *metric.Options, expected *metric.Options) {
if diff := cmp.Diff(got, expected); diff != "" {
t.Errorf("Compare options: -got +want %s", diff)
Expand Down Expand Up @@ -448,6 +560,29 @@ func TestMeasure(t *testing.T) {
}
}

func TestObserver(t *testing.T) {
{
meter := mock.NewMeter()
labels := meter.Labels()
o := meter.RegisterFloat64Observer("test.observer.float", func(result metric.Float64ObserverResult) {
result.Observe(42, labels)
})
t.Log("Testing float observer")
meter.RunObservers()
checkObserverBatch(t, labels, meter, core.Float64NumberKind, o)
}
{
meter := mock.NewMeter()
labels := meter.Labels()
o := meter.RegisterInt64Observer("test.observer.int", func(result metric.Int64ObserverResult) {
result.Observe(42, labels)
})
t.Log("Testing int observer")
meter.RunObservers()
checkObserverBatch(t, labels, meter, core.Int64NumberKind, o)
}
}

func checkBatches(t *testing.T, ctx context.Context, labels metric.LabelSet, meter *mock.Meter, kind core.NumberKind, instrument metric.InstrumentImpl) {
t.Helper()
if len(meter.MeasurementBatches) != 3 {
Expand Down Expand Up @@ -496,7 +631,43 @@ func checkBatches(t *testing.T, ctx context.Context, labels metric.LabelSet, met
}
}

func checkObserverBatch(t *testing.T, labels metric.LabelSet, meter *mock.Meter, kind core.NumberKind, observer interface{}) {
t.Helper()
assert.Len(t, meter.MeasurementBatches, 1)
if len(meter.MeasurementBatches) < 1 {
return
}
o := getMockObserver(t, observer, kind)
if !assert.NotNil(t, o) {
return
}
ourLabelSet := labels.(*mock.LabelSet)
got := meter.MeasurementBatches[0]
assert.Equal(t, ourLabelSet, got.LabelSet)
assert.Len(t, got.Measurements, 1)
if len(got.Measurements) < 1 {
return
}
measurement := got.Measurements[0]
assert.Equal(t, o.Instrument, measurement.Instrument)
ft := fortyTwo(t, kind)
assert.Equal(t, 0, measurement.Number.CompareNumber(kind, ft))
}

func getMockObserver(t *testing.T, observer interface{}, kind core.NumberKind) *mock.Observer {
t.Helper()
switch kind {
case core.Int64NumberKind:
return observer.(mock.Int64Observer).Observer
case core.Float64NumberKind:
return observer.(mock.Float64Observer).Observer
}
t.Errorf("Invalid value kind %q", kind)
return nil
}

func fortyTwo(t *testing.T, kind core.NumberKind) core.Number {
t.Helper()
switch kind {
case core.Int64NumberKind:
return core.NewInt64Number(42)
Expand Down
4 changes: 4 additions & 0 deletions sdk/metric/alignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestMain(m *testing.M) {
Name: "record.modified",
Offset: unsafe.Offsetof(record{}.modified),
},
{
Name: "observer.callback",
Offset: unsafe.Offsetof(observer{}.callback),
},
}
if !ottest.Aligned8Byte(fields, os.Stderr) {
os.Exit(1)
Expand Down
Loading

0 comments on commit 4668a7b

Please sign in to comment.