[英]How to pull metrics in prometheus using client_golang
I am trying to write a JSON exporter in GoLang using client_golang 我正在尝试使用client_golang在GoLang中编写一个JSON导出器
I could not find any useful example for this. 我找不到任何有用的示例。 I have a service ABC that produces JSON output over HTTP.
我有一个服务ABC,它通过HTTP生成JSON输出。 I want to use the client-golang to export this metric to prometheus.
我想使用client-golang将该指标导出到prometheus。
Take a look at the godoc for the Go client, it is very detailed and contains plenty of examples. 看一下Go客户端的godoc,它非常详细,并包含许多示例。 The one for the Collector interface is probably the most relevant here:
这里的Collector界面最相关:
https://godoc.org/github.com/prometheus/client_golang/prometheus#example-Collector https://godoc.org/github.com/prometheus/client_golang/prometheus#example-Collector
Essentially, you would implement the Collector interface, which contains two methods: describe
and collect
. 本质上,您将实现Collector接口,该接口包含两个方法:
describe
和collect
。
describe
simply sends descriptions for the possible Metrics of your Collector over the given channel. describe
只是通过给定通道发送有关收集器的可能指标的描述。 This includes their name, possible label values and help string. 这包括它们的名称,可能的标签值和帮助字符串。
collect
creates actual metrics that match the descriptions from describe
and populates them with data. collect
创建一个从符合描述的实际指标describe
并用数据填充它们。 So in your case, it would GET the JSON from your service, unmarshal it, and write values to the relevant metrics. 因此,在您的情况下,它将从服务中获取JSON,将其解组,然后将值写入相关指标。
In your main
function, you then have to register your collector, and start the HTTP server, like this: 然后,在
main
功能中,您必须注册您的收集器,然后启动HTTP服务器,如下所示:
prometheus.MustRegister(NewCustomCollector())
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
You mean you want to write an exporter for your own service using golang? 您是说要使用golang为自己的服务编写导出程序? The exporters listed on prometheus exporter page are all good examples, many of which are written in golang, you could pick a simple one like redis exporter to see how it's implemented.
普罗米修斯出口商页面上列出的出口商都是很好的例子,其中许多都是用golang编写的,您可以选择一个简单的例子,例如redis出口商,以了解其实现方式。
Basically what you need to do is: 基本上,您需要做的是:
Exporter
type Exporter
类型 Exporter
to prometheus by prometheus.MustRegister Exporter
到prometheus。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.