在 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
为您提供测量次数并产生一个(递增的)计数器。所以,这对你没有帮助。
唯一的其他替代方案是 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 运行 .
会产生:
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学习网公众号,带你了解更多关于的知识点!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习