Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk/metric: Add experimental Enabled method to synchronous instruments #6016

Merged
merged 9 commits into from
Dec 6, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

- Add `Reset` method to `SpanRecorder` in `go.opentelemetry.io/otel/sdk/trace/tracetest`. (#5994)
- Experimental `EnabledInstrument` interface added to the metric SDK.
See [metric documentation](./sdk/metric/internal/x/README.md#instrument-enabled) for more information about this feature and how to use it. (#)
codeboten marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
11 changes: 11 additions & 0 deletions sdk/metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.opentelemetry.io/otel/metric/embedded"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
"go.opentelemetry.io/otel/sdk/metric/internal/x"
)

var zeroScope instrumentation.Scope
Expand Down Expand Up @@ -183,6 +184,7 @@ type int64Inst struct {
embedded.Int64UpDownCounter
embedded.Int64Histogram
embedded.Int64Gauge
x.EnabledInstrument
pellared marked this conversation as resolved.
Show resolved Hide resolved
}

var (
Expand All @@ -202,6 +204,10 @@ func (i *int64Inst) Record(ctx context.Context, val int64, opts ...metric.Record
i.aggregate(ctx, val, c.Attributes())
}

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

func (i *int64Inst) aggregate(ctx context.Context, val int64, s attribute.Set) { // nolint:revive // okay to shadow pkg with method.
for _, in := range i.measures {
in(ctx, val, s)
Expand All @@ -215,6 +221,7 @@ type float64Inst struct {
embedded.Float64UpDownCounter
embedded.Float64Histogram
embedded.Float64Gauge
x.EnabledInstrument
pellared marked this conversation as resolved.
Show resolved Hide resolved
}

var (
Expand All @@ -234,6 +241,10 @@ func (i *float64Inst) Record(ctx context.Context, val float64, opts ...metric.Re
i.aggregate(ctx, val, c.Attributes())
}

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

func (i *float64Inst) aggregate(ctx context.Context, val float64, s attribute.Set) {
for _, in := range i.measures {
in(ctx, val, s)
Expand Down
19 changes: 19 additions & 0 deletions sdk/metric/internal/x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ See the [Compatibility and Stability](#compatibility-and-stability) section for

- [Cardinality Limit](#cardinality-limit)
- [Exemplars](#exemplars)
- [Instrument Enabled](#instrument-enabled)

### Cardinality Limit

Expand Down Expand Up @@ -102,6 +103,24 @@ Revert to the default exemplar filter (`"trace_based"`)
unset OTEL_METRICS_EXEMPLAR_FILTER
```

### Instrument Enabled

To help users avoid performing computationally expensive operations when recording measurements, synchronous instruments provide an `Enabled` method.

#### Examples

The following code shows an example of how to check if an instrument implements the `EnabledInstrument` interface before using the `Enabled` function to avoid doing an expensive computation:

```go
type enabledInstrument interface { Enabled() bool }

ctr, err := m.Int64Counter("expensive-counter")
c, ok := ctr.(enabledInstrument)
if c.Enabled() {
pellared marked this conversation as resolved.
Show resolved Hide resolved
c.Add(expensiveComputation())
}
```

## Compatibility and Stability

Experimental features do not fall within the scope of the OpenTelemetry Go versioning and stability [policy](../../../../VERSIONING.md).
Expand Down
11 changes: 11 additions & 0 deletions sdk/metric/internal/x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ func (f Feature[T]) Enabled() bool {
_, ok := f.Lookup()
return ok
}

// EnabledInstrument provides the interface for synchronous
// instruments to returned whether or not they are enabled.
codeboten marked this conversation as resolved.
Show resolved Hide resolved
type EnabledInstrument interface {
// Enabled returns whether the instrument has any measurements
// associated with it.
codeboten marked this conversation as resolved.
Show resolved Hide resolved
//
// This function can be used in places where measuring an instrument
// would result in computationally expensive operations.
Enabled() bool
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}
113 changes: 113 additions & 0 deletions sdk/metric/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/exemplar"
"go.opentelemetry.io/otel/sdk/metric/internal/x"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -388,6 +389,9 @@ func TestMeterCreatesInstruments(t *testing.T) {
ctr, err := m.Int64Counter("sint")
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
ctr.Add(ctx, 3)
},
want: metricdata.Metrics{
Expand All @@ -407,6 +411,9 @@ func TestMeterCreatesInstruments(t *testing.T) {
ctr, err := m.Int64UpDownCounter("sint")
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
ctr.Add(ctx, 11)
},
want: metricdata.Metrics{
Expand Down Expand Up @@ -452,6 +459,9 @@ func TestMeterCreatesInstruments(t *testing.T) {
ctr, err := m.Float64Counter("sfloat")
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
ctr.Add(ctx, 3)
},
want: metricdata.Metrics{
Expand All @@ -471,6 +481,9 @@ func TestMeterCreatesInstruments(t *testing.T) {
ctr, err := m.Float64UpDownCounter("sfloat")
assert.NoError(t, err)

c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.True(t, c.Enabled())
ctr.Add(ctx, 11)
},
want: metricdata.Metrics{
Expand Down Expand Up @@ -532,6 +545,106 @@ func TestMeterCreatesInstruments(t *testing.T) {
}
}

func TestMeterWithDropView(t *testing.T) {
testCases := []struct {
name string
fn func(*testing.T, metric.Meter)
}{
{
name: "SyncInt64Count",
fn: func(t *testing.T, m metric.Meter) {
ctr, err := m.Int64Counter("sint")
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
codeboten marked this conversation as resolved.
Show resolved Hide resolved
assert.False(t, c.Enabled())
pellared marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
name: "SyncInt64UpDownCount",
fn: func(t *testing.T, m metric.Meter) {
ctr, err := m.Int64UpDownCounter("sint")
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, c.Enabled())
},
},
{
name: "SyncInt64Gauge",
fn: func(t *testing.T, m metric.Meter) {
gauge, err := m.Int64Gauge("sint")
assert.NoError(t, err)
g, ok := gauge.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, g.Enabled())
},
},
{
name: "SyncInt64Histogram",
fn: func(t *testing.T, m metric.Meter) {
histo, err := m.Int64Histogram("histogram")
assert.NoError(t, err)
h, ok := histo.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, h.Enabled())
},
},
{
name: "SyncFloat64Count",
fn: func(t *testing.T, m metric.Meter) {
ctr, err := m.Float64Counter("sfloat")
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, c.Enabled())
},
},
{
name: "SyncFloat64UpDownCount",
fn: func(t *testing.T, m metric.Meter) {
ctr, err := m.Float64UpDownCounter("sfloat")
assert.NoError(t, err)
c, ok := ctr.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, c.Enabled())
},
},
{
name: "SyncFloat64Gauge",
fn: func(t *testing.T, m metric.Meter) {
gauge, err := m.Float64Gauge("sfloat")
assert.NoError(t, err)
g, ok := gauge.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, g.Enabled())
},
},
{
name: "SyncFloat64Histogram",
fn: func(t *testing.T, m metric.Meter) {
histo, err := m.Float64Histogram("histogram")
assert.NoError(t, err)
h, ok := histo.(x.EnabledInstrument)
assert.True(t, ok)
assert.False(t, h.Enabled())
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
rdr := NewManualReader()
dropView := NewView(
Instrument{Name: "*"},
Stream{Aggregation: AggregationDrop{}},
)
m := NewMeterProvider(WithReader(rdr), WithView(dropView)).Meter("testInstruments")
tt.fn(t, m)
})
}
}

func TestMeterCreatesInstrumentsValidations(t *testing.T) {
testCases := []struct {
name string
Expand Down
Loading