Skip to content

Commit

Permalink
update test to use require, add context param, validate interface
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten committed Dec 4, 2024
1 parent 4280aa4 commit 7661cd6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
8 changes: 4 additions & 4 deletions sdk/metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ type int64Inst struct {
embedded.Int64UpDownCounter
embedded.Int64Histogram
embedded.Int64Gauge
x.EnabledInstrument
}

var (
_ metric.Int64Counter = (*int64Inst)(nil)
_ metric.Int64UpDownCounter = (*int64Inst)(nil)
_ metric.Int64Histogram = (*int64Inst)(nil)
_ metric.Int64Gauge = (*int64Inst)(nil)
_ x.EnabledInstrument = (*int64Inst)(nil)
)

func (i *int64Inst) Add(ctx context.Context, val int64, opts ...metric.AddOption) {
Expand All @@ -204,7 +204,7 @@ func (i *int64Inst) Record(ctx context.Context, val int64, opts ...metric.Record
i.aggregate(ctx, val, c.Attributes())
}

func (i *int64Inst) Enabled() bool {
func (i *int64Inst) Enabled(_ context.Context) bool {
return len(i.measures) != 0
}

Expand All @@ -221,14 +221,14 @@ type float64Inst struct {
embedded.Float64UpDownCounter
embedded.Float64Histogram
embedded.Float64Gauge
x.EnabledInstrument
}

var (
_ metric.Float64Counter = (*float64Inst)(nil)
_ metric.Float64UpDownCounter = (*float64Inst)(nil)
_ metric.Float64Histogram = (*float64Inst)(nil)
_ metric.Float64Gauge = (*float64Inst)(nil)
_ x.EnabledInstrument = (*float64Inst)(nil)
)

func (i *float64Inst) Add(ctx context.Context, val float64, opts ...metric.AddOption) {
Expand All @@ -241,7 +241,7 @@ func (i *float64Inst) Record(ctx context.Context, val float64, opts ...metric.Re
i.aggregate(ctx, val, c.Attributes())
}

func (i *float64Inst) Enabled() bool {
func (i *float64Inst) Enabled(_ context.Context) bool {
return len(i.measures) != 0
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/metric/internal/x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package x // import "go.opentelemetry.io/otel/sdk/metric/internal/x"

import (
"context"
"os"
"strconv"
)
Expand Down Expand Up @@ -76,5 +77,5 @@ type EnabledInstrument interface {
//
// This function can be used in places where measuring an instrument
// would result in computationally expensive operations.
Enabled() bool
Enabled(context.Context) bool
}
46 changes: 23 additions & 23 deletions sdk/metric/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
require.True(t, ok)
assert.True(t, c.Enabled(context.Background()))
ctr.Add(ctx, 3)
},
want: metricdata.Metrics{
Expand All @@ -412,8 +412,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
require.True(t, ok)
assert.True(t, c.Enabled(context.Background()))
ctr.Add(ctx, 11)
},
want: metricdata.Metrics{
Expand Down Expand Up @@ -460,8 +460,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
require.True(t, ok)
assert.True(t, c.Enabled(context.Background()))
ctr.Add(ctx, 3)
},
want: metricdata.Metrics{
Expand All @@ -482,8 +482,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
require.True(t, ok)
assert.True(t, c.Enabled(context.Background()))
ctr.Add(ctx, 11)
},
want: metricdata.Metrics{
Expand Down Expand Up @@ -557,7 +557,7 @@ func TestMeterWithDropView(t *testing.T) {
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
require.True(t, ok)
assert.False(t, c.Enabled())
assert.False(t, c.Enabled(context.Background()))
},
},
{
Expand All @@ -566,8 +566,8 @@ func TestMeterWithDropView(t *testing.T) {
ctr, err := m.Int64UpDownCounter("sint")
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, c.Enabled())
require.True(t, ok)
assert.False(t, c.Enabled(context.Background()))
},
},
{
Expand All @@ -576,8 +576,8 @@ func TestMeterWithDropView(t *testing.T) {
gauge, err := m.Int64Gauge("sint")
assert.NoError(t, err)
g, ok := gauge.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, g.Enabled())
require.True(t, ok)
assert.False(t, g.Enabled(context.Background()))
},
},
{
Expand All @@ -586,8 +586,8 @@ func TestMeterWithDropView(t *testing.T) {
histo, err := m.Int64Histogram("histogram")
assert.NoError(t, err)
h, ok := histo.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, h.Enabled())
require.True(t, ok)
assert.False(t, h.Enabled(context.Background()))
},
},
{
Expand All @@ -596,8 +596,8 @@ func TestMeterWithDropView(t *testing.T) {
ctr, err := m.Float64Counter("sfloat")
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, c.Enabled())
require.True(t, ok)
assert.False(t, c.Enabled(context.Background()))
},
},
{
Expand All @@ -606,8 +606,8 @@ func TestMeterWithDropView(t *testing.T) {
ctr, err := m.Float64UpDownCounter("sfloat")
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, c.Enabled())
require.True(t, ok)
assert.False(t, c.Enabled(context.Background()))
},
},
{
Expand All @@ -616,8 +616,8 @@ func TestMeterWithDropView(t *testing.T) {
gauge, err := m.Float64Gauge("sfloat")
assert.NoError(t, err)
g, ok := gauge.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, g.Enabled())
require.True(t, ok)
assert.False(t, g.Enabled(context.Background()))
},
},
{
Expand All @@ -626,8 +626,8 @@ func TestMeterWithDropView(t *testing.T) {
histo, err := m.Float64Histogram("histogram")
assert.NoError(t, err)
h, ok := histo.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, h.Enabled())
require.True(t, ok)
assert.False(t, h.Enabled(context.Background()))
},
},
}
Expand Down

0 comments on commit 7661cd6

Please sign in to comment.