feat: slow threshold customizable in sqlx (#1188)

This commit is contained in:
Kevin Wan
2021-11-01 08:37:44 +08:00
committed by GitHub
parent f13e6f1149
commit 785d100be9
4 changed files with 25 additions and 10 deletions

View File

@@ -5,10 +5,18 @@ import (
"time"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx"
"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) {
stmt, err := format(q, args...)
@@ -19,7 +27,7 @@ func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
startTime := timex.Now()
result, err := conn.Exec(q, args...)
duration := timex.Since(startTime)
if duration > slowThreshold {
if duration > slowThreshold.Load() {
logx.WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
} else {
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()
result, err := conn.Exec(args...)
duration := timex.Since(startTime)
if duration > slowThreshold {
if duration > slowThreshold.Load() {
logx.WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
} else {
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()
rows, err := conn.Query(q, args...)
duration := timex.Since(startTime)
if duration > slowThreshold {
if duration > slowThreshold.Load() {
logx.WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
} else {
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()
rows, err := conn.Query(args...)
duration := timex.Since(startTime)
if duration > slowThreshold {
if duration > slowThreshold.Load() {
logx.WithDuration(duration).Slowf("[SQL] queryStmt: slowcall - %s", stmt)
} else {
logx.WithDuration(duration).Infof("sql queryStmt: %s", stmt)