/
middleware.go
37 lines (29 loc) · 1.12 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package gin_metrics
import "C"
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"time"
)
func metricsMiddleware(c *gin.Context) {
start := time.Now().UnixNano()
// Request time
GinRequestTotalCount.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.Request.URL.Path}).Inc()
//GinRequestGauge.With(prometheus.Labels{"method": c.Request.Method, "path": c.Request.URL.Path}).
//fmt.Println(time.Now().UnixNano() - start)
//fmt.Printf("时间戳(毫秒):%v;\n", time.Now().Sub(start))
//fmt.Printf("时间戳(毫秒):%v;\n", time.Now().UnixNano()/1e6)
//start := time.Now()
c.Next()
//fmt.Println(c.ClientIP(), c.Request.URL.Path, start.Sub(time.Now()))
//costTime := time.Since(start)
//respTime.WithLabelValues(fmt.Sprintf("%d", costTime)).Observe(costTime.Seconds())
//
end := time.Now().UnixNano()
use_time := end - start
GinRequestSummary.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.Request.URL.Path}).Observe(float64(use_time))
//use_time := float64(end / 1e6)
//fmt.Println((end - start) / 1e6)
//fmt.Println()
//fmt.Println(path)
}