Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions metric/asyncfloat64.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Float64ObservableCounter interface {
type Float64ObservableCounterConfig struct {
description string
unit string
optIn bool
callbacks []Float64Callback
}

Expand All @@ -66,6 +67,11 @@ func (c Float64ObservableCounterConfig) Unit() string {
return c.unit
}

// OptIn returns true if the instrument is disabled by default.
func (c Float64ObservableCounterConfig) OptIn() bool {
return c.optIn
}

// Callbacks returns the configured callbacks.
func (c Float64ObservableCounterConfig) Callbacks() []Float64Callback {
return c.callbacks
Expand Down Expand Up @@ -101,6 +107,7 @@ type Float64ObservableUpDownCounter interface {
type Float64ObservableUpDownCounterConfig struct {
description string
unit string
optIn bool
callbacks []Float64Callback
}

Expand All @@ -126,6 +133,11 @@ func (c Float64ObservableUpDownCounterConfig) Unit() string {
return c.unit
}

// OptIn returns true if the instrument is disabled by default.
func (c Float64ObservableUpDownCounterConfig) OptIn() bool {
return c.optIn
}

// Callbacks returns the configured callbacks.
func (c Float64ObservableUpDownCounterConfig) Callbacks() []Float64Callback {
return c.callbacks
Expand Down Expand Up @@ -160,6 +172,7 @@ type Float64ObservableGauge interface {
type Float64ObservableGaugeConfig struct {
description string
unit string
optIn bool
callbacks []Float64Callback
}

Expand All @@ -183,6 +196,11 @@ func (c Float64ObservableGaugeConfig) Unit() string {
return c.unit
}

// OptIn returns true if the instrument is disabled by default.
func (c Float64ObservableGaugeConfig) OptIn() bool {
return c.optIn
}

// Callbacks returns the configured callbacks.
func (c Float64ObservableGaugeConfig) Callbacks() []Float64Callback {
return c.callbacks
Expand Down
6 changes: 6 additions & 0 deletions metric/asyncfloat64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ func TestFloat64ObservableConfiguration(t *testing.T) {
token float64 = 43
desc = "Instrument description."
uBytes = "By"
optIn = true
)

run := func(got float64ObservableConfig) func(*testing.T) {
return func(t *testing.T) {
assert.Equal(t, desc, got.Description(), "description")
assert.Equal(t, uBytes, got.Unit(), "unit")
assert.Equal(t, optIn, got.OptIn(), "optIn")

// Functions are not comparable.
cBacks := got.Callbacks()
Expand All @@ -44,6 +46,7 @@ func TestFloat64ObservableConfiguration(t *testing.T) {
NewFloat64ObservableCounterConfig(
WithDescription(desc),
WithUnit(uBytes),
WithOptIn(),
WithFloat64Callback(cback),
),
))
Expand All @@ -52,6 +55,7 @@ func TestFloat64ObservableConfiguration(t *testing.T) {
NewFloat64ObservableUpDownCounterConfig(
WithDescription(desc),
WithUnit(uBytes),
WithOptIn(),
WithFloat64Callback(cback),
),
))
Expand All @@ -60,6 +64,7 @@ func TestFloat64ObservableConfiguration(t *testing.T) {
NewFloat64ObservableGaugeConfig(
WithDescription(desc),
WithUnit(uBytes),
WithOptIn(),
WithFloat64Callback(cback),
),
))
Expand All @@ -68,6 +73,7 @@ func TestFloat64ObservableConfiguration(t *testing.T) {
type float64ObservableConfig interface {
Description() string
Unit() string
OptIn() bool
Callbacks() []Float64Callback
}

Expand Down
18 changes: 18 additions & 0 deletions metric/asyncint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Int64ObservableCounter interface {
type Int64ObservableCounterConfig struct {
description string
unit string
optIn bool
callbacks []Int64Callback
}

Expand All @@ -65,6 +66,11 @@ func (c Int64ObservableCounterConfig) Unit() string {
return c.unit
}

// OptIn returns true if the instrument is disabled by default.
func (c Int64ObservableCounterConfig) OptIn() bool {
return c.optIn
}

// Callbacks returns the configured callbacks.
func (c Int64ObservableCounterConfig) Callbacks() []Int64Callback {
return c.callbacks
Expand Down Expand Up @@ -100,6 +106,7 @@ type Int64ObservableUpDownCounter interface {
type Int64ObservableUpDownCounterConfig struct {
description string
unit string
optIn bool
callbacks []Int64Callback
}

Expand All @@ -125,6 +132,11 @@ func (c Int64ObservableUpDownCounterConfig) Unit() string {
return c.unit
}

// OptIn returns true if the instrument is disabled by default.
func (c Int64ObservableUpDownCounterConfig) OptIn() bool {
return c.optIn
}

// Callbacks returns the configured callbacks.
func (c Int64ObservableUpDownCounterConfig) Callbacks() []Int64Callback {
return c.callbacks
Expand Down Expand Up @@ -159,6 +171,7 @@ type Int64ObservableGauge interface {
type Int64ObservableGaugeConfig struct {
description string
unit string
optIn bool
callbacks []Int64Callback
}

Expand All @@ -182,6 +195,11 @@ func (c Int64ObservableGaugeConfig) Unit() string {
return c.unit
}

// OptIn returns true if the instrument is disabled by default.
func (c Int64ObservableGaugeConfig) OptIn() bool {
return c.optIn
}

// Callbacks returns the configured callbacks.
func (c Int64ObservableGaugeConfig) Callbacks() []Int64Callback {
return c.callbacks
Expand Down
6 changes: 6 additions & 0 deletions metric/asyncint64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ func TestInt64ObservableConfiguration(t *testing.T) {
token int64 = 43
desc = "Instrument description."
uBytes = "By"
optIn = true
)

run := func(got int64ObservableConfig) func(*testing.T) {
return func(t *testing.T) {
assert.Equal(t, desc, got.Description(), "description")
assert.Equal(t, uBytes, got.Unit(), "unit")
assert.Equal(t, optIn, got.OptIn(), "optIn")

// Functions are not comparable.
cBacks := got.Callbacks()
Expand All @@ -44,6 +46,7 @@ func TestInt64ObservableConfiguration(t *testing.T) {
NewInt64ObservableCounterConfig(
WithDescription(desc),
WithUnit(uBytes),
WithOptIn(),
WithInt64Callback(cback),
),
))
Expand All @@ -52,6 +55,7 @@ func TestInt64ObservableConfiguration(t *testing.T) {
NewInt64ObservableUpDownCounterConfig(
WithDescription(desc),
WithUnit(uBytes),
WithOptIn(),
WithInt64Callback(cback),
),
))
Expand All @@ -60,6 +64,7 @@ func TestInt64ObservableConfiguration(t *testing.T) {
NewInt64ObservableGaugeConfig(
WithDescription(desc),
WithUnit(uBytes),
WithOptIn(),
WithInt64Callback(cback),
),
))
Expand All @@ -68,6 +73,7 @@ func TestInt64ObservableConfiguration(t *testing.T) {
type int64ObservableConfig interface {
Description() string
Unit() string
OptIn() bool
Callbacks() []Int64Callback
}

Expand Down
20 changes: 20 additions & 0 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,23 @@ func ExampleMeter_attributes() {
metric.WithAttributes(semconv.HTTPResponseStatusCode(statusCode)))
})
}

// You can define metrics which are disabled by default using the [WithOptIn] option.
//
// Here's how you might define a counter that is disabled by default.
func ExampleMeter_withOptIn() {
var err error
itemsCounter, err := meter.Int64UpDownCounter(
"items.counter",
metric.WithDescription("Number of items."),
metric.WithUnit("{item}"),
metric.WithOptIn(),
)
if err != nil {
panic(err)
}

// This increment is a no-op unless the user has explicitly enabled the
// metric using the SDK.
itemsCounter.Add(context.Background(), 1)
}
79 changes: 79 additions & 0 deletions metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,85 @@
// The unit u should be defined using the appropriate [UCUM](https://ucum.org) case-sensitive code.
func WithUnit(u string) InstrumentOption { return unitOpt(u) }

type optInOpt struct{}

func (o optInOpt) applyFloat64Counter(c Float64CounterConfig) Float64CounterConfig {

Check failure on line 201 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 201 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyFloat64UpDownCounter(c Float64UpDownCounterConfig) Float64UpDownCounterConfig {

Check failure on line 206 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 206 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyFloat64Histogram(c Float64HistogramConfig) Float64HistogramConfig {

Check failure on line 211 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 211 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyFloat64Gauge(c Float64GaugeConfig) Float64GaugeConfig {

Check failure on line 216 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 216 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyFloat64ObservableCounter(c Float64ObservableCounterConfig) Float64ObservableCounterConfig {

Check failure on line 221 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 221 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyFloat64ObservableUpDownCounter(

Check failure on line 226 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 226 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c Float64ObservableUpDownCounterConfig,
) Float64ObservableUpDownCounterConfig {
c.optIn = true
return c
}

func (o optInOpt) applyFloat64ObservableGauge(c Float64ObservableGaugeConfig) Float64ObservableGaugeConfig {

Check failure on line 233 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 233 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyInt64Counter(c Int64CounterConfig) Int64CounterConfig {

Check failure on line 238 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 238 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyInt64UpDownCounter(c Int64UpDownCounterConfig) Int64UpDownCounterConfig {

Check failure on line 243 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 243 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyInt64Histogram(c Int64HistogramConfig) Int64HistogramConfig {

Check failure on line 248 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check failure on line 248 in metric/instrument.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
c.optIn = true
return c
}

func (o optInOpt) applyInt64Gauge(c Int64GaugeConfig) Int64GaugeConfig {
c.optIn = true
return c
}

func (o optInOpt) applyInt64ObservableCounter(c Int64ObservableCounterConfig) Int64ObservableCounterConfig {
c.optIn = true
return c
}

func (o optInOpt) applyInt64ObservableUpDownCounter(
c Int64ObservableUpDownCounterConfig,
) Int64ObservableUpDownCounterConfig {
c.optIn = true
return c
}

func (o optInOpt) applyInt64ObservableGauge(c Int64ObservableGaugeConfig) Int64ObservableGaugeConfig {
c.optIn = true
return c
}

// WithOptIn sets the instrument to be disabled by default.
func WithOptIn() InstrumentOption { return optInOpt{} }

// WithExplicitBucketBoundaries sets the instrument explicit bucket boundaries.
//
// This option is considered "advisory", and may be ignored by API implementations.
Expand Down
Loading
Loading