28 lines
521 B
Go
28 lines
521 B
Go
package monitoring
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// PrometheusMiddleware Prometheus监控中间件
|
|
func PrometheusMiddleware(metrics *Metrics) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
start := time.Now()
|
|
|
|
c.Next()
|
|
|
|
duration := time.Since(start)
|
|
method := c.Request.Method
|
|
path := c.FullPath()
|
|
status := c.Writer.Status()
|
|
|
|
// 记录请求数
|
|
metrics.IncHTTPRequest(method, path, status)
|
|
|
|
// 记录请求耗时
|
|
metrics.ObserveHTTPRequestDuration(method, path, duration)
|
|
}
|
|
}
|