package main import ( "log" "net/http" "github.com/gin-gonic/gin" "golang.org/x/sync/errgroup" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" ) var ( sOkay = promauto.NewCounter(prometheus.CounterOpts{ Name: "http_processed_okay", Help: "The number of successful requests", }) sBad = promauto.NewCounter(prometheus.CounterOpts{ Name: "http_processed_ops_bad", Help: "The number of bad requests", }) sError = promauto.NewCounter(prometheus.CounterOpts{ Name: "http_processed_ops_error", Help: "The number of internal error requests", }) sTotal = promauto.NewCounter(prometheus.CounterOpts{ Name: "http_processed_ops_total", Help: "The total number of requests", }) g errgroup.Group ) func main() { r := gin.Default() r.Use(func(ctx *gin.Context) { ctx.Next() switch c := ctx.Writer.Status(); { case c >= 200 && c < 300: sOkay.Inc() case c >= 400 && c < 500: sBad.Inc() case c >= 500: sError.Inc() } sTotal.Inc() }) r.GET("/ping", func(ctx *gin.Context) { ctx.AbortWithStatus(http.StatusBadRequest) }) g.Go(func() error { http.Handle("/metrics", promhttp.Handler()) return http.ListenAndServe(":2112", nil) }) g.Go(func() error { return r.Run() }) log.Println(g.Wait()) }