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

*: add global variable tidb_slow_log_masking to control masking slow log query (#17637) #17694

Merged
merged 10 commits into from
Jun 6, 2020
9 changes: 7 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,13 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
if (!enable || costTime < threshold) && level > zapcore.DebugLevel {
return
}
sql := FormatSQL(a.Text, sessVars.PreparedParams)
var sql stringutil.StringerFunc
normalizedSQL, digest := sessVars.StmtCtx.SQLDigest()
if sessVars.EnableSlowLogMasking {
sql = FormatSQL(normalizedSQL, nil)
} else {
sql = FormatSQL(a.Text, sessVars.PreparedParams)
}

var tableIDs, indexNames string
if len(sessVars.StmtCtx.TableIDs) > 0 {
Expand All @@ -824,7 +830,6 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
copTaskInfo := sessVars.StmtCtx.CopTasksDetails()
statsInfos := plannercore.GetStatsInfo(a.Plan)
memMax := sessVars.StmtCtx.MemTracker.MaxConsumed()
_, digest := sessVars.StmtCtx.SQLDigest()
_, planDigest := getPlanDigest(a.Ctx, a.Plan)
slowItems := &variable.SlowQueryLogItems{
TxnTS: txnTS,
Expand Down
11 changes: 11 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ func (s *testSuite5) TestSetVar(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "tidb_metric_query_range_duration(9) cannot be smaller than 10 or larger than 216000")
tk.MustQuery("select @@session.tidb_metric_query_range_duration;").Check(testkit.Rows("120"))

// test for tidb_slow_log_masking
tk.MustQuery(`select @@global.tidb_slow_log_masking;`).Check(testkit.Rows("0"))
tk.MustExec("set global tidb_slow_log_masking = 1")
tk.MustQuery(`select @@global.tidb_slow_log_masking;`).Check(testkit.Rows("1"))
tk.MustExec("set global tidb_slow_log_masking = 0")
tk.MustQuery(`select @@global.tidb_slow_log_masking;`).Check(testkit.Rows("0"))
_, err = tk.Exec("set session tidb_slow_log_masking = 0")
c.Assert(err, NotNil)
_, err = tk.Exec(`select @@session.tidb_slow_log_masking;`)
c.Assert(err, NotNil)
}

func (s *testSuite5) TestSetCharset(c *C) {
Expand Down
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,7 @@ var builtinGlobalVariable = []string{
variable.TiDBEvolvePlanBaselines,
variable.TiDBIsolationReadEngines,
variable.TiDBStoreLimit,
variable.TiDBSlowLogMasking,
}

var (
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ type SessionVars struct {

// SelectLimit limits the max counts of select statement's output
SelectLimit uint64

// EnableSlowLogMasking indicates that whether masking the query data when log slow query.
EnableSlowLogMasking bool
}

// PreparedParams contains the parameters of the current prepared statement when executing it.
Expand Down Expand Up @@ -690,6 +693,7 @@ func NewSessionVars() *SessionVars {
PrevFoundInPlanCache: DefTiDBFoundInPlanCache,
FoundInPlanCache: DefTiDBFoundInPlanCache,
SelectLimit: math.MaxUint64,
EnableSlowLogMasking: DefTiDBSlowLogMasking,
}
vars.KVVars = kv.NewVariables(&vars.Killed)
vars.Concurrency = Concurrency{
Expand Down Expand Up @@ -1276,6 +1280,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
return errors.Trace(err)
}
s.SelectLimit = result
case TiDBSlowLogMasking:
s.EnableSlowLogMasking = TiDBOptOn(val)
}
s.systems[name] = val
return nil
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBQueryLogMaxLen, strconv.Itoa(logutil.DefaultQueryLogMaxLen)},
{ScopeSession, TiDBCheckMb4ValueInUTF8, BoolToIntStr(config.GetGlobalConfig().CheckMb4ValueInUTF8)},
{ScopeSession, TiDBFoundInPlanCache, BoolToIntStr(DefTiDBFoundInPlanCache)},
{ScopeGlobal, TiDBSlowLogMasking, BoolToIntStr(DefTiDBSlowLogMasking)},
}

// SynonymsSysVariables is synonyms of system variables.
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ const (

// TiDBMetricSchemaRangeDuration indicates the range duration when query metric schema.
TiDBMetricSchemaRangeDuration = "tidb_metric_query_range_duration"

// TiDBSlowLogMasking indicates that whether masking the query data when log slow query.
TiDBSlowLogMasking = "tidb_slow_log_masking"
)

// Default TiDB system variable values.
Expand Down Expand Up @@ -483,6 +486,7 @@ const (
DefTiDBMetricSchemaStep = 60 // 60s
DefTiDBMetricSchemaRangeDuration = 60 // 60s
DefTiDBFoundInPlanCache = false
DefTiDBSlowLogMasking = false
)

// Process global variables.
Expand Down