首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OpenCensus测量普罗米修斯仪表?

如何使用OpenCensus测量普罗米修斯仪表?
EN

Stack Overflow用户
提问于 2020-10-08 14:07:16
回答 1查看 207关注 0票数 1

我正在尝试找到一种在Golang中使用OpenCencus测量普罗米修斯量规的方法。目标是跟踪活动会话的数量。因此,该值可以增加和减少,也可以在服务器重新启动时重置为0。

他们有一个https://opencensus.io/quickstart/go/metrics/的例子,但我不能将任何与Gauge和relate关联到0。

你能建议我应该使用哪个测量和视图来测量仪表,哪些可以增加、减少和重置为0吗?

EN

回答 1

Stack Overflow用户

发布于 2020-10-16 04:40:08

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

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

Count给出了测量的次数,并产生一个(递增的)计数器。所以,这对你没有帮助。

唯一的替代方案是SumDistribution

如果LastValue不能产生量规,您可能需要使用Distribution

更新:LastValue == Gauge

黑掉了给出的例子:

代码语言:javascript
复制
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 .会产生如下结果:

代码语言:javascript
复制
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收益率的指标:

代码语言:javascript
复制
# 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=8distlast_dist_sum的值为463

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64256689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档