-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.go
43 lines (38 loc) · 1.28 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
38
39
40
41
42
43
package gin_exporter
import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"time"
)
func exporterAppInfo(c *gin.Context) {
GinAppInfo.With(prometheus.Labels{
"group": "default", // 分组
"type": "default", // 类型
"system": "default-api", // 系统
"instance": "default-api-instances-1", // 实例
"version": "0.0.0", // 版本
"commit_id": "00000000000000000000000000000000000000", // commit_id
"started_at": "2019-08-17 00:00:00", //
"platform": "windows...", //
})
}
func exporterGlobalRequestCount(c *gin.Context) {
GinRequestCount.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.FullPath()}).Inc()
}
func exporterGinRequestHistogram(c *gin.Context) {
start := time.Now().UnixNano()
c.Next()
end := time.Now().UnixNano()
use_time := end - start
GinRequestSummary.With(
prometheus.Labels{
"method": c.Request.Method,
"endpoint": c.FullPath(),
},
).Observe(float64(use_time))
}
func defaultExporter(c *gin.Context) {
exporterAppInfo(c)
exporterGlobalRequestCount(c)
exporterGinRequestHistogram(c)
}