go-kratos / examples

Examples of Kratos framework
MIT License
364 stars 145 forks source link

examples/metrics not work #6

Closed younglifestyle closed 2 years ago

younglifestyle commented 2 years ago

代码中设置的指标在prometheus找不到

8d009eb6e99c3ed06852fd40865d356 9343bcf55ef2ee7d101f8c84f980edf

younglifestyle commented 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")
}