From 49b7dc9c74b4121e674fae626ade26474df4fa81 Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Tue, 6 Nov 2018 17:08:45 +0800 Subject: [PATCH] *: add a variable tidb_query_log_max_len to set the max length of the query string in the log dynamically (#8183) (#8200) --- config/config.go | 4 ++-- executor/adapter.go | 4 ++-- executor/set_test.go | 7 +++++++ sessionctx/variable/session.go | 2 ++ sessionctx/variable/sysvar.go | 1 + sessionctx/variable/tidb_vars.go | 3 +++ sessionctx/variable/varsutil.go | 6 +++++- util/logutil/log.go | 2 ++ 8 files changed, 24 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 5b849a5fef110..e568f92a1133f 100644 --- a/config/config.go +++ b/config/config.go @@ -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. @@ -275,7 +275,7 @@ var defaultConf = Config{ }, SlowThreshold: logutil.DefaultSlowThreshold, ExpensiveThreshold: 10000, - QueryLogMaxLen: 2048, + QueryLogMaxLen: logutil.DefaultQueryLogMaxLen, }, Status: Status{ ReportStatus: true, diff --git a/executor/adapter.go b/executor/adapter.go index 95e76acdaadb1..508ff1f9382f1 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -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() diff --git a/executor/set_test.go b/executor/set_test.go index bd6d0f9dc64cb..dd9b2955e324d 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -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) { diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index 89b1224109469..4ac6b9b978df6 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -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: diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 2ab0c4d9fed9d..69b5a2839ac84 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -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"}, diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 8b1ea4371c3d6..341b8df20e818 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -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" diff --git a/sessionctx/variable/varsutil.go b/sessionctx/variable/varsutil.go index e8766de4fccc4..c13effeebd902 100644 --- a/sessionctx/variable/varsutil.go +++ b/sessionctx/variable/varsutil.go @@ -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 { @@ -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) diff --git a/util/logutil/log.go b/util/logutil/log.go index 610c43b91bcc1..8c74aed9d0737 100644 --- a/util/logutil/log.go +++ b/util/logutil/log.go @@ -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.