douding6266 2017-03-17 07:09
浏览 1274

如何使用client_golang在Prometheus中提取指标

I am trying to write a JSON exporter in GoLang using client_golang

I could not find any useful example for this. I have a service ABC that produces JSON output over HTTP. I want to use the client-golang to export this metric to prometheus.

  • 写回答

2条回答 默认 最新

  • douqilin4296 2017-03-26 10:47
    关注

    Take a look at the godoc for the Go client, it is very detailed and contains plenty of examples. The one for the Collector interface is probably the most relevant here:

    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.

    describe simply sends descriptions for the possible Metrics of your Collector over the given channel. 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. So in your case, it would GET the JSON from your service, unmarshal it, and write values to the relevant metrics.

    In your main function, you then have to register your collector, and start the HTTP server, like this:

    prometheus.MustRegister(NewCustomCollector())
    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(":8080", nil))
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大