Skip to content

Commit

Permalink
Redefine gauge in Prometheus example to be a Gauge (#3498)
Browse files Browse the repository at this point in the history
* Rename gauge var name in Prometheus example

Fix #3493

* Switch to actual gauge
  • Loading branch information
MrAlias committed Nov 30, 2022
1 parent 289a612 commit 291aaa0
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions example/prometheus/main.go
Expand Up @@ -18,9 +18,11 @@ import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"

Expand All @@ -30,6 +32,10 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func main() {
ctx := context.Background()

Expand Down Expand Up @@ -58,12 +64,17 @@ func main() {
}
counter.Add(ctx, 5, attrs...)

gauge, err := meter.SyncFloat64().UpDownCounter("bar", instrument.WithDescription("a fun little gauge"))
gauge, err := meter.AsyncFloat64().Gauge("bar", instrument.WithDescription("a fun little gauge"))
if err != nil {
log.Fatal(err)
}
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)
}
gauge.Add(ctx, 100, attrs...)
gauge.Add(ctx, -25, attrs...)

// This is the equivalent of prometheus.NewHistogramVec
histogram, err := meter.SyncFloat64().Histogram("baz", instrument.WithDescription("a very nice histogram"))
Expand Down

0 comments on commit 291aaa0

Please sign in to comment.