Skip to content

Commit

Permalink
*: add global variable tidb_slow_log_masking to control masking slow …
Browse files Browse the repository at this point in the history
…log query (#17637) (#17694)

* cherry pick #17637 to release-4.0

Signed-off-by: sre-bot <sre-bot@pingcap.com>

* fix conflict

Signed-off-by: crazycs <crazycs520@gmail.com>

Co-authored-by: crazycs <crazycs520@gmail.com>
Co-authored-by: Lynn <zimu_xia@126.com>
  • Loading branch information
3 people authored Jun 6, 2020
1 parent 7e64fb6 commit 57c1884
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
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

0 comments on commit 57c1884

Please sign in to comment.