2 Star 1 Fork 1

mosache/YFrame

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
middleware.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
ヤ沒脩袮兲︶ 提交于 2023-05-19 18:09 . 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)
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mosache/YFrame.git
git@gitee.com:mosache/YFrame.git
mosache
YFrame
YFrame
v0.1.87

搜索帮助