feat: slow threshold customizable in sqlx (#1188)
This commit is contained in:
@@ -17,6 +17,7 @@ type (
|
|||||||
Option func(opts *options)
|
Option func(opts *options)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SetSlowThreshold sets the slow threshold.
|
||||||
func SetSlowThreshold(threshold time.Duration) {
|
func SetSlowThreshold(threshold time.Duration) {
|
||||||
slowThreshold.Set(threshold)
|
slowThreshold.Set(threshold)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type (
|
|||||||
Host string
|
Host string
|
||||||
Type string `json:",default=node,options=node|cluster"`
|
Type string `json:",default=node,options=node|cluster"`
|
||||||
Pass string `json:",optional"`
|
Pass string `json:",optional"`
|
||||||
Tls bool `json:",default=false,options=true|false"`
|
Tls bool `json:",optional"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// A RedisKeyConf is a redis config with key.
|
// A RedisKeyConf is a redis config with key.
|
||||||
|
|||||||
@@ -5,10 +5,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tal-tech/go-zero/core/logx"
|
"github.com/tal-tech/go-zero/core/logx"
|
||||||
|
"github.com/tal-tech/go-zero/core/syncx"
|
||||||
"github.com/tal-tech/go-zero/core/timex"
|
"github.com/tal-tech/go-zero/core/timex"
|
||||||
)
|
)
|
||||||
|
|
||||||
const slowThreshold = time.Millisecond * 500
|
const defaultSlowThreshold = time.Millisecond * 500
|
||||||
|
|
||||||
|
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
|
||||||
|
|
||||||
|
// SetSlowThreshold sets the slow threshold.
|
||||||
|
func SetSlowThreshold(threshold time.Duration) {
|
||||||
|
slowThreshold.Set(threshold)
|
||||||
|
}
|
||||||
|
|
||||||
func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
|
func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
|
||||||
stmt, err := format(q, args...)
|
stmt, err := format(q, args...)
|
||||||
@@ -19,7 +27,7 @@ func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
|
|||||||
startTime := timex.Now()
|
startTime := timex.Now()
|
||||||
result, err := conn.Exec(q, args...)
|
result, err := conn.Exec(q, args...)
|
||||||
duration := timex.Since(startTime)
|
duration := timex.Since(startTime)
|
||||||
if duration > slowThreshold {
|
if duration > slowThreshold.Load() {
|
||||||
logx.WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
|
logx.WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
|
||||||
} else {
|
} else {
|
||||||
logx.WithDuration(duration).Infof("sql exec: %s", stmt)
|
logx.WithDuration(duration).Infof("sql exec: %s", stmt)
|
||||||
@@ -40,7 +48,7 @@ func execStmt(conn stmtConn, q string, args ...interface{}) (sql.Result, error)
|
|||||||
startTime := timex.Now()
|
startTime := timex.Now()
|
||||||
result, err := conn.Exec(args...)
|
result, err := conn.Exec(args...)
|
||||||
duration := timex.Since(startTime)
|
duration := timex.Since(startTime)
|
||||||
if duration > slowThreshold {
|
if duration > slowThreshold.Load() {
|
||||||
logx.WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
|
logx.WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
|
||||||
} else {
|
} else {
|
||||||
logx.WithDuration(duration).Infof("sql execStmt: %s", stmt)
|
logx.WithDuration(duration).Infof("sql execStmt: %s", stmt)
|
||||||
@@ -61,7 +69,7 @@ func query(conn sessionConn, scanner func(*sql.Rows) error, q string, args ...in
|
|||||||
startTime := timex.Now()
|
startTime := timex.Now()
|
||||||
rows, err := conn.Query(q, args...)
|
rows, err := conn.Query(q, args...)
|
||||||
duration := timex.Since(startTime)
|
duration := timex.Since(startTime)
|
||||||
if duration > slowThreshold {
|
if duration > slowThreshold.Load() {
|
||||||
logx.WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
|
logx.WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
|
||||||
} else {
|
} else {
|
||||||
logx.WithDuration(duration).Infof("sql query: %s", stmt)
|
logx.WithDuration(duration).Infof("sql query: %s", stmt)
|
||||||
@@ -84,7 +92,7 @@ func queryStmt(conn stmtConn, scanner func(*sql.Rows) error, q string, args ...i
|
|||||||
startTime := timex.Now()
|
startTime := timex.Now()
|
||||||
rows, err := conn.Query(args...)
|
rows, err := conn.Query(args...)
|
||||||
duration := timex.Since(startTime)
|
duration := timex.Since(startTime)
|
||||||
if duration > slowThreshold {
|
if duration > slowThreshold.Load() {
|
||||||
logx.WithDuration(duration).Slowf("[SQL] queryStmt: slowcall - %s", stmt)
|
logx.WithDuration(duration).Slowf("[SQL] queryStmt: slowcall - %s", stmt)
|
||||||
} else {
|
} else {
|
||||||
logx.WithDuration(duration).Infof("sql queryStmt: %s", stmt)
|
logx.WithDuration(duration).Infof("sql queryStmt: %s", stmt)
|
||||||
|
|||||||
@@ -171,6 +171,12 @@ func TestStmt_query(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetSlowThreshold(t *testing.T) {
|
||||||
|
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
|
||||||
|
SetSlowThreshold(time.Second)
|
||||||
|
assert.Equal(t, time.Second, slowThreshold.Load())
|
||||||
|
}
|
||||||
|
|
||||||
type mockedSessionConn struct {
|
type mockedSessionConn struct {
|
||||||
lastInsertId int64
|
lastInsertId int64
|
||||||
rowsAffected int64
|
rowsAffected int64
|
||||||
@@ -180,7 +186,7 @@ type mockedSessionConn struct {
|
|||||||
|
|
||||||
func (m *mockedSessionConn) Exec(query string, args ...interface{}) (sql.Result, error) {
|
func (m *mockedSessionConn) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||||
if m.delay {
|
if m.delay {
|
||||||
time.Sleep(slowThreshold + time.Millisecond)
|
time.Sleep(defaultSlowThreshold + time.Millisecond)
|
||||||
}
|
}
|
||||||
return mockedResult{
|
return mockedResult{
|
||||||
lastInsertId: m.lastInsertId,
|
lastInsertId: m.lastInsertId,
|
||||||
@@ -190,7 +196,7 @@ func (m *mockedSessionConn) Exec(query string, args ...interface{}) (sql.Result,
|
|||||||
|
|
||||||
func (m *mockedSessionConn) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
func (m *mockedSessionConn) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
||||||
if m.delay {
|
if m.delay {
|
||||||
time.Sleep(slowThreshold + time.Millisecond)
|
time.Sleep(defaultSlowThreshold + time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := errMockedPlaceholder
|
err := errMockedPlaceholder
|
||||||
@@ -209,7 +215,7 @@ type mockedStmtConn struct {
|
|||||||
|
|
||||||
func (m *mockedStmtConn) Exec(args ...interface{}) (sql.Result, error) {
|
func (m *mockedStmtConn) Exec(args ...interface{}) (sql.Result, error) {
|
||||||
if m.delay {
|
if m.delay {
|
||||||
time.Sleep(slowThreshold + time.Millisecond)
|
time.Sleep(defaultSlowThreshold + time.Millisecond)
|
||||||
}
|
}
|
||||||
return mockedResult{
|
return mockedResult{
|
||||||
lastInsertId: m.lastInsertId,
|
lastInsertId: m.lastInsertId,
|
||||||
@@ -219,7 +225,7 @@ func (m *mockedStmtConn) Exec(args ...interface{}) (sql.Result, error) {
|
|||||||
|
|
||||||
func (m *mockedStmtConn) Query(args ...interface{}) (*sql.Rows, error) {
|
func (m *mockedStmtConn) Query(args ...interface{}) (*sql.Rows, error) {
|
||||||
if m.delay {
|
if m.delay {
|
||||||
time.Sleep(slowThreshold + time.Millisecond)
|
time.Sleep(defaultSlowThreshold + time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := errMockedPlaceholder
|
err := errMockedPlaceholder
|
||||||
|
|||||||
Reference in New Issue
Block a user