Ai
2 Star 1 Fork 1

mosache/YFrame

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
middleware.go 1.03 KB
Copy Edit Raw Blame History
ヤ沒脩袮兲︶ authored 2023-05-19 18:09 +08:00 . temp
package prometheus
import (
"gitee.com/mosache/YFrame/rest"
"github.com/prometheus/client_golang/prometheus"
"strconv"
"time"
)
type MiddlewareBuilder struct {
Namespace string
Subsystem string
Name string
Help string
}
func (m MiddlewareBuilder) Build() rest.MiddleWare {
vector := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: m.Namespace,
Subsystem: m.Subsystem,
Name: m.Name,
Help: m.Help,
Objectives: map[float64]float64{
0.5: 0.01,
0.75: 0.01,
0.90: 0.01,
0.99: 0.001,
0.999: 0.0001,
},
}, []string{"pattern", "method", "status"})
prometheus.MustRegister(vector)
return func(next rest.HandleFunc) rest.HandleFunc {
return func(ctx *rest.Context) {
st := time.Now()
defer func() {
duration := time.Now().Sub(st).Milliseconds()
pattern := ctx.MatchedRoute
if pattern == "" {
pattern = "unknown"
}
vector.WithLabelValues(pattern, ctx.Req.Method, strconv.Itoa(ctx.RespStatusCode)).Observe(float64(duration))
}()
next(ctx)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mosache/YFrame.git
git@gitee.com:mosache/YFrame.git
mosache
YFrame
YFrame
v0.1.116

Search