Closed younglifestyle closed 2 years ago
前面的example里面我也主动请求调用了接口,没有反应。 这个直接调用prom接口可以注册指标
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.",
})
var cpuTemp1 = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu_temperature_celsius2",
Help: "Current temperature of the CPU.",
})
func init() {
prometheus.MustRegister(cpuTemp)
prometheus.MustRegister(cpuTemp1)
}
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(":9001")
}
代码中设置的指标在prometheus找不到