3 回答
TA贡献1828条经验 获得超4个赞
我们只是利用处理程序。promhttp
package main
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu_temperature_celsius",
Help: "Current temperature of the CPU.",
})
func init() {
prometheus.MustRegister(cpuTemp)
}
func prometheusHandler() gin.HandlerFunc {
h := promhttp.Handler()
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
cpuTemp.Set(65.3)
r := gin.New()
r.GET("/", func(c *gin.Context) {
c.JSON(200, "Hello world!")
})
r.GET("/metrics", prometheusHandler())
r.Run()
}
或者我们总是可以切换到普罗米修斯中间件 - https://github.com/zsais/go-gin-prometheus
TA贡献1874条经验 获得超12个赞
我正在将普罗米修斯与其他库 https://github.com/Depado/ginprom 一起使用:
package main
import (
"github.com/Depado/ginprom"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
p := ginprom.New(
ginprom.Engine(r),
ginprom.Subsystem("gin"),
ginprom.Path("/metrics"),
)
r.Use(p.Instrument())
r.GET("/hello/:id", func(c *gin.Context) {})
r.GET("/world/:id", func(c *gin.Context) {})
r.Run("127.0.0.1:8080")
}
- 3 回答
- 0 关注
- 123 浏览
添加回答
举报