2 Star 0 Fork 0

TeamsHub/backend-gopkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
metrics.go 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
HCY 提交于 2024-05-10 14:24 +08:00 . [REV] 更新库
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cast"
)
const (
namespace = "xinliangnote"
subsystem = "go_gin_api"
)
// metricsRequestsTotal metrics for request total 计数器(Counter)
var metricsRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "requests_total",
Help: "request(ms) total",
},
[]string{"method", "path"},
)
// metricsRequestsCost metrics for requests cost 累积直方图(Histogram)
var metricsRequestsCost = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "requests_cost",
Help: "request(ms) cost milliseconds",
},
[]string{"method", "path", "success", "http_code", "business_code", "cost_milliseconds", "trace_id"},
)
//func init() {
// prometheus.MustRegister(metricsRequestsTotal, metricsRequestsCost)
//}
// RecordMetrics 记录指标
func RecordMetrics(method, uri string, success bool, httpCode, businessCode int, costSeconds float64, traceId string) {
metricsRequestsTotal.With(prometheus.Labels{
"method": method,
"path": uri,
}).Inc()
metricsRequestsCost.With(prometheus.Labels{
"method": method,
"path": uri,
"success": cast.ToString(success),
"http_code": cast.ToString(httpCode),
"business_code": cast.ToString(businessCode),
"cost_milliseconds": cast.ToString(costSeconds * 1000),
"trace_id": traceId,
}).Observe(costSeconds)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzheng0709/backend-gopkg.git
git@gitee.com:wuzheng0709/backend-gopkg.git
wuzheng0709
backend-gopkg
backend-gopkg
v1.6.28

搜索帮助