-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgin_middleware.go
More file actions
55 lines (43 loc) · 1.08 KB
/
gin_middleware.go
File metadata and controls
55 lines (43 loc) · 1.08 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
package influence
import (
"path/filepath"
"time"
"github.com/gin-gonic/gin"
"github.com/influxdata/influxdb/client/v2"
)
// GinMiddleware sends http handler statistic to InfluxDB
func GinMiddleware(conn client.Client) gin.HandlerFunc {
tags := getEnvTags(confHandlerTagsKey)
return func(c *gin.Context) {
startTime := time.Now()
defer func() {
go func() {
endTime := time.Now()
bp, err := getPointsBatch()
if err != nil {
return
}
fields := map[string]interface{}{
"execution_time": endTime.Sub(startTime).Nanoseconds() / int64(time.Millisecond),
"content_length": c.Writer.Size(),
"status_code": c.Writer.Status(),
}
handlerTags := map[string]string{
"handler": c.Request.URL.Path,
"handler_func": filepath.Base(c.HandlerName()),
"method": c.Request.Method,
}
for k, v := range handlerTags {
tags[k] = v
}
pt, err := client.NewPoint(httpHandlerTablename, tags, fields, endTime)
if err != nil {
return
}
bp.AddPoint(pt)
conn.Write(bp)
}()
}()
c.Next()
}
}