Skip to content

Commit

Permalink
Update single asynchronous example to use inst opt (#3574)
Browse files Browse the repository at this point in the history
Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
  • Loading branch information
MrAlias and hanyuancheung authored Jan 11, 2023
1 parent 96d5de1 commit 89cb6a4
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"runtime"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/unit"
Expand Down Expand Up @@ -51,27 +52,29 @@ func ExampleMeter_asynchronous_single() {
meterProvider := metric.NewNoopMeterProvider()
meter := meterProvider.Meter("go.opentelemetry.io/otel/metric#AsyncExample")

memoryUsage, err := meter.Int64ObservableGauge(
"MemoryUsage",
_, err := meter.Int64ObservableGauge(
"DiskUsage",
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) error {
// instrument.WithCallbackFunc(func(ctx context.Context) {
//Do Work to get the real memoryUsage
// mem := GatherMemory(ctx)
mem := 75000

memoryUsage.Observe(ctx, int64(mem))
instrument.WithInt64Callback(func(ctx context.Context, inst instrument.Int64Observer) error {
// Do the real work here to get the real disk usage. For example,
//
// usage, err := GetDiskUsage(diskID)
// if err != nil {
// if retryable(err) {
// // Retry the usage measurement.
// } else {
// return err
// }
// }
//
// For demonstration purpose, a static value is used here.
usage := 75000
inst.Observe(ctx, int64(usage), attribute.Int("disk.id", 3))
return nil
})
}),
)
if err != nil {
fmt.Println("Failed to register callback")
fmt.Println("failed to register instrument")
panic(err)
}
}
Expand Down

0 comments on commit 89cb6a4

Please sign in to comment.