-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtmiddleware.go
45 lines (37 loc) · 962 Bytes
/
rtmiddleware.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
44
45
package trex
import (
"time"
"github.com/gin-gonic/gin"
)
type ReqTrFunc func(
context interface{},
method string,
path string,
query string,
agent string,
ip string,
status int,
bytes int,
start time.Time,
end time.Time,
)
// Injects a request tracer middleware into the gin context
// It collects information from the request and response, and calls
// a parameter supplied handler function with the information
// The function is called with a transaction context of interface{} type
func RequestTracerMiddleware(trf ReqTrFunc) gin.HandlerFunc {
return func(c *gin.Context) {
txc := c.MustGet("tx-context")
method := c.Request.Method
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
agent := c.Request.UserAgent()
ip := c.ClientIP()
start := time.Now()
c.Next()
end := time.Now()
status := c.Writer.Status()
size := c.Writer.Size()
trf(txc, method, path, query, agent, ip, status, size, start, end)
}
}