如何解决如何使用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
为您提供测量次数,并产生一个(递增的)计数器。因此,这对您没有帮助。
其他唯一的选择是Sum
和Distribution
。
如果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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。