stmt.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package sqlx
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. "github.com/wuntsong-org/go-zero-plus/core/logx"
  7. "github.com/wuntsong-org/go-zero-plus/core/syncx"
  8. "github.com/wuntsong-org/go-zero-plus/core/timex"
  9. )
  10. const defaultSlowThreshold = time.Millisecond * 500
  11. var (
  12. slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
  13. logSql = syncx.ForAtomicBool(true)
  14. logSlowSql = syncx.ForAtomicBool(true)
  15. )
  16. // DisableLog disables logging of sql statements, includes info and slow logs.
  17. func DisableLog() {
  18. logSql.Set(false)
  19. logSlowSql.Set(false)
  20. }
  21. // DisableStmtLog disables info logging of sql statements, but keeps slow logs.
  22. func DisableStmtLog() {
  23. logSql.Set(false)
  24. }
  25. // SetSlowThreshold sets the slow threshold.
  26. func SetSlowThreshold(threshold time.Duration) {
  27. slowThreshold.Set(threshold)
  28. }
  29. func exec(ctx context.Context, conn sessionConn, q string, args ...any) (sql.Result, error) {
  30. guard := newGuard("exec")
  31. if err := guard.start(q, args...); err != nil {
  32. return nil, err
  33. }
  34. result, err := conn.ExecContext(ctx, q, args...)
  35. guard.finish(ctx, err)
  36. return result, err
  37. }
  38. func execStmt(ctx context.Context, conn stmtConn, q string, args ...any) (sql.Result, error) {
  39. guard := newGuard("execStmt")
  40. if err := guard.start(q, args...); err != nil {
  41. return nil, err
  42. }
  43. result, err := conn.ExecContext(ctx, args...)
  44. guard.finish(ctx, err)
  45. return result, err
  46. }
  47. func query(ctx context.Context, conn sessionConn, scanner func(*sql.Rows) error,
  48. q string, args ...any) error {
  49. guard := newGuard("query")
  50. if err := guard.start(q, args...); err != nil {
  51. return err
  52. }
  53. rows, err := conn.QueryContext(ctx, q, args...)
  54. guard.finish(ctx, err)
  55. if err != nil {
  56. return err
  57. }
  58. defer rows.Close()
  59. return scanner(rows)
  60. }
  61. func queryStmt(ctx context.Context, conn stmtConn, scanner func(*sql.Rows) error,
  62. q string, args ...any) error {
  63. guard := newGuard("queryStmt")
  64. if err := guard.start(q, args...); err != nil {
  65. return err
  66. }
  67. rows, err := conn.QueryContext(ctx, args...)
  68. guard.finish(ctx, err)
  69. if err != nil {
  70. return err
  71. }
  72. defer rows.Close()
  73. return scanner(rows)
  74. }
  75. type (
  76. sqlGuard interface {
  77. start(q string, args ...any) error
  78. finish(ctx context.Context, err error)
  79. }
  80. nilGuard struct{}
  81. realSqlGuard struct {
  82. command string
  83. stmt string
  84. startTime time.Duration
  85. }
  86. )
  87. func newGuard(command string) sqlGuard {
  88. if logSql.True() || logSlowSql.True() {
  89. return &realSqlGuard{
  90. command: command,
  91. }
  92. }
  93. return nilGuard{}
  94. }
  95. func (n nilGuard) start(_ string, _ ...any) error {
  96. return nil
  97. }
  98. func (n nilGuard) finish(_ context.Context, _ error) {
  99. }
  100. func (e *realSqlGuard) finish(ctx context.Context, err error) {
  101. duration := timex.Since(e.startTime)
  102. if duration > slowThreshold.Load() {
  103. logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] %s: slowcall - %s", e.command, e.stmt)
  104. metricSlowCount.Inc(e.command)
  105. } else if logSql.True() {
  106. logx.WithContext(ctx).WithDuration(duration).Infof("sql %s: %s", e.command, e.stmt)
  107. }
  108. if err != nil {
  109. logSqlError(ctx, e.stmt, err)
  110. }
  111. metricReqDur.ObserveFloat(float64(duration)/float64(time.Millisecond), e.command)
  112. }
  113. func (e *realSqlGuard) start(q string, args ...any) error {
  114. stmt, err := format(q, args...)
  115. if err != nil {
  116. return err
  117. }
  118. e.stmt = stmt
  119. e.startTime = timex.Now()
  120. return nil
  121. }