Skip to content

Commit 291aaa0

Browse files
authored
Redefine gauge in Prometheus example to be a Gauge (#3498)
* Rename gauge var name in Prometheus example Fix #3493 * Switch to actual gauge
1 parent 289a612 commit 291aaa0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

example/prometheus/main.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3339
func 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

Comments
 (0)