我正在尝试找到一种在 Golang 中使用 OpenCencus 来检测 Prometheus Gauge 指标的方法。目标是跟踪活动会话的数量。所以值可以增加和减少,也可以在服务器重新启动时重置为 0。
他们有一个例子https://opencensus.io/quickstart/go/metrics/,但我无法将任何与 Gauge 相关联并重置为 0。
您能否建议我应该使用哪个 Measure 和 View 来检测可以增加、减少和重置为 0 的 Gauge?
我正在尝试找到一种在 Golang 中使用 OpenCencus 来检测 Prometheus Gauge 指标的方法。目标是跟踪活动会话的数量。所以值可以增加和减少,也可以在服务器重新启动时重置为 0。
他们有一个例子https://opencensus.io/quickstart/go/metrics/,但我无法将任何与 Gauge 相关联并重置为 0。
您能否建议我应该使用哪个 Measure 和 View 来检测可以增加、减少和重置为 0 的 Gauge?
https://opencensus.io/stats/view/
我没有尝试过,但LastValue
可能(!?)转换为 Prometheus Gauge。
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",
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)
}
}
然后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
NOTE 对应的
distlast_last
值为并且值为。8
n=8
distlast_dist_sum
463