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

Added a Callback option to the api #680

Closed
wants to merge 3 commits into from
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: 6 additions & 12 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/asyncint64"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/unit"
)
Expand Down Expand Up @@ -51,26 +52,19 @@ func ExampleAsyncInstrument() {
meterProvider := metric.NewNoopMeterProvider()
meter := meterProvider.Meter("AsyncExample")

memoryUsage, err := meter.AsyncInt64().Gauge(
_, err := meter.AsyncInt64().Gauge(
"MemoryUsage",
instrument.WithUnit(unit.Bytes),
)
if err != nil {
fmt.Println("Failed to register instrument")
panic(err)
}

err = meter.RegisterCallback([]instrument.Asynchronous{memoryUsage},
func(ctx context.Context) {
// instrument.WithCallbackFunc(func(ctx context.Context) {
asyncint64.WithGaugeCallback(func(ctx context.Context, memoryUsage asyncint64.Gauge) {
//Do Work to get the real memoryUsage
// mem := GatherMemory(ctx)
mem := 75000

memoryUsage.Observe(ctx, int64(mem))
})
}),
)
if err != nil {
fmt.Println("Failed to register callback")
fmt.Println("Failed to register instrument")
panic(err)
}
}
Expand Down
22 changes: 22 additions & 0 deletions metric/instrument/asyncfloat64/asyncfloat64.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,25 @@ type Gauge interface {

instrument.Asynchronous
}

func WithCounterCallback(fn func(context.Context, Counter)) instrument.Option {
return instrument.WithCallback(func(ctx context.Context, inst instrument.Asynchronous) {
if counter, ok := inst.(Counter); ok {
fn(ctx, counter)
}
})
}
func WithUpDownCounterCallback(fn func(context.Context, UpDownCounter)) instrument.Option {
return instrument.WithCallback(func(ctx context.Context, inst instrument.Asynchronous) {
if counter, ok := inst.(UpDownCounter); ok {
fn(ctx, counter)
}
})
}
func WithGaugeCallback(fn func(context.Context, Gauge)) instrument.Option {
return instrument.WithCallback(func(ctx context.Context, inst instrument.Asynchronous) {
if gauge, ok := inst.(Gauge); ok {
fn(ctx, gauge)
}
})
}
22 changes: 22 additions & 0 deletions metric/instrument/asyncint64/asyncint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,25 @@ type Gauge interface {

instrument.Asynchronous
}

func WithCounterCallback(fn func(context.Context, Counter)) instrument.Option {
return instrument.WithCallback(func(ctx context.Context, inst instrument.Asynchronous) {
if counter, ok := inst.(Counter); ok {
fn(ctx, counter)
}
})
}
func WithUpDownCounterCallback(fn func(context.Context, UpDownCounter)) instrument.Option {
return instrument.WithCallback(func(ctx context.Context, inst instrument.Asynchronous) {
if counter, ok := inst.(UpDownCounter); ok {
fn(ctx, counter)
}
})
}
func WithGaugeCallback(fn func(context.Context, Gauge)) instrument.Option {
return instrument.WithCallback(func(ctx context.Context, inst instrument.Asynchronous) {
if gauge, ok := inst.(Gauge); ok {
fn(ctx, gauge)
}
})
}
19 changes: 18 additions & 1 deletion metric/instrument/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

package instrument // import "go.opentelemetry.io/otel/metric/instrument"

import "go.opentelemetry.io/otel/metric/unit"
import (
"go.opentelemetry.io/otel/metric/unit"
"golang.org/x/net/context"
)

// Config contains options for metric instrument descriptors.
type Config struct {
description string
unit unit.Unit

callback func(context.Context, Asynchronous)
}

// Description describes the instrument in human-readable terms.
Expand All @@ -32,6 +37,12 @@ func (cfg Config) Unit() unit.Unit {
return cfg.unit
}

// Callback returns the function that will be registered on asynchronous
// instrument creation.
func (cfg Config) Callback() func(context.Context, Asynchronous) {
return cfg.callback
}

// Option is an interface for applying metric instrument options.
type Option interface {
// ApplyMeter is used to set a Option value of a
Expand Down Expand Up @@ -68,3 +79,9 @@ func WithUnit(unit unit.Unit) Option {
cfg.unit = unit
})
}

func WithCallback(fn func(context.Context, Asynchronous)) Option {
return optionFunc(func(cfg *Config) {
cfg.callback = fn
})
}