-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.go
53 lines (43 loc) · 1.08 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
44
45
46
47
48
49
50
51
52
53
package treblle
import (
"bytes"
"errors"
"log"
"time"
"github.com/gin-gonic/gin"
)
const (
TreblleVersion = 0.6
SdkName = "go"
)
func GinMiddleware() gin.HandlerFunc {
return func(gctx *gin.Context) {
startTime := time.Now()
requestInfo, errReqInfo := getRequestInfo(gctx.Request, startTime)
blw := &BodyLogWriter{Body: bytes.NewBufferString(""), ResponseWriter: gctx.Writer}
gctx.Writer = blw
gctx.Next()
if !errors.Is(errReqInfo, ErrNotJson) {
ti := MetaData{
ApiKey: Config.APIKey,
ProjectID: Config.ProjectID,
Version: TreblleVersion,
Sdk: SdkName,
Data: DataInfo{
Server: Config.serverInfo,
Language: Config.languageInfo,
Request: requestInfo,
Response: getGinResponseInfo(blw, gctx.Copy(), startTime),
},
}
// don't block execution while sending data to Treblle
go sendToTreblle(ti)
}
}
}
// If anything happens to go wrong inside one of treblle-go internals, recover from panic and continue
func dontPanic() {
if err := recover(); err != nil {
log.Printf("treblle-go panic: %s", err)
}
}