我可能在滥用promhttp.Handler()来实现我的微服务的用例,告诉我:

  • 版本
  • (如果具有数据库连接性)

  • 如果有更好的方法来监视我的微服务,请告诉我!

    我不确定如何以这样的方式构造句柄,即在调用/metrics时会重新评估db.Ping()

    https://s.natalian.org/2019-06-02/msping.mp4
    package main
    
    import (
        "log"
        "net/http"
        "os"
    
        _ "github.com/go-sql-driver/mysql"
        "github.com/gorilla/mux"
        "github.com/jmoiron/sqlx"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promhttp"
    )
    
    const version = "0.0.1"
    
    type App struct {
        Router *mux.Router
        DB     *sqlx.DB
    }
    
    func main() {
        a := App{}
        a.Initialize()
    
        log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), a.Router))
    }
    
    func (a *App) Initialize() {
        connectionString := "root:secret@tcp(localhost:3306)/rest_api_example?multiStatements=true&sql_mode=TRADITIONAL&timeout=5s"
        var err error
        a.DB, err = sqlx.Open("mysql", connectionString)
        if err != nil {
            log.Fatal(err)
        }
    
        microservicecheck := prometheus.NewGaugeVec(
            prometheus.GaugeOpts{
                Name: "mscheck",
                Help: "Version with DB ping check",
            },
            []string{
                "commit",
            },
        )
    
        if a.DB.Ping() == nil {
            microservicecheck.WithLabelValues(version).Set(1)
        } else {
            microservicecheck.WithLabelValues(version).Set(0)
        }
    
        prometheus.MustRegister(microservicecheck)
    
        a.Router = mux.NewRouter()
        a.initializeRoutes()
    }
    
    func (a *App) initializeRoutes() {
        a.Router.Handle("/metrics", promhttp.Handler()).Methods("GET")
    }
    

    https://play.golang.org/p/9DdXnz77S55

    最佳答案

    您还可以在调用promhttp.Handler()之前添加一个执行preflight routine(即ping测试)的中间件挂钩。但是,在收集时,我认为指标应该已经被记录;并且不是在集合实例中生成的。所以...

    尝试一个单独的go例程,该例程将定期轮询数据库连接的运行状况。这避免了任何困惑的钩子(Hook)或自定义收集器:

    var pingPollingFreq = 5 * time.Second // this should probably match the Prometheus scrape interval
    
    func (a *App) Initialize() {
        // ...
    
        prometheus.MustRegister(microservicecheck)
    
        go func() {
            for {
                if a.DB.Ping() == nil {
                    microservicecheck.WithLabelValues(version).Set(1)
                } else {
                    microservicecheck.WithLabelValues(version).Set(0)
                }
                time.Sleep(pingPollingFreq)
            }
        }()
    
        // ...
    }
    

    关于go - 如何在数据库更改时重新评估promhttp.Handler?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56412460/

    10-10 19:46