-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzap.go
137 lines (110 loc) · 3.26 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package zap
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/alexfalkowski/go-service/meta"
sstrings "github.com/alexfalkowski/go-service/strings"
stime "github.com/alexfalkowski/go-service/time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
httpURL = "http.url"
httpMethod = "http.method"
httpDuration = "http.duration_ms"
httpStartTime = "http.start_time"
httpDeadline = "http.deadline"
httpStatusCode = "http.status_code"
component = "component"
httpComponent = "http"
client = "client"
server = "server"
)
// HandlerParams for zap.
type HandlerParams struct {
Logger *zap.Logger
Handler http.Handler
}
// NewHandler for zap.
func NewHandler(params HandlerParams) *Handler {
return &Handler{logger: params.Logger, Handler: params.Handler}
}
// Handler for zap.
type Handler struct {
logger *zap.Logger
http.Handler
}
// ServeHTTP or zap.
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
service, method := req.URL.Path, strings.ToLower(req.Method)
if sstrings.IsHealth(service) {
h.Handler.ServeHTTP(resp, req)
return
}
start := time.Now().UTC()
ctx := req.Context()
h.Handler.ServeHTTP(resp, req)
fields := []zapcore.Field{
zap.Int64(httpDuration, stime.ToMilliseconds(time.Since(start))),
zap.String(httpStartTime, start.Format(time.RFC3339)),
zap.String(httpURL, service),
zap.String(httpMethod, method),
zap.String("span.kind", server),
zap.String(component, httpComponent),
}
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(httpDeadline, d.UTC().Format(time.RFC3339)))
}
h.logger.Info("finished call", fields...)
}
// RoundTripperParams for zap.
type RoundTripperParams struct {
Logger *zap.Logger
RoundTripper http.RoundTripper
}
// NewRoundTripper for zap.
func NewRoundTripper(params RoundTripperParams) *RoundTripper {
return &RoundTripper{logger: params.Logger, RoundTripper: params.RoundTripper}
}
// 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 sstrings.IsHealth(req.URL.String()) {
return r.RoundTripper.RoundTrip(req)
}
service, method := req.URL.Hostname(), strings.ToLower(req.Method)
start := time.Now().UTC()
ctx := req.Context()
resp, err := r.RoundTripper.RoundTrip(req)
fields := []zapcore.Field{
zap.Int64(httpDuration, stime.ToMilliseconds(time.Since(start))),
zap.String(httpStartTime, start.Format(time.RFC3339)),
zap.String(httpURL, service),
zap.String(httpMethod, method),
zap.String("span.kind", client),
zap.String(component, httpComponent),
}
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(httpDeadline, d.UTC().Format(time.RFC3339)))
}
if err != nil {
fields = append(fields, zap.Error(err))
r.logger.Error("finished call with error", fields...)
return nil, err
}
fields = append(fields, zap.Int(httpStatusCode, resp.StatusCode))
r.logger.Info(fmt.Sprintf("finished call with code %s", resp.Status), fields...)
return resp, nil
}