Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stmt support sql custom mask handle #4213

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions core/stores/sqlx/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import (

"github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/rescue"
"github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/timex"
)

const defaultSlowThreshold = time.Millisecond * 500

var (
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
logSql = syncx.ForAtomicBool(true)
logSlowSql = syncx.ForAtomicBool(true)
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
logSql = syncx.ForAtomicBool(true)
logSlowSql = syncx.ForAtomicBool(true)
maskFn MaskFn = nil
)

type (
MaskFn func(sql string) string
// StmtSession interface represents a session that can be used to execute statements.
StmtSession interface {
Close() error
Expand Down Expand Up @@ -170,6 +173,10 @@ func DisableStmtLog() {
logSql.Set(false)
}

func SetMaskFn(fn MaskFn) {
maskFn = fn
}

// SetSlowThreshold sets the slow threshold.
func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold)
Expand Down Expand Up @@ -265,17 +272,32 @@ func (n nilGuard) start(_ string, _ ...any) error {
func (n nilGuard) finish(_ context.Context, _ error) {
}

func (e *realSqlGuard) mask(ctx context.Context, stmt string) string {
defer rescue.RecoverCtx(ctx)

return maskFn(stmt)
}

func (e *realSqlGuard) finish(ctx context.Context, err error) {
duration := timex.Since(e.startTime)

stmt := e.stmt
if maskFn != nil {
stmt = e.mask(ctx, e.stmt)
if len(stmt) == 0 {
stmt = e.stmt
}
}

if duration > slowThreshold.Load() {
logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] %s: slowcall - %s", e.command, e.stmt)
logx.WithContext(ctx).WithDuration(duration).Slowf("[SQL] %s: slowcall - %s", e.command, stmt)
metricSlowCount.Inc(e.command)
} else if logSql.True() {
logx.WithContext(ctx).WithDuration(duration).Infof("sql %s: %s", e.command, e.stmt)
logx.WithContext(ctx).WithDuration(duration).Infof("sql %s: %s", e.command, stmt)
}

if err != nil {
logSqlError(ctx, e.stmt, err)
logSqlError(ctx, stmt, err)
}

metricReqDur.ObserveFloat(float64(duration)/float64(time.Millisecond), e.command)
Expand Down
10 changes: 10 additions & 0 deletions core/stores/sqlx/stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ func TestSetSlowThreshold(t *testing.T) {
assert.Equal(t, time.Second, slowThreshold.Load())
}

func TestSetMaskFn(t *testing.T) {
logSql.Set(true)
logSlowSql.Set(true)
SetMaskFn(func(sql string) string {
return sql
})
guard := newGuard("any")
guard.finish(context.Background(), nil)
}

func TestDisableLog(t *testing.T) {
assert.True(t, logSql.True())
assert.True(t, logSlowSql.True())
Expand Down