mirror of
https://github.com/xinliangnote/go-gin-api.git
synced 2024-04-21 12:31:46 +00:00
- 将 middleware 命名为 interceptor - 将 deploy 命名为 deployments - 移除 pkg/errno - 使用 proposal 目录 - 优化代码
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
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, path string, success bool, httpCode, businessCode int, costSeconds float64, traceId string) {
|
||
metricsRequestsTotal.With(prometheus.Labels{
|
||
"method": method,
|
||
"path": path,
|
||
}).Inc()
|
||
|
||
metricsRequestsCost.With(prometheus.Labels{
|
||
"method": method,
|
||
"path": path,
|
||
"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)
|
||
}
|