-
Notifications
You must be signed in to change notification settings - Fork 7
/
gin.go
88 lines (74 loc) · 2.21 KB
/
gin.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package apitoolkit
import (
"bytes"
"context"
"errors"
"io"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type ginBodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w *ginBodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func (w *ginBodyLogWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (c *Client) GinMiddleware(ctx *gin.Context) {
// Register the client in the context,
// so it can be used for outgoing requests with little ceremony
ctx.Set(string(CurrentClient), c)
msgID := uuid.Must(uuid.NewRandom())
ctx.Set(string(CurrentRequestMessageID), msgID)
errorList := []ATError{}
ctx.Set(string(ErrorListCtxKey), &errorList)
newCtx := context.WithValue(ctx.Request.Context(), ErrorListCtxKey, &errorList)
newCtx = context.WithValue(newCtx, CurrentClient, c)
newCtx = context.WithValue(newCtx, CurrentRequestMessageID, msgID)
ctx.Request = ctx.Request.WithContext(newCtx)
start := time.Now()
reqByteBody, _ := io.ReadAll(ctx.Request.Body)
ctx.Request.Body = io.NopCloser(bytes.NewBuffer(reqByteBody))
blw := &ginBodyLogWriter{body: bytes.NewBuffer([]byte{}), ResponseWriter: ctx.Writer}
ctx.Writer = blw
pathParams := map[string]string{}
for _, param := range ctx.Params {
pathParams[param.Key] = param.Value
}
defer func() {
if err := recover(); err != nil {
if _, ok := err.(error); !ok {
err = errors.New(err.(string))
}
ReportError(ctx.Request.Context(), err.(error))
payload := c.buildPayload(GoGinSDKType, start,
ctx.Request, 500,
reqByteBody, blw.body.Bytes(), ctx.Writer.Header().Clone(),
pathParams, ctx.FullPath(),
c.config.RedactHeaders, c.config.RedactRequestBody, c.config.RedactResponseBody,
errorList,
msgID,
nil,
)
c.PublishMessage(ctx, payload)
panic(err)
}
}()
ctx.Next()
payload := c.buildPayload(GoGinSDKType, start,
ctx.Request, ctx.Writer.Status(),
reqByteBody, blw.body.Bytes(), ctx.Writer.Header().Clone(),
pathParams, ctx.FullPath(),
c.config.RedactHeaders, c.config.RedactRequestBody, c.config.RedactResponseBody,
errorList,
msgID,
nil,
)
c.PublishMessage(ctx, payload)
}