-
Notifications
You must be signed in to change notification settings - Fork 3
/
zap.go
229 lines (173 loc) · 6.89 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
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
package zap
import (
"context"
"database/sql/driver"
"fmt"
"strings"
"time"
"github.com/alexfalkowski/go-service/meta"
stime "github.com/alexfalkowski/go-service/time"
"github.com/ngrok/sqlmw"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
duration = "%s.duration"
startTime = "%s.start_time"
deadline = "%s.deadline"
component = "component"
)
// 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().UTC()
fields := []zapcore.Field{
zap.String(fmt.Sprintf(startTime, i.name), start.Format(time.RFC3339)),
zap.String("span.kind", i.name),
zap.String(component, i.name),
zap.String(fmt.Sprintf("%s.query", i.name), query),
}
for _, a := range args {
fields = append(fields, zap.Any(fmt.Sprintf("%s.args.%s", i.name, strings.ToLower(a.Name)), a.Value))
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(fmt.Sprintf(deadline, i.name), d.UTC().Format(time.RFC3339)))
}
res, err := i.interceptor.ConnExecContext(ctx, conn, query, args)
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(fmt.Sprintf(duration, i.name), 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().UTC()
fields := []zapcore.Field{
zap.String(fmt.Sprintf(startTime, i.name), start.Format(time.RFC3339)),
zap.String("span.kind", i.name),
zap.String(component, i.name),
zap.String(fmt.Sprintf("%s.query", i.name), query),
}
for _, a := range args {
fields = append(fields, zap.Any(fmt.Sprintf("%s.args.%s", i.name, strings.ToLower(a.Name)), a.Value))
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(fmt.Sprintf(deadline, i.name), d.UTC().Format(time.RFC3339)))
}
ctx, res, err := i.interceptor.ConnQueryContext(ctx, conn, query, args)
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(fmt.Sprintf(duration, i.name), 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().UTC()
fields := []zapcore.Field{
zap.String(fmt.Sprintf(startTime, i.name), start.Format(time.RFC3339)),
zap.String("span.kind", i.name),
zap.String(component, i.name),
zap.String(fmt.Sprintf("%s.query", i.name), query),
}
for _, a := range args {
fields = append(fields, zap.Any(fmt.Sprintf("%s.args.%s", i.name, strings.ToLower(a.Name)), a.Value))
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(fmt.Sprintf(deadline, i.name), d.UTC().Format(time.RFC3339)))
}
res, err := i.interceptor.StmtExecContext(ctx, stmt, query, args)
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(fmt.Sprintf(duration, i.name), 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().UTC()
fields := []zapcore.Field{
zap.String(fmt.Sprintf(startTime, i.name), start.Format(time.RFC3339)),
zap.String("span.kind", i.name),
zap.String(component, i.name),
zap.String(fmt.Sprintf("%s.query", i.name), query),
}
for _, a := range args {
fields = append(fields, zap.Any(fmt.Sprintf("%s.args.%s", i.name, strings.ToLower(a.Name)), a.Value))
}
if d, ok := ctx.Deadline(); ok {
fields = append(fields, zap.String(fmt.Sprintf(deadline, i.name), d.UTC().Format(time.RFC3339)))
}
ctx, res, err := i.interceptor.StmtQueryContext(ctx, stmt, query, args)
for k, v := range meta.Attributes(ctx) {
fields = append(fields, zap.String(k, v))
}
fields = append(fields, zap.Int64(fmt.Sprintf(duration, i.name), 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)
}