-
Notifications
You must be signed in to change notification settings - Fork 0
/
zero.go
207 lines (176 loc) · 4.64 KB
/
zero.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Package ginzero provides log handing useing zerolog package.
// Code structure based on ginrus package.
package ginzero
import (
"bytes"
"io"
"net"
"net/http"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)
var Xid = "Xid"
// ZapLogger is the minimal logger interface compatible with zerolog.Logger
type ZeroLogger interface {
Info() *zerolog.Event
Error() *zerolog.Event
}
type Config struct {
SkipPaths []string
Genxid func() string
}
type OptionFunc func(*Config)
func SkipPaths(paths []string) OptionFunc {
return func(c *Config) {
c.SkipPaths = append(c.SkipPaths, paths...)
}
}
func Genxid(genxid func() string) OptionFunc {
return func(c *Config) {
if genxid != nil {
c.Genxid = genxid
}
}
}
func Ginzero(logger ZeroLogger, optFuncs ...OptionFunc) gin.HandlerFunc {
config := &Config{}
for _, of := range optFuncs {
of(config)
}
return GinzeroWithConfig(logger, config)
}
var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func GinzeroWithConfig(logger ZeroLogger, conf *Config) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
// some evil middlewares modify this values
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
skipPaths := make(map[string]bool, len(conf.SkipPaths))
for _, path := range conf.SkipPaths {
skipPaths[path] = true
}
bodyBuf := bufferPool.Get().(*bytes.Buffer)
defer func() {
bodyBuf.Reset()
bufferPool.Put(bodyBuf)
}()
_, err := io.Copy(bodyBuf, c.Request.Body)
if err != nil {
log.Error().Err(err).Msg("Failed to read request body")
c.Error(err)
return
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBuf.Bytes()))
defer func() {
if _, ok := skipPaths[path]; !ok {
end := time.Now()
latency := end.Sub(start)
if len(c.Errors) > 0 {
l := logger.Error().
Int("status", c.Writer.Status()).
Str("method", c.Request.Method).
Str("path", path).
Str("query", query).
RawJSON("request_body", bodyBuf.Bytes()).
Str("ip", c.ClientIP()).
Str("user-agent", c.Request.UserAgent()).
Dur("latency", latency)
if conf.Genxid != nil {
l.Str(Xid, conf.Genxid())
}
// Append error field if this is an erroneous request.
for _, e := range c.Errors.Errors() {
l.Str("error", e).Send()
}
} else {
l := logger.
Info().
Int("status", c.Writer.Status()).
Str("method", c.Request.Method).
Str("path", path).
Str("query", query).
RawJSON("request_body", bodyBuf.Bytes()).
Str("ip", c.ClientIP()).
Str("user-agent", c.Request.UserAgent()).
Dur("latency", latency)
if conf.Genxid != nil {
l.Str("xid", conf.Genxid())
}
l.Send()
}
}
}()
c.Next()
}
}
func defaultHandleRecovery(c *gin.Context, _ any) {
c.AbortWithStatus(http.StatusInternalServerError)
}
func RecoveryWithZero(logger ZeroLogger, stack bool) gin.HandlerFunc {
return CustomRecoveryWithZero(logger, stack, defaultHandleRecovery)
}
func CustomRecoveryWithZero(logger ZeroLogger, stack bool, recovery gin.RecoveryFunc) gin.HandlerFunc {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// Check for a broken connection, as it is not really a
// condition that warrants a panic stack trace.
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
httpRequest, _ := httputil.DumpRequest(c.Request, false)
if brokenPipe {
logger.
Error().
Str("path", c.Request.URL.Path).
Any("error", err).
Str("request", string(httpRequest)).
Send()
// If the connection is dead, we can't write a status to it.
c.Error(err.(error)) // nolint: errcheck
c.Abort()
return
}
if stack {
errors.New(string(debug.Stack()))
logger.
Error().
Stack().
Err(errors.New(string(debug.Stack()))).
Str("error", "[Recovery from panic]").
Str("request", string(httpRequest)).
Send()
} else {
logger.
Error().
Str("error", "[Recovery from panic]").
Any("error", err).
Str("request", string(httpRequest)).
Send()
}
recovery(c, err)
}
}()
c.Next()
}
}