forked from libgo/mysqlx
/
hook.go
57 lines (44 loc) · 1.42 KB
/
hook.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
package mysqlx
import (
"context"
"database/sql"
"database/sql/driver"
"time"
"github.com/gchaincl/sqlhooks"
"github.com/go-sql-driver/mysql"
"github.com/libgo/logx"
)
const (
driverName = "mysql_with_hook"
)
func init() {
sql.Register(driverName, sqlhooks.Wrap(&mysql.MySQLDriver{}, &Hook{}))
}
type Hook struct {
}
type sqlTimer struct{}
// Before hook will print the query with it's args and return the context with the timestamp
func (h *Hook) Before(ctx context.Context, _ string, _ ...interface{}) (context.Context, error) {
return context.WithValue(ctx, sqlTimer{}, time.Now()), nil
}
// After hook will get the timestamp registered on the Before hook and print the elapsed time
func (h *Hook) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
logger := logx.FromContext(ctx)
if logger.DebugEnabled() {
if startAt, ok := ctx.Value(sqlTimer{}).(time.Time); ok {
logger.KV("span", "sql", "took", time.Since(startAt).Milliseconds()).Debugf("> %s. %v", query, args)
}
}
return ctx, nil
}
func (h *Hook) OnError(ctx context.Context, err error, query string, args ...interface{}) error {
// skip
if err == driver.ErrSkip || err == driver.ErrBadConn {
return err
}
if startAt, ok := ctx.Value(sqlTimer{}).(time.Time); ok {
logger := logx.FromContext(ctx)
logger.KV("span", "sql", "took", time.Since(startAt).Milliseconds()).Errorf("> %s. %v", query, args)
}
return err
}