-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
413 lines (348 loc) · 10.6 KB
/
helper.go
File metadata and controls
413 lines (348 loc) · 10.6 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package logging
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/snabble/go-logging/v2/tracex"
)
// AccessLogCookiesBlacklist The list of cookies which should not be logged
var AccessLogCookiesBlacklist = []string{}
var LifecycleEnvVars = []string{"BUILD_NUMBER", "BUILD_HASH", "BUILD_DATE"}
func init() {
if testing.Testing() {
_ = Set("info", true)
} else {
_ = Set("info", false)
}
}
var DefaultLogConfig = LogConfig{
EnableTraces: true,
EnableTextLogging: false,
//LogLevelForServerError defaults to ErrorLevel if not set
}
type LogConfig struct {
EnableTraces bool
EnableTextLogging bool
googleCloudLogging bool
LogLevelForServerError *logrus.Level
}
func (c *LogConfig) getLogLevelForServerError() logrus.Level {
if c != nil && c.LogLevelForServerError != nil {
return *c.LogLevelForServerError
}
return logrus.ErrorLevel
}
// Set creates a new Logger with the matching specification
func Set(level string, textLogging bool) error {
config := &LogConfig{EnableTraces: true, EnableTextLogging: textLogging}
return SetWithConfig(level, config)
}
// SetGoogle configures the Logger to use GoogleCloud compatible fields in JSON format
// https://cloud.google.com/logging/docs/structured-logging#structured_logging_special_fields
func SetGoogle(level string) error {
config := &LogConfig{EnableTraces: true, googleCloudLogging: true}
return SetWithConfig(level, config)
}
// SetWithConfig creates a new Logger with the matching specification based on the config, pass nil to use
// the defaults.
func SetWithConfig(level string, config *LogConfig) error {
if config == nil {
config = &DefaultLogConfig
}
l, err := logrus.ParseLevel(level)
if err != nil {
return err
}
logger := logrus.New()
if config.EnableTextLogging {
logger.Formatter = &logrus.TextFormatter{DisableColors: true}
} else if config.googleCloudLogging {
logger.Formatter = &LogstashFormatter{
FieldMap: map[string]string{
// https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields
"@timestamp": "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
"trace": "traceID", // Trace is special in GCE so we have to use another name.
},
TimestampFormat: time.RFC3339Nano,
}
} else {
logger.Formatter = &LogstashFormatter{TimestampFormat: time.RFC3339Nano}
}
if config.EnableTraces {
logger.AddHook(tracex.NewLogrusHook())
}
logger.Level = l
Log = &Logger{Logger: logger, config: config}
return nil
}
// Access logs an access entry with call duration and status code
func Access(r *http.Request, start time.Time, statusCode int) {
access(logrus.InfoLevel, r, start, statusCode)
}
func access(level logrus.Level, r *http.Request, start time.Time, statusCode int) {
e := createAccessEntry(r, start, statusCode, nil)
var msg string
if len(r.URL.RawQuery) == 0 {
msg = fmt.Sprintf("%v ->%v %v", statusCode, r.Method, r.URL.Path)
} else {
msg = fmt.Sprintf("%v ->%v %v?%s", statusCode, r.Method, r.URL.Path, r.URL.RawQuery)
}
e.Log(Log.accessLogLevelFor(level, r, statusCode), msg)
}
func (l *Logger) accessLogLevelFor(level logrus.Level, r *http.Request, statusCode int) logrus.Level {
if statusCode < 200 {
// 100er codes are unexpected in the context of http handlers using this middleware
return logrus.ErrorLevel
}
if statusCode <= 399 {
if isHealthRequest(r) {
return logrus.DebugLevel
}
return level
}
if statusCode == http.StatusInternalServerError {
return l.config.getLogLevelForServerError()
}
return logrus.WarnLevel
}
func isHealthRequest(r *http.Request) bool {
return r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/health")
}
// AccessError logs an error while accessing
func AccessError(r *http.Request, start time.Time, err error, stack []byte) {
e := createAccessEntry(r, start, 0, err)
if stack != nil {
e = e.WithField("stack", string(stack))
}
e.Errorf("ERROR ->%v %v", r.Method, r.URL.Path)
}
func AccessAborted(r *http.Request, start time.Time) {
e := createAccessEntry(r, start, 0, nil)
e.Infof("ABORTED ->%v %v", r.Method, r.URL.Path)
}
func createAccessEntry(r *http.Request, start time.Time, statusCode int, err error) *Entry {
url := r.URL.Path
if r.URL.RawQuery != "" {
url += "?" + r.URL.RawQuery
}
fields := logrus.Fields{
TypeField: TypeAccess,
"@timestamp": start,
"remote_ip": getRemoteIP(r),
"host": r.Host,
"url": url,
"method": r.Method,
"proto": r.Proto,
"duration": time.Since(start).Nanoseconds() / 1000000,
"User_Agent": r.Header.Get("User-Agent"),
}
if statusCode != 0 {
fields["response_status"] = statusCode
}
if err != nil {
fields[logrus.ErrorKey] = err.Error()
}
cookies := map[string]string{}
for _, c := range r.Cookies() {
if !contains(AccessLogCookiesBlacklist, c.Name) {
cookies[c.Name] = c.Value
}
}
if len(cookies) > 0 {
fields["cookies"] = cookies
}
return Log.WithContext(r.Context()).WithFields(fields)
}
// Call logs the result of an outgoing call. This logs on error level if the call failed, if that is not wanted use CallWarn instead.
func Call(r *http.Request, resp *http.Response, start time.Time, err error) {
fields := fieldsForCall(r, resp, start, err)
logCall(fields, r, resp, err, logrus.ErrorLevel)
}
// Call logs the result of an outgoing call. Same as Call but logs failed calls on warning level instead of error level.
func CallWarn(r *http.Request, resp *http.Response, start time.Time, err error) {
fields := fieldsForCall(r, resp, start, err)
logCall(fields, r, resp, err, logrus.WarnLevel)
}
// FlakyCall logs the result of an outgoing call and marks it as flaky
func FlakyCall(r *http.Request, resp *http.Response, start time.Time, err error) {
fields := fieldsForCall(r, resp, start, err)
fields[FlakyField] = true
logCall(fields, r, resp, err, logrus.ErrorLevel)
}
func fieldsForCall(r *http.Request, resp *http.Response, start time.Time, err error) logrus.Fields {
url := r.URL.Path
if r.URL.RawQuery != "" {
url += "?" + r.URL.RawQuery
}
fields := logrus.Fields{
TypeField: TypeCall,
"@timestamp": start,
"host": r.Host,
"url": url,
"full_url": r.URL.String(),
"method": r.Method,
"duration": time.Since(start).Nanoseconds() / 1000000,
"content_length": r.ContentLength,
}
if err != nil {
fields[logrus.ErrorKey] = err.Error()
}
if resp != nil {
fields["response_status"] = resp.StatusCode
fields["content_type"] = resp.Header.Get("Content-Type")
}
return fields
}
func logCall(fields logrus.Fields, r *http.Request, resp *http.Response, err error, levelForErrors logrus.Level) {
entry := Log.WithContext(r.Context()).WithFields(fields)
if ctxErr := r.Context().Err(); ctxErr != nil {
entry.Info(fmt.Sprintf("Context canceled for %s-> %s with error: %s", r.Method, r.URL.String(), ctxErr.Error()))
return
}
if err != nil {
entry.Log(levelForErrors, err)
return
}
if resp != nil {
msg := fmt.Sprintf("%d %s-> %s", resp.StatusCode, r.Method, r.URL.String())
if resp.StatusCode >= 200 && resp.StatusCode <= 399 {
entry.Info(msg)
} else if resp.StatusCode >= 400 && resp.StatusCode <= 499 {
entry.Warn(msg)
} else {
entry.Log(levelForErrors, msg)
}
return
}
entry.Warn("call, but no response given")
}
// Cacheinfo logs the hit information an accessing a resource
func Cacheinfo(url string, hit bool) {
var msg string
if hit {
msg = fmt.Sprintf("cache hit: %v", url)
} else {
msg = fmt.Sprintf("cache miss: %v", url)
}
Log.WithFields(
logrus.Fields{
TypeField: TypeCacheinfo,
"url": url,
"hit": hit,
}).
Debug(msg)
}
// Application Return a log entry for application logs.
func Application(h http.Header) *Entry {
fields := logrus.Fields{
TypeField: TypeApplication,
}
return Log.WithFields(fields)
}
// LifecycleStart logs the start of an application
// with the configuration struct or map as parameter.
func LifecycleStart(appName string, args interface{}) {
fields := logrus.Fields{}
if args != nil {
jsonString, err := json.Marshal(args)
if err == nil {
err := json.Unmarshal(jsonString, &fields)
if err != nil {
fields["parse_error"] = err.Error()
}
}
}
fields[TypeField] = TypeLifecycle
fields["event"] = "start"
for _, env := range LifecycleEnvVars {
if os.Getenv(env) != "" {
fields[strings.ToLower(env)] = os.Getenv(env)
}
}
Log.WithFields(fields).Infof("starting application: %v", appName)
}
// LifecycleStop logs the request to stop an application
func LifecycleStop(appName string, signal os.Signal, err error) {
fields := logrus.Fields{
TypeField: TypeLifecycle,
"event": "stop",
}
if signal != nil {
fields["signal"] = signal.String()
}
if os.Getenv("BUILD_NUMBER") != "" {
fields["build_number"] = os.Getenv("BUILD_NUMBER")
}
if err != nil {
Log.WithFields(fields).
WithError(err).
Errorf("stopping application: %v (%v)", appName, err)
} else {
Log.WithFields(fields).Infof("stopping application: %v (%v)", appName, signal)
}
}
// LifecycleStoped logs the stop of an application
// Deprecated: Typo in name LifecycleStoped, please use LifecycleStopped instead.
func LifecycleStoped(appName string, err error) {
logApplicationLifecycleEvent(appName, "stoped", err)
}
// LifecycleStopped logs the stop of an application
func LifecycleStopped(appName string, err error) {
logApplicationLifecycleEvent(appName, "stopped", err)
}
func logApplicationLifecycleEvent(appName string, eventName string, err error) {
fields := logrus.Fields{
TypeField: TypeLifecycle,
"event": eventName,
}
if os.Getenv("BUILD_NUMBER") != "" {
fields["build_number"] = os.Getenv("BUILD_NUMBER")
}
if err != nil {
Log.WithFields(fields).
WithError(err).
Errorf("stopping application: %v (%v)", appName, err)
} else {
Log.WithFields(fields).Infof("application %s: %v", eventName, appName)
}
}
// ServerClosed logs the closing of a server
func ServerClosed(appName string) {
fields := logrus.Fields{
TypeField: TypeApplication,
"event": "stop",
}
if os.Getenv("BUILD_NUMBER") != "" {
fields["build_number"] = os.Getenv("BUILD_NUMBER")
}
Log.WithFields(fields).Infof("http server was closed: %v", appName)
}
func getRemoteIP(r *http.Request) string {
if r.Header.Get("X-Cluster-Client-Ip") != "" {
return r.Header.Get("X-Cluster-Client-Ip")
}
if r.Header.Get("X-Real-Ip") != "" {
return r.Header.Get("X-Real-Ip")
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}