From a34b8bfc90c340f502f72163463df603ddb22c55 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 29 Nov 2022 08:29:23 -0800 Subject: [PATCH 1/4] Rename gauge var name in Prometheus example Fix #3493 --- example/prometheus/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example/prometheus/main.go b/example/prometheus/main.go index e1d87693c22..9f8675d56d5 100644 --- a/example/prometheus/main.go +++ b/example/prometheus/main.go @@ -58,12 +58,12 @@ func main() { } counter.Add(ctx, 5, attrs...) - gauge, err := meter.SyncFloat64().UpDownCounter("bar", instrument.WithDescription("a fun little gauge")) + upDownCounter, err := meter.SyncFloat64().UpDownCounter("bar", instrument.WithDescription("a fun up-down counter")) if err != nil { log.Fatal(err) } - gauge.Add(ctx, 100, attrs...) - gauge.Add(ctx, -25, attrs...) + upDownCounter.Add(ctx, 100, attrs...) + upDownCounter.Add(ctx, -25, attrs...) // This is the equivalent of prometheus.NewHistogramVec histogram, err := meter.SyncFloat64().Histogram("baz", instrument.WithDescription("a very nice histogram")) From 10b333df8b1a79f650c5e4c769ee1b21a5aab754 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 29 Nov 2022 12:41:58 -0800 Subject: [PATCH 2/4] Switch to actual gauge --- example/prometheus/main.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/example/prometheus/main.go b/example/prometheus/main.go index 9f8675d56d5..1708a8ca94a 100644 --- a/example/prometheus/main.go +++ b/example/prometheus/main.go @@ -18,9 +18,11 @@ import ( "context" "fmt" "log" + "math/rand" "net/http" "os" "os/signal" + "time" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -58,12 +60,19 @@ func main() { } counter.Add(ctx, 5, attrs...) - upDownCounter, err := meter.SyncFloat64().UpDownCounter("bar", instrument.WithDescription("a fun up-down counter")) + gauge, err := meter.AsyncFloat64().Gauge("bar", instrument.WithDescription("a fun little gauge")) if err != nil { log.Fatal(err) } - upDownCounter.Add(ctx, 100, attrs...) - upDownCounter.Add(ctx, -25, attrs...) + cBack := func() func(context.Context) { + min, max := -10., 100. + rand.Seed(time.Now().UnixNano()) + return func(ctx context.Context) { + n := min + rand.Float64()*(max-min) + gauge.Observe(ctx, n, attrs...) + } + } + meter.RegisterCallback([]instrument.Asynchronous{gauge}, cBack()) // This is the equivalent of prometheus.NewHistogramVec histogram, err := meter.SyncFloat64().Histogram("baz", instrument.WithDescription("a very nice histogram")) From 5c99c35314794ae629d176ee9d09e4715f52543d Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 29 Nov 2022 12:45:29 -0800 Subject: [PATCH 3/4] Simplify callback --- example/prometheus/main.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/example/prometheus/main.go b/example/prometheus/main.go index 1708a8ca94a..e4d81f83cfd 100644 --- a/example/prometheus/main.go +++ b/example/prometheus/main.go @@ -32,6 +32,10 @@ import ( "go.opentelemetry.io/otel/sdk/metric" ) +func init() { + rand.Seed(time.Now().UnixNano()) +} + func main() { ctx := context.Background() @@ -64,15 +68,10 @@ func main() { if err != nil { log.Fatal(err) } - cBack := func() func(context.Context) { - min, max := -10., 100. - rand.Seed(time.Now().UnixNano()) - return func(ctx context.Context) { - n := min + rand.Float64()*(max-min) - gauge.Observe(ctx, n, attrs...) - } - } - meter.RegisterCallback([]instrument.Asynchronous{gauge}, cBack()) + meter.RegisterCallback([]instrument.Asynchronous{gauge}, func(ctx context.Context) { + n := -10. + rand.Float64()*(90.) // [-10, 100) + gauge.Observe(ctx, n, attrs...) + }) // This is the equivalent of prometheus.NewHistogramVec histogram, err := meter.SyncFloat64().Histogram("baz", instrument.WithDescription("a very nice histogram")) From 6ec4b6a97907e6f3f6101e79d4ce8d7b9b066ead Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 29 Nov 2022 13:08:58 -0800 Subject: [PATCH 4/4] Fail on RegisterCallback err --- example/prometheus/main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/example/prometheus/main.go b/example/prometheus/main.go index e4d81f83cfd..bc15f041486 100644 --- a/example/prometheus/main.go +++ b/example/prometheus/main.go @@ -68,10 +68,13 @@ func main() { if err != nil { log.Fatal(err) } - meter.RegisterCallback([]instrument.Asynchronous{gauge}, func(ctx context.Context) { + err = meter.RegisterCallback([]instrument.Asynchronous{gauge}, func(ctx context.Context) { n := -10. + rand.Float64()*(90.) // [-10, 100) gauge.Observe(ctx, n, attrs...) }) + if err != nil { + log.Fatal(err) + } // This is the equivalent of prometheus.NewHistogramVec histogram, err := meter.SyncFloat64().Histogram("baz", instrument.WithDescription("a very nice histogram"))