1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-03-25 21:28:58 +02:00

Redefine gauge in Prometheus example to be a Gauge (#3498)

* Rename gauge var name in Prometheus example

Fix #3493

* Switch to actual gauge
This commit is contained in:
Tyler Yahn 2022-11-30 12:00:57 -08:00 committed by GitHub
parent 289a612e6a
commit 291aaa0221
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,9 +18,11 @@ import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -30,6 +32,10 @@ import (
"go.opentelemetry.io/otel/sdk/metric"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
ctx := context.Background()
@ -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"))