Go gin 接入 prometheus


Prometheus 的 go sdk 是由官方提供的,地址:https://github.com/prometheus/client_golang ,里面包含了生成 prometheus metrics、http handler、prometheus client 的功能。

安裝:

go get github.com/prometheus/client_golang

編寫 gin handle

func PromHandler(handler http.Handler) gin.HandlerFunc {
	return func(c *gin.Context) {
		handler.ServeHTTP(c.Writer, c.Request)
	}
}

配置 route

# e *gin.Engine
e.GET("metrics", prom.PromHandler(promhttp.Handler()))

啟動項目,訪問 /metrics

包含了一些內置指標,比如 gc 等等。下一步就可以配置接入 prometheus server,grafana 配置界面了

grafana dashboard


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM