@@ -18,9 +18,11 @@ import (
1818 "context"
1919 "fmt"
2020 "log"
21+ "math/rand"
2122 "net/http"
2223 "os"
2324 "os/signal"
25+ "time"
2426
2527 "github.com/prometheus/client_golang/prometheus/promhttp"
2628
@@ -30,6 +32,10 @@ import (
3032 "go.opentelemetry.io/otel/sdk/metric"
3133)
3234
35+ func init () {
36+ rand .Seed (time .Now ().UnixNano ())
37+ }
38+
3339func main () {
3440 ctx := context .Background ()
3541
@@ -58,12 +64,17 @@ func main() {
5864 }
5965 counter .Add (ctx , 5 , attrs ... )
6066
61- gauge , err := meter .SyncFloat64 ().UpDownCounter ("bar" , instrument .WithDescription ("a fun little gauge" ))
67+ gauge , err := meter .AsyncFloat64 ().Gauge ("bar" , instrument .WithDescription ("a fun little gauge" ))
68+ if err != nil {
69+ log .Fatal (err )
70+ }
71+ err = meter .RegisterCallback ([]instrument.Asynchronous {gauge }, func (ctx context.Context ) {
72+ n := - 10. + rand .Float64 ()* (90. ) // [-10, 100)
73+ gauge .Observe (ctx , n , attrs ... )
74+ })
6275 if err != nil {
6376 log .Fatal (err )
6477 }
65- gauge .Add (ctx , 100 , attrs ... )
66- gauge .Add (ctx , - 25 , attrs ... )
6778
6879 // This is the equivalent of prometheus.NewHistogramVec
6980 histogram , err := meter .SyncFloat64 ().Histogram ("baz" , instrument .WithDescription ("a very nice histogram" ))
0 commit comments