简体   繁体   English

如何使用 OpenCensus 检测 Prometheus Gauge?

[英]How to instrument Prometheus Gauge using OpenCensus?

I am trying to find a way to instrument Prometheus Gauge metrics using OpenCencus in Golang.我正在尝试找到一种方法来使用 Golang 中的 OpenCencus 来检测 Prometheus Gauge 指标。 The goal is to track no of active sessions.目标是跟踪没有活动的会话。 So value can increase and decrease and also can reset to 0 on server restart.所以值可以增加和减少,也可以在服务器重启时重置为 0。

They have an example of it https://opencensus.io/quickstart/go/metrics/ , but I am not able to co-relate any with Gauge and resetting to 0.他们有一个例子https://opencensus.io/quickstart/go/metrics/ ,但我无法将任何与 Gauge 相关联并重置为 0。

Could you suggest which Measure and View I should use to instrument Gauge which can increase, decrease, and reset to 0?您能否建议我应该使用哪种 Measure 和 View 来测量可以增加、减少和重置为 0 的 Gauge?

https://opencensus.io/stats/view/ https://opencensus.io/stats/view/

I've not tried this but LastValue may (!?) convert to a Prometheus Gauge.我还没有尝试过,但LastValue可能(!?)转换为 Prometheus Gauge。

Count gives you the number of measurements and yields an (increasing) Counter. Count为您提供测量次数并产生一个(递增)计数器。 So, that's not helpful for you.所以,这对你没有帮助。

The only other alternatives are Sum and Distribution .唯一的其他选择是SumDistribution

If LastValue doesn't yield a gauge, you may need to use Distribution .如果LastValue不产生仪表,您可能需要使用Distribution

Update: LastValue == Gauge更新: LastValue == Gauge

Hacked the example that was given:黑掉了给出的例子:

package main

import (
    "context"
    "fmt"
    "log"
    "math/rand"
    "net/http"
    "os"
    "time"

    "contrib.go.opencensus.io/exporter/prometheus"
    "go.opencensus.io/stats"
    "go.opencensus.io/stats/view"
    "go.opencensus.io/tag"
)

var (
    MLatencyMs = stats.Float64("latency", "The latency in milliseconds", "ms")
)
var (
    KeyMethod, _ = tag.NewKey("method")
)

func main() {

    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    view1 := &view.View{
        Name:        "dist",
        Measure:     MLatencyMs,
        Description: "The dist of the latencies",
        TagKeys:     []tag.Key{KeyMethod},
        Aggregation: view.Distribution(0, 10, 100, 1000, 10000, 100000),
    }

    view2 := &view.View{
        Name:        "last",
        Measure:     MLatencyMs,
        Description: "The last of the latencies",
        TagKeys:     []tag.Key{KeyMethod},
        Aggregation: view.LastValue(),
    }

    if err := view.Register(view1, view2); err != nil {
        log.Fatalf("Failed to register the views: %v", err)
    }

    pe, err := prometheus.NewExporter(prometheus.Options{
        Namespace: "distlast",
    })
    if err != nil {
        log.Fatalf("Failed to create the Prometheus stats exporter: %v", err)
    }

    go func() {
        mux := http.NewServeMux()
        mux.Handle("/metrics", pe)
        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), mux))
    }()

    rand.Seed(time.Now().UnixNano())
    ctx := context.Background()

    for {
        n := rand.Intn(100)
        log.Printf("[loop] n=%d\n", n)
        stats.Record(ctx, MLatencyMs.M(float64(time.Duration(n))))
        time.Sleep(1 * time.Second)
    }

}

And then go run .然后go run . yields:产量:

2020/10/15 14:03:25 [loop] n=77
2020/10/15 14:03:26 [loop] n=62
2020/10/15 14:03:27 [loop] n=48
2020/10/15 14:03:28 [loop] n=76
2020/10/15 14:03:29 [loop] n=20
2020/10/15 14:03:30 [loop] n=46
2020/10/15 14:03:31 [loop] n=47
2020/10/15 14:03:32 [loop] n=64
2020/10/15 14:03:33 [loop] n=15
2020/10/15 14:03:34 [loop] n=8

And metrics on localhost:8080/metrics yields: localhost:8080/metrics产生:

# HELP distlast_dist The dist of the latencies
# TYPE distlast_dist histogram
distlast_dist_bucket{method="",le="10"} 1
distlast_dist_bucket{method="",le="100"} 10
distlast_dist_bucket{method="",le="1000"} 10
distlast_dist_bucket{method="",le="10000"} 10
distlast_dist_bucket{method="",le="100000"} 10
distlast_dist_bucket{method="",le="+Inf"} 10
distlast_dist_sum{method=""} 463.00000000000006
distlast_dist_count{method=""} 10
# HELP distlast_last The last of the latencies
# TYPE distlast_last gauge
distlast_last{method=""} 8

NOTE distlast_last has a value of 8 corresponding to the n=8 and distlast_dist_sum has a value of 463 .注意distlast_last的值为8对应于n=8并且distlast_dist_sum的值为463

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM