-
Notifications
You must be signed in to change notification settings - Fork 2
/
zap.go
197 lines (146 loc) · 5.72 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
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
package zap
import (
"context"
"database/sql/driver"
"time"
"github.com/alexfalkowski/go-service/meta"
stime "github.com/alexfalkowski/go-service/time"
tm "github.com/alexfalkowski/go-service/transport/meta"
"github.com/ngrok/sqlmw"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewInterceptor for zap.
func NewInterceptor(name string, logger *zap.Logger, interceptor sqlmw.Interceptor) *Interceptor {
return &Interceptor{name: name, logger: logger, interceptor: interceptor}
}
// Interceptor for zap.
type Interceptor struct {
name string
logger *zap.Logger
interceptor sqlmw.Interceptor
}
func (i *Interceptor) ConnBeginTx(ctx context.Context, conn driver.ConnBeginTx, txOpts driver.TxOptions) (context.Context, driver.Tx, error) {
return i.interceptor.ConnBeginTx(ctx, conn, txOpts)
}
func (i *Interceptor) ConnPrepareContext(ctx context.Context, conn driver.ConnPrepareContext, query string) (context.Context, driver.Stmt, error) {
return i.interceptor.ConnPrepareContext(ctx, conn, query)
}
func (i *Interceptor) ConnPing(ctx context.Context, conn driver.Pinger) error {
return i.interceptor.ConnPing(ctx, conn)
}
//nolint:dupl
func (i *Interceptor) ConnExecContext(ctx context.Context, conn driver.ExecerContext, query string, args []driver.NamedValue) (driver.Result, error) {
start := time.Now()
fields := []zapcore.Field{
zap.String(tm.StartTimeKey, start.Format(time.RFC3339)),
zap.String(tm.ServiceKey, i.name),
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(tm.DeadlineKey, d.Format(time.RFC3339)))
}
res, err := i.interceptor.ConnExecContext(ctx, conn, query, args)
for k, v := range meta.Strings(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(tm.DurationKey, stime.ToMilliseconds(time.Since(start))))
if err != nil {
fields = append(fields, zap.Error(err))
i.logger.Error("finished call with error", fields...)
return nil, err
}
i.logger.Info("finished call with success", fields...)
return res, nil
}
func (i *Interceptor) ConnQueryContext(ctx context.Context, conn driver.QueryerContext, query string, args []driver.NamedValue) (context.Context, driver.Rows, error) {
start := time.Now()
fields := []zapcore.Field{
zap.String(tm.StartTimeKey, start.Format(time.RFC3339)),
zap.String(tm.ServiceKey, i.name),
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(tm.DeadlineKey, d.Format(time.RFC3339)))
}
ctx, res, err := i.interceptor.ConnQueryContext(ctx, conn, query, args)
for k, v := range meta.Strings(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(tm.DurationKey, stime.ToMilliseconds(time.Since(start))))
if err != nil {
fields = append(fields, zap.Error(err))
i.logger.Error("finished call with error", fields...)
return ctx, nil, err
}
i.logger.Info("finished call with success", fields...)
return ctx, res, nil
}
func (i *Interceptor) ConnectorConnect(ctx context.Context, connect driver.Connector) (driver.Conn, error) {
return i.interceptor.ConnectorConnect(ctx, connect)
}
//nolint:revive,stylecheck
func (i *Interceptor) ResultLastInsertId(res driver.Result) (int64, error) {
return i.interceptor.ResultLastInsertId(res)
}
func (i *Interceptor) ResultRowsAffected(res driver.Result) (int64, error) {
return i.interceptor.ResultRowsAffected(res)
}
func (i *Interceptor) RowsNext(ctx context.Context, rows driver.Rows, dest []driver.Value) error {
return i.interceptor.RowsNext(ctx, rows, dest)
}
func (i *Interceptor) RowsClose(ctx context.Context, rows driver.Rows) error {
return i.interceptor.RowsClose(ctx, rows)
}
//nolint:dupl
func (i *Interceptor) StmtExecContext(ctx context.Context, stmt driver.StmtExecContext, query string, args []driver.NamedValue) (driver.Result, error) {
start := time.Now()
fields := []zapcore.Field{
zap.String(tm.StartTimeKey, start.Format(time.RFC3339)),
zap.String(tm.ServiceKey, i.name),
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(tm.DeadlineKey, d.Format(time.RFC3339)))
}
res, err := i.interceptor.StmtExecContext(ctx, stmt, query, args)
for k, v := range meta.Strings(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(tm.DurationKey, stime.ToMilliseconds(time.Since(start))))
if err != nil {
fields = append(fields, zap.Error(err))
i.logger.Error("finished call with error", fields...)
return nil, err
}
i.logger.Info("finished call with success", fields...)
return res, nil
}
func (i *Interceptor) StmtQueryContext(ctx context.Context, stmt driver.StmtQueryContext, query string, args []driver.NamedValue) (context.Context, driver.Rows, error) {
start := time.Now()
fields := []zapcore.Field{
zap.String(tm.StartTimeKey, start.Format(time.RFC3339)),
zap.String(tm.ServiceKey, i.name),
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(tm.DeadlineKey, d.Format(time.RFC3339)))
}
ctx, res, err := i.interceptor.StmtQueryContext(ctx, stmt, query, args)
for k, v := range meta.Strings(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(tm.DurationKey, stime.ToMilliseconds(time.Since(start))))
if err != nil {
fields = append(fields, zap.Error(err))
i.logger.Error("finished call with error", fields...)
return ctx, nil, err
}
i.logger.Info("finished call with success", fields...)
return ctx, res, nil
}
func (i *Interceptor) StmtClose(ctx context.Context, stmt driver.Stmt) error {
return i.interceptor.StmtClose(ctx, stmt)
}
func (i *Interceptor) TxCommit(ctx context.Context, tx driver.Tx) error {
return i.interceptor.TxCommit(ctx, tx)
}
func (i *Interceptor) TxRollback(ctx context.Context, tx driver.Tx) error {
return i.interceptor.TxRollback(ctx, tx)
}