diff --git a/metric/example_test.go b/metric/example_test.go index ac72d3e8cba..64441c23ace 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -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" @@ -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) } }