我正在尝试使用 client_golang 在 GoLang 中编写 JSON 导出器
我找不到任何有用的例子。我有一个服务 ABC,它通过 HTTP 生成 JSON 输出。我想使用 client-golang 将这个指标导出到 prometheus。
最佳答案
看一下 Go 客户端的 godoc,它非常详细并且包含大量示例。 Collector 接口(interface)可能是最相关的:
https://godoc.org/github.com/prometheus/client_golang/prometheus#example-Collector
本质上,您需要实现 Collector 接口(interface),其中包含两个方法:describe
和 collect
。
describe
只是通过给定 channel 发送收集器可能指标的描述。这包括它们的名称、可能的标签值和帮助字符串。
collect
创建与 describe
中的描述相匹配的实际指标,并用数据填充它们。因此,在您的情况下,它将从您的服务获取 JSON,对其进行解码,并将值写入相关指标。
在您的 main
函数中,您必须注册您的收集器,并启动 HTTP 服务器,如下所示:
prometheus.MustRegister(NewCustomCollector())
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
关于json - 如何使用 client_golang 在 prometheus 中提取指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42851300/