dpcyx08288 2016-06-03 10:18
浏览 1598
已采纳

如何使用client_golang将指标推送到Prometheus?

I have not yet able to find some good examples in using Gauge, Counter and Histogram in prometheus. Any help on this will do. I tried using the the documentation but I was not able to successfully create a working app.

  • 写回答

2条回答 默认 最新

  • douhuai2861 2016-06-03 10:40
    关注

    Prometheus is a pull-based system, if you want push-based monitoring, you need to use a gateway of some sort. A minimal example (without actually doing anything useful like starting an HTTP listener, or actually doing anything to a metric) follows:

    import (
            "github.com/prometheus/client_golang/prometheus"
            "net/http"
    )
    
    var responseMetric = prometheus.NewHistogram(
            prometheus.HistogramOpts{
                    Name: "request_duration_milliseconds",
                    Help: "Request latency distribution",
                    Buckets: prometheus.ExponentialBuckets(10.0, 1.13, 40),
            })
    
    func main() {
            prometheus.MustRegister(responseMetric)
            http.Handle("/metrics", prometheus.Handler())
            // Any other setup, then an http.ListenAndServe here
    }
    

    You then need to configure Prometheus to scrape the /metrics page your binary provides.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douyou4819 2016-06-03 11:55
    关注

    I have found this

    `

    package main
    
    import (
        "net/http"
    
        "github.com/prometheus/client_golang/prometheus"
    )
    
    var (
    cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
        Name: "cpu_temperature_celsius",
        Help: "Current temperature of the CPU.",
     })
    hdFailures = prometheus.NewCounter(prometheus.CounterOpts{
        Name: "hd_errors_total",
        Help: "Number of hard-disk errors.",
    })
    )
    
    func init() {
        prometheus.MustRegister(cpuTemp)
        prometheus.MustRegister(hdFailures)
    }
    
    func main() {
        cpuTemp.Set(65.3)
        hdFailures.Inc()
    
        http.Handle("/metrics", prometheus.Handler())
        http.ListenAndServe(":8080", nil)
    }
    

    `

    This might be useful to some.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于网上一个easyx制作的见缝插针小游戏(c++)
  • ¥15 开地址法双散列函数处理碰撞
  • ¥15 想问一下这个是什么情况 虚拟机Linux打不开了
  • ¥15 联通光猫掉注册了怎么重新注册上去
  • ¥15 关于unity开发steamvr程序遇到的问题
  • ¥60 求tc downloader的下载方式
  • ¥15 华为 快捷方式 手电筒 接口
  • ¥15 Qt6.5支不支持Android13开发啊
  • ¥20 网络只能跑一半,应该如何设置
  • ¥20 Python调用百度开发者平台人脸识别接口