示例代码(main.go):package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var reqTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{Name: "http_requests_total", Help: "Total HTTP requests"},
[]string{"path"},
)
func init() {
prometheus.MustRegister(reqTotal)
}
func main() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/api/ping", func(w http.ResponseWriter, r *http.Request) {
reqTotal.WithLabelValues("/api/ping").Inc()
w.Header().Set("content-type", "application/json")
w.Write([]byte(`{"ok":true}`))
})
http.ListenAndServe(":8080", nil)
}

发表评论 取消回复