Merge branch 'main' of ssh://git.makemake.in:5566/kzkzzzz/mycommon

This commit is contained in:
kzkzzzz
2025-07-27 10:12:24 +08:00
5 changed files with 477 additions and 3 deletions

View File

@@ -0,0 +1,32 @@
package httpsr
import (
"git.makemake.in/kzkzzzz/mycommon/mymetric"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
"time"
)
const CtxCollectRequestFrom = "ctx_collect_request_from"
func QPSCollect(svcName string) gin.HandlerFunc {
hs := mymetric.NewQPSHistogram(svcName, []string{
"svc", "method", "route", "status", "from",
}...)
return func(ctx *gin.Context) {
st := time.Now()
ctx.Next()
hs.With(prometheus.Labels{
"svc": svcName,
"method": ctx.Request.Method,
"route": ctx.Request.URL.Path,
"status": cast.ToString(ctx.Writer.Status()),
"from": ctx.GetString(CtxCollectRequestFrom),
}).Observe(float64(time.Since(st).Milliseconds()))
}
}