-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
executor, session: replace new line and add user in query log #6748
Changes from 2 commits
be234d3
955cdae
b3fbac0
f59855a
95b2dde
62314b6
784d3dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,6 +324,9 @@ func (a *ExecStmt) buildExecutor(ctx sessionctx.Context) (Executor, error) { | |
return e, nil | ||
} | ||
|
||
// QueryReplacer replaces new line and tab for grep result including query string. | ||
var QueryReplacer = strings.NewReplacer("\r", "", "\n", "", "\t", " ") | ||
|
||
func (a *ExecStmt) logSlowQuery(txnTS uint64, succ bool) { | ||
level := log.GetLevel() | ||
if level < log.WarnLevel { | ||
|
@@ -339,19 +342,27 @@ func (a *ExecStmt) logSlowQuery(txnTS uint64, succ bool) { | |
if len(sql) > int(cfg.Log.QueryLogMaxLen) { | ||
sql = fmt.Sprintf("%.*q(len:%d)", cfg.Log.QueryLogMaxLen, sql, len(a.Text)) | ||
} | ||
connID := a.Ctx.GetSessionVars().ConnectionID | ||
currentDB := a.Ctx.GetSessionVars().CurrentDB | ||
tableIDs := strings.Replace(fmt.Sprintf("%v", a.Ctx.GetSessionVars().StmtCtx.TableIDs), " ", ",", -1) | ||
indexIDs := strings.Replace(fmt.Sprintf("%v", a.Ctx.GetSessionVars().StmtCtx.IndexIDs), " ", ",", -1) | ||
|
||
sql = QueryReplacer.Replace(sql) | ||
|
||
sessVars := a.Ctx.GetSessionVars() | ||
connID := sessVars.ConnectionID | ||
currentDB := sessVars.CurrentDB | ||
var tableIDs, indexIDs string | ||
if len(sessVars.StmtCtx.TableIDs) > 0 { | ||
tableIDs = strings.Replace(fmt.Sprintf("table_ids:%v ", a.Ctx.GetSessionVars().StmtCtx.TableIDs), " ", ",", -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you want to replace spaces with commas? I think it has a certain loss of performance. If it is not necessary, it may not be done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is useful when we need to use ' ' as fields separator. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or should we There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The performance is not critical here, we better use a simpler solution. |
||
} | ||
if len(sessVars.StmtCtx.IndexIDs) > 0 { | ||
indexIDs = strings.Replace(fmt.Sprintf("index_ids:%v ", a.Ctx.GetSessionVars().StmtCtx.IndexIDs), " ", ",", -1) | ||
} | ||
user := a.Ctx.GetSessionVars().User | ||
if costTime < threshold { | ||
logutil.SlowQueryLogger.Debugf( | ||
"[QUERY] cost_time:%v succ:%v connection_id:%v txn_start_ts:%v database:%v table_ids:%v index_ids:%v sql:%v", | ||
costTime, succ, connID, txnTS, currentDB, tableIDs, indexIDs, sql) | ||
"[QUERY] cost_time:%v succ:%v con:%v user:%s txn_start_ts:%v database:%v %v%vsql:%v", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it doesn't matter much. |
||
costTime, succ, connID, user, txnTS, currentDB, tableIDs, indexIDs, sql) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the user contain host-ip? |
||
} else { | ||
logutil.SlowQueryLogger.Warnf( | ||
"[SLOW_QUERY] cost_time:%v succ:%v connection_id:%v txn_start_ts:%v database:%v table_ids:%v index_ids:%v sql:%v", | ||
costTime, succ, connID, txnTS, currentDB, tableIDs, indexIDs, sql) | ||
"[SLOW_QUERY] cost_time:%v succ:%v con:%v user:%s txn_start_ts:%v database:%v %v%vsql:%v", | ||
costTime, succ, connID, user, txnTS, currentDB, tableIDs, indexIDs, sql) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1457,9 +1457,9 @@ func logStmt(node ast.StmtNode, vars *variable.SessionVars) { | |
user := vars.User | ||
schemaVersion := vars.TxnCtx.SchemaVersion | ||
if ss, ok := node.(ast.SensitiveStmtNode); ok { | ||
log.Infof("[CRUCIAL OPERATION] [con:%d][schema ver:%d] %s (by %s).", vars.ConnectionID, schemaVersion, ss.SecureText(), user) | ||
log.Infof("[CRUCIAL OPERATION] con:%d schema_ver:%d %s (by %s).", vars.ConnectionID, schemaVersion, ss.SecureText(), user) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to make the same changes to unify the log format in line 503? |
||
} else { | ||
log.Infof("[CRUCIAL OPERATION] [con:%d][schema ver:%d] %s (by %s).", vars.ConnectionID, schemaVersion, stmt.Text(), user) | ||
log.Infof("[CRUCIAL OPERATION] con:%d schema_ver:%d %s (by %s).", vars.ConnectionID, schemaVersion, stmt.Text(), user) | ||
} | ||
default: | ||
logQuery(node.Text(), vars) | ||
|
@@ -1468,6 +1468,7 @@ func logStmt(node ast.StmtNode, vars *variable.SessionVars) { | |
|
||
func logQuery(query string, vars *variable.SessionVars) { | ||
if atomic.LoadUint32(&variable.ProcessGeneralLog) != 0 && !vars.InRestrictedSQL { | ||
log.Infof("[con:%d][schema ver:%d][txn:%d] %s", vars.ConnectionID, vars.TxnCtx.SchemaVersion, vars.TxnCtx.StartTS, query) | ||
query = executor.QueryReplacer.Replace(query) | ||
log.Infof("[GENERAL_LOG] con:%d user:%s schema_ver:%d start_ts:%d sql:%s", vars.ConnectionID, vars.User, vars.TxnCtx.SchemaVersion, vars.TxnCtx.StartTS, query) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about there is a newline or tab in the string literal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be escaped as three characters `\' '\' 'n' instead of a single character '\n'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query is used for logging, it's ok even if we replace new line in the string.