-
Notifications
You must be signed in to change notification settings - Fork 1
/
ent.go
256 lines (237 loc) · 9.62 KB
/
ent.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
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
package tracer
import (
"context"
"database/sql"
"fmt"
"reflect"
"strings"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
// EntHook defines the "mutation middleware". A function that gets a Mutator
// and returns a Mutator.
func EntHook() ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
// Extract dynamic labels from mutation.
var (
labels = fmt.Sprintf("%s.%s", m.Type(), m.Op().String())
cx, span, l = StartSpanLogTrace(ctx, labels)
start = time.Now()
)
defer span.End()
// Before mutation, start measuring time.
v, err := next.Mutate(cx, m)
if err != nil {
l.Error().Err(err).Msg("error hook tracer")
}
// Stop time measure.
var duration = time.Since(start)
l.Info().Dur("duration", duration).
Str("table", m.Type()).
Str("operation", m.Op().String()).
Msg("The latency of calls in milliseconds")
return v, err
})
}
}
// EntInterceptor defines an execution middleware for various types of Ent queries.
func EntInterceptor() ent.Interceptor {
return ent.InterceptFunc(func(next ent.Querier) ent.Querier {
return ent.QuerierFunc(func(ctx context.Context, query ent.Query) (ent.Value, error) {
var (
labels = strings.ToLower(reflect.TypeOf(query).String())
cx, span, l = StartSpanLogTrace(ctx, fmt.Sprintf("query.%s", labels))
start = time.Now()
)
if strings.HasSuffix(labels, "query") {
defer span.End()
labels = strings.ReplaceAll(labels, "query", "")
labels = strings.ReplaceAll(labels, "*ent.", "")
}
// Before mutation, start measuring time.
v, err := next.Query(cx, query)
if err != nil {
l.Error().Err(err).Msg("error interceptor tracer")
}
// Stop time measure.
var duration = time.Since(start)
l.Info().Dur("duration", duration).
Str("table", labels).
Str("operation", "query").
Msg("The latency of calls in milliseconds")
return v, err
})
})
}
// EntDriver is a driver that logs all driver operations.
type EntDriver struct {
dialect.Driver // underlying driver.
log func(context.Context, zerolog.Level, string, error) // log function. defaults to log.Println.
}
// EntDriverWithContext gets a driver and a logging function, and returns
// a new tracer-driver that prints all outgoing operations with context.
func EntDriverWithContext(d dialect.Driver, logger func(context.Context, zerolog.Level, string, error)) dialect.Driver {
drv := &EntDriver{d, logger}
return drv
}
// Exec logs its params and calls the underlying driver Exec method.
func (d *EntDriver) Exec(ctx context.Context, query string, args, v any) error {
if err := d.Driver.Exec(ctx, query, args, v); err != nil {
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("driver.Exec: query=%v args=%v", query, args), err)
return err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("driver.Exec: query=%v args=%v", query, args), nil)
return nil
}
// ExecContext logs its params and calls the underlying driver ExecContext method if it is supported.
func (d *EntDriver) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
drv, ok := d.Driver.(interface {
ExecContext(context.Context, string, ...any) (sql.Result, error)
})
if !ok {
var err = fmt.Errorf("Driver.ExecContext is not supported")
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("driver.ExecContext: query=%v args=%v", query, args), err)
return nil, err
}
result, err := drv.ExecContext(ctx, query, args...)
if err != nil {
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("driver.ExecContext: query=%v args=%v", query, args), err)
return nil, err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("driver.ExecContext: query=%v args=%v", query, args), nil)
return result, nil
}
// Query logs its params and calls the underlying driver Query method.
func (d *EntDriver) Query(ctx context.Context, query string, args, v any) error {
if err := d.Driver.Query(ctx, query, args, v); err != nil {
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("driver.Query: query=%v args=%v", query, args), err)
return err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("driver.Query: query=%v args=%v", query, args), nil)
return nil
}
// QueryContext logs its params and calls the underlying driver QueryContext method if it is supported.
func (d *EntDriver) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
drv, ok := d.Driver.(interface {
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
})
if !ok {
var err = fmt.Errorf("Driver.QueryContext is not supported")
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("driver.QueryContext: query=%v args=%v", query, args), err)
return nil, err
}
rows, err := drv.QueryContext(ctx, query, args...)
if err != nil {
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("driver.QueryContext: query=%v args=%v", query, args), err)
return nil, err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("driver.QueryContext: query=%v args=%v", query, args), nil)
return rows, nil
}
// Tx adds a log-id for the transaction and calls the underlying driver Tx command.
func (d *EntDriver) Tx(ctx context.Context) (dialect.Tx, error) {
tx, err := d.Driver.Tx(ctx)
if err != nil {
return nil, err
}
id := uuid.New().String()
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("driver.Tx(%s): started", id), nil)
return &Tx{tx, id, d.log, ctx}, nil
}
// BeginTx adds a log-id for the transaction and calls the underlying driver BeginTx command if it is supported.
func (d *EntDriver) BeginTx(ctx context.Context, opts *sql.TxOptions) (dialect.Tx, error) {
drv, ok := d.Driver.(interface {
BeginTx(context.Context, *sql.TxOptions) (dialect.Tx, error)
})
if !ok {
var err = fmt.Errorf("Driver.BeginTx is not supported")
d.log(ctx, zerolog.ErrorLevel, "driver.BeginTx: cannot started", err)
return nil, err
}
tx, err := drv.BeginTx(ctx, opts)
if err != nil {
d.log(ctx, zerolog.InfoLevel, "driver.BeginTx: cannot started", err)
return nil, err
}
id := uuid.New().String()
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("driver.BeginTx(%s): started", id), nil)
return &Tx{tx, id, d.log, ctx}, nil
}
// Tx is a transaction implementation that logs all transaction operations.
type Tx struct {
dialect.Tx // underlying transaction.
id string // transaction logging id.
log func(context.Context, zerolog.Level, string, error) // log function. defaults to fmt.Println.
ctx context.Context // underlying transaction context.
}
// Exec logs its params and calls the underlying transaction Exec method.
func (d *Tx) Exec(ctx context.Context, query string, args, v any) error {
if err := d.Tx.Exec(ctx, query, args, v); err != nil {
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("Tx(%s).Exec: query=%v args=%v", d.id, query, args), err)
return err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("Tx(%s).Exec: query=%v args=%v", d.id, query, args), nil)
return nil
}
// ExecContext logs its params and calls the underlying transaction ExecContext method if it is supported.
func (d *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
drv, ok := d.Tx.(interface {
ExecContext(context.Context, string, ...any) (sql.Result, error)
})
if !ok {
var err = fmt.Errorf("Tx.ExecContext is not supported")
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("Tx(%s).Exec: query=%v args=%v", d.id, query, args), err)
return nil, err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("Tx(%s).ExecContext: query=%v args=%v", d.id, query, args), nil)
return drv.ExecContext(ctx, query, args...)
}
// Query logs its params and calls the underlying transaction Query method.
func (d *Tx) Query(ctx context.Context, query string, args, v any) error {
if err := d.Tx.Query(ctx, query, args, v); err != nil {
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("Tx(%s).Exec: query=%v args=%v", d.id, query, args), err)
return err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("Tx(%s).Query: query=%v args=%v", d.id, query, args), nil)
return nil
}
// QueryContext logs its params and calls the underlying transaction QueryContext method if it is supported.
func (d *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
drv, ok := d.Tx.(interface {
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
})
if !ok {
var err = fmt.Errorf("Tx.QueryContext is not supported")
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("Tx(%s).Exec: query=%v args=%v", d.id, query, args), err)
return nil, err
}
row, err := drv.QueryContext(ctx, query, args...)
if err != nil {
d.log(ctx, zerolog.ErrorLevel, fmt.Sprintf("Tx(%s).Exec: query=%v args=%v", d.id, query, args), err)
return nil, err
}
d.log(ctx, zerolog.InfoLevel, fmt.Sprintf("Tx(%s).QueryContext: query=%v args=%v", d.id, query, args), nil)
return row, nil
}
// Commit logs this step and calls the underlying transaction Commit method.
func (d *Tx) Commit() error {
if err := d.Tx.Commit(); err != nil {
d.log(d.ctx, zerolog.ErrorLevel, fmt.Sprintf("Tx(%s): committed", d.id), err)
return err
}
d.log(d.ctx, zerolog.InfoLevel, fmt.Sprintf("Tx(%s): committed", d.id), nil)
return nil
}
// Rollback logs this step and calls the underlying transaction Rollback method.
func (d *Tx) Rollback() error {
if err := d.Tx.Rollback(); err != nil {
d.log(d.ctx, zerolog.ErrorLevel, fmt.Sprintf("Tx(%s): rollbacked", d.id), err)
return err
}
d.log(d.ctx, zerolog.InfoLevel, fmt.Sprintf("Tx(%s): rollbacked", d.id), nil)
return nil
}