-
Notifications
You must be signed in to change notification settings - Fork 3
/
zap.go
121 lines (94 loc) · 2.77 KB
/
zap.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
package zap
import (
"fmt"
"net/http"
"strings"
"time"
tz "github.com/alexfalkowski/go-service/telemetry/logger/zap"
tm "github.com/alexfalkowski/go-service/transport/meta"
ss "github.com/alexfalkowski/go-service/transport/strings"
snoop "github.com/felixge/httpsnoop"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
service = "http"
)
// NewHandler for zap.
func NewHandler(logger *zap.Logger) *Handler {
return &Handler{logger: logger}
}
// Handler for zap.
type Handler struct {
logger *zap.Logger
}
// ServeHTTP or zap.
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
path, method := req.URL.Path, strings.ToLower(req.Method)
if ss.IsHealth(path) {
next(resp, req)
return
}
ctx := req.Context()
fields := []zapcore.Field{
zap.String(tm.ServiceKey, service),
zap.String(tm.PathKey, path),
zap.String(tm.MethodKey, method),
}
m := snoop.CaptureMetricsFn(resp, func(res http.ResponseWriter) { next(res, req.WithContext(ctx)) })
fields = append(fields, zap.Stringer(tm.DurationKey, m.Duration), zap.Int(tm.CodeKey, m.Code))
fields = append(fields, tz.Meta(ctx)...)
tz.LogWithFunc(message(fmt.Sprintf("%s %s", method, path)), nil, codeToLevel(m.Code, h.logger), fields...)
}
// NewRoundTripper for zap.
func NewRoundTripper(logger *zap.Logger, r http.RoundTripper) *RoundTripper {
return &RoundTripper{logger: logger, RoundTripper: r}
}
// RoundTripper for zap.
type RoundTripper struct {
logger *zap.Logger
http.RoundTripper
}
// RoundTrip for zap.
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if ss.IsHealth(req.URL.String()) {
return r.RoundTripper.RoundTrip(req)
}
path, method := req.URL.Path, strings.ToLower(req.Method)
start := time.Now()
ctx := req.Context()
resp, err := r.RoundTripper.RoundTrip(req)
fields := []zapcore.Field{
zap.Stringer(tm.DurationKey, time.Since(start)),
zap.String(tm.ServiceKey, service),
zap.String(tm.PathKey, path),
zap.String(tm.MethodKey, method),
}
fields = append(fields, tz.Meta(ctx)...)
if resp != nil {
fields = append(fields, zap.Int(tm.CodeKey, resp.StatusCode))
}
tz.LogWithFunc(message(fmt.Sprintf("%s %s", method, req.URL.Redacted())), err, respToLevel(resp, r.logger), fields...)
return resp, err
}
func respToLevel(resp *http.Response, logger *zap.Logger) func(msg string, fields ...zapcore.Field) {
var code int
if resp != nil {
code = resp.StatusCode
} else {
code = 500
}
return codeToLevel(code, logger)
}
func codeToLevel(code int, logger *zap.Logger) func(msg string, fields ...zapcore.Field) {
if code >= 400 && code <= 499 {
return logger.Warn
}
if code >= 500 && code <= 599 {
return logger.Error
}
return logger.Info
}
func message(msg string) string {
return "http: " + msg
}