登录
首页 >  Golang >  Go问答

在 OpenCensus 中如何监测 Prometheus Gauge?

来源:stackoverflow

时间:2024-03-13 10:27:28 482浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《在 OpenCensus 中如何监测 Prometheus Gauge?》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

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

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

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


解决方案


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

我还没有尝试过这个,但 lastvalue 可能(!?)转换为 prometheus 仪表。

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",
        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 运行 . 会产生:

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

到这里,我们也就讲完了《在 OpenCensus 中如何监测 Prometheus Gauge?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>