如何使用OpenCensus来测量Prometheus量规? 更新:LastValue == Gauge

问题描述

我正在尝试找到一种在Golang中使用OpenCencus来衡量Prometheus Gauge指标的方法。目标是不跟踪活动会话。因此,值可以增加和减少,也可以在服务器重新启动时重置为0。

他们有一个https://opencensus.io/quickstart/go/metrics/的示例,但我无法与Gauge关联并将它们重置为0。

您能建议我使用哪种测量和视图来测量可以增加,减少和重置为0的仪表吗?

解决方法

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

我没有尝试过,但是LastValue可能(!?)转换为普罗米修斯量规。

Count为您提供测量次数,并产生一个(递增的)计数器。因此,这对您没有帮助。

其他唯一的选择是SumDistribution

如果LastValue未产生量规,则可能需要使用Distribution

更新:LastValue == Gauge

掌握了给出的示例:

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",Description: "The last of the latencies",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)
    }

}

然后go run .产生:

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

关于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

注意 distlast_last的值为8对应于n=8,而distlast_dist_sum的值为463。 / p>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...