Skip to content

Commit

Permalink
*: add a variable tidb_query_log_max_len to set the max length of the…
Browse files Browse the repository at this point in the history
… query string in the log dynamically (#8183) (#8200)
  • Loading branch information
jackysp authored and zz-jason committed Nov 6, 2018
1 parent 89140e2 commit 49b7dc9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Log struct {
SlowQueryFile string `toml:"slow-query-file" json:"slow-query-file"`
SlowThreshold uint64 `toml:"slow-threshold" json:"slow-threshold"`
ExpensiveThreshold uint `toml:"expensive-threshold" json:"expensive-threshold"`
QueryLogMaxLen uint `toml:"query-log-max-len" json:"query-log-max-len"`
QueryLogMaxLen uint64 `toml:"query-log-max-len" json:"query-log-max-len"`
}

// Security is the security section of the config.
Expand Down Expand Up @@ -275,7 +275,7 @@ var defaultConf = Config{
},
SlowThreshold: logutil.DefaultSlowThreshold,
ExpensiveThreshold: 10000,
QueryLogMaxLen: 2048,
QueryLogMaxLen: logutil.DefaultQueryLogMaxLen,
},
Status: Status{
ReportStatus: true,
Expand Down
4 changes: 2 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
return
}
sql := a.Text
if len(sql) > int(cfg.Log.QueryLogMaxLen) {
sql = fmt.Sprintf("%.*q(len:%d)", cfg.Log.QueryLogMaxLen, sql, len(a.Text))
if maxQueryLen := atomic.LoadUint64(&cfg.Log.QueryLogMaxLen); uint64(len(sql)) > maxQueryLen {
sql = fmt.Sprintf("%.*q(len:%d)", maxQueryLen, sql, len(a.Text))
}
sessVars := a.Ctx.GetSessionVars()
sql = QueryReplacer.Replace(sql) + sessVars.GetExecuteArgumentsInfo()
Expand Down
7 changes: 7 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ func (s *testSuite) TestSetVar(c *C) {
tk.MustQuery("select @@session.tidb_slow_log_threshold;").Check(testkit.Rows("1"))
_, err = tk.Exec("set global tidb_slow_log_threshold = 0")
c.Assert(err, NotNil)

tk.MustExec("set tidb_query_log_max_len = 0")
tk.MustQuery("select @@session.tidb_query_log_max_len;").Check(testkit.Rows("0"))
tk.MustExec("set tidb_query_log_max_len = 20")
tk.MustQuery("select @@session.tidb_query_log_max_len;").Check(testkit.Rows("20"))
_, err = tk.Exec("set global tidb_query_log_max_len = 20")
c.Assert(err, NotNil)
}

func (s *testSuite) TestSetCharset(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
atomic.StoreUint32(&ProcessGeneralLog, uint32(tidbOptPositiveInt32(val, DefTiDBGeneralLog)))
case TiDBSlowLogThreshold:
atomic.StoreUint64(&config.GetGlobalConfig().Log.SlowThreshold, uint64(tidbOptInt64(val, logutil.DefaultSlowThreshold)))
case TiDBQueryLogMaxLen:
atomic.StoreUint64(&config.GetGlobalConfig().Log.QueryLogMaxLen, uint64(tidbOptInt64(val, logutil.DefaultQueryLogMaxLen)))
case TiDBRetryLimit:
s.RetryLimit = tidbOptInt64(val, DefTiDBRetryLimit)
case TiDBDisableTxnAutoRetry:
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ var defaultSysVars = []*SysVar{
/* The following variable is defined as session scope but is actually server scope. */
{ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)},
{ScopeSession, TiDBSlowLogThreshold, strconv.Itoa(logutil.DefaultSlowThreshold)},
{ScopeSession, TiDBQueryLogMaxLen, strconv.Itoa(logutil.DefaultQueryLogMaxLen)},
{ScopeSession, TiDBConfig, ""},
{ScopeGlobal | ScopeSession, TiDBDDLReorgWorkerCount, strconv.Itoa(DefTiDBDDLReorgWorkerCount)},
{ScopeSession, TiDBDDLReorgPriority, "PRIORITY_LOW"},
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ const (
// tidb_slow_log_threshold is used to set the slow log threshold in the server.
TiDBSlowLogThreshold = "tidb_slow_log_threshold"

// tidb_query_log_max_len is used to set the max length of the query in the log.
TiDBQueryLogMaxLen = "tidb_query_log_max_len"

// tidb_retry_limit is the maximum number of retries when committing a transaction.
TiDBRetryLimit = "tidb_retry_limit"

Expand Down
6 changes: 5 additions & 1 deletion sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func GetSessionOnlySysVars(s *SessionVars, key string) (string, bool, error) {
return mysql.Priority2Str[mysql.PriorityEnum(atomic.LoadInt32(&ForcePriority))], true, nil
case TiDBSlowLogThreshold:
return strconv.FormatUint(atomic.LoadUint64(&config.GetGlobalConfig().Log.SlowThreshold), 10), true, nil
case TiDBQueryLogMaxLen:
return strconv.FormatUint(atomic.LoadUint64(&config.GetGlobalConfig().Log.QueryLogMaxLen), 10), true, nil
}
sVal, ok := s.systems[key]
if ok {
Expand Down Expand Up @@ -327,7 +329,9 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
TIDBMemQuotaIndexLookupReader,
TIDBMemQuotaIndexLookupJoin,
TIDBMemQuotaNestedLoopApply,
TiDBRetryLimit, TiDBSlowLogThreshold:
TiDBRetryLimit,
TiDBSlowLogThreshold,
TiDBQueryLogMaxLen:
_, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return value, ErrWrongValueForVar.GenWithStackByArgs(name)
Expand Down
2 changes: 2 additions & 0 deletions util/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
defaultLogLevel = log.InfoLevel
// DefaultSlowThreshold is the default slow log threshold in millisecond.
DefaultSlowThreshold = 300
// DefaultQueryLogMaxLen is the default max length of the query in the log.
DefaultQueryLogMaxLen = 2048
)

// FileLogConfig serializes file log related config in toml/json.
Expand Down

0 comments on commit 49b7dc9

Please sign in to comment.