Skip to content

Commit

Permalink
session: add session variable to log query string. (#5633)
Browse files Browse the repository at this point in the history
Use an atomic global variable to control the switch to log query.
And the query is logged before execution.
  • Loading branch information
coocood authored and zimulala committed Jan 16, 2018
1 parent 4df3a04 commit 0961907
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
18 changes: 14 additions & 4 deletions session.go
Expand Up @@ -678,7 +678,7 @@ func (s *session) executeStatement(goCtx goctx.Context, connID uint64, stmtNode
} else {
s.ClearValue(context.LastExecuteDDL)
}

logStmt(stmtNode, s.sessionVars)
startTS := time.Now()
recordSet, err := runStmt(goCtx, s, stmt)
if err != nil {
Expand All @@ -692,7 +692,6 @@ func (s *session) executeStatement(goCtx goctx.Context, connID uint64, stmtNode
if recordSet != nil {
recordSets = append(recordSets, recordSet)
}
logCrucialStmt(stmtNode, s.sessionVars.User)
return recordSets, nil
}

Expand Down Expand Up @@ -861,6 +860,7 @@ func (s *session) ExecutePreparedStmt(goCtx goctx.Context, stmtID uint32, args .
if err != nil {
return nil, errors.Trace(err)
}
logQuery(st.OriginText(), s.sessionVars)
r, err := runStmt(goCtx, s, st)
return r, errors.Trace(err)
}
Expand Down Expand Up @@ -1341,16 +1341,26 @@ func (s *session) ShowProcess() util.ProcessInfo {
return pi
}

// logCrucialStmt logs some crucial SQL including: CREATE USER/GRANT PRIVILEGE/CHANGE PASSWORD/DDL etc.
func logCrucialStmt(node ast.StmtNode, user *auth.UserIdentity) {
// logStmt logs some crucial SQL including: CREATE USER/GRANT PRIVILEGE/CHANGE PASSWORD/DDL etc and normal SQL
// if variable.ProcessGeneralLog is set.
func logStmt(node ast.StmtNode, vars *variable.SessionVars) {
switch stmt := node.(type) {
case *ast.CreateUserStmt, *ast.DropUserStmt, *ast.AlterUserStmt, *ast.SetPwdStmt, *ast.GrantStmt,
*ast.RevokeStmt, *ast.AlterTableStmt, *ast.CreateDatabaseStmt, *ast.CreateIndexStmt, *ast.CreateTableStmt,
*ast.DropDatabaseStmt, *ast.DropIndexStmt, *ast.DropTableStmt, *ast.RenameTableStmt, *ast.TruncateTableStmt:
user := vars.User
if ss, ok := node.(ast.SensitiveStmtNode); ok {
log.Infof("[CRUCIAL OPERATION] %s (by %s).", ss.SecureText(), user)
} else {
log.Infof("[CRUCIAL OPERATION] %s (by %s).", stmt.Text(), user)
}
default:
logQuery(node.Text(), vars)
}
}

func logQuery(query string, vars *variable.SessionVars) {
if atomic.LoadUint32(&variable.ProcessGeneralLog) != 0 && !vars.InRestrictedSQL {
log.Infof("[%d] %s", vars.ConnectionID, query)
}
}
2 changes: 2 additions & 0 deletions sessionctx/variable/sysvar.go
Expand Up @@ -620,6 +620,8 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBDMLBatchSize, strconv.Itoa(DefDMLBatchSize)},
{ScopeSession, TiDBCurrentTS, strconv.Itoa(DefCurretTS)},
{ScopeSession, TiDBMaxChunkSize, strconv.Itoa(DefMaxChunkSize)},
/* The following variable is defined as session scope but is actually server scope. */
{ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)},
}

// SetNamesVariables is the system variable names related to set names statements.
Expand Down
9 changes: 9 additions & 0 deletions sessionctx/variable/tidb_vars.go
Expand Up @@ -104,6 +104,9 @@ const (

// tidb_max_chunk_capacity is used to control the max chunk size during query execution.
TiDBMaxChunkSize = "tidb_max_chunk_size"

// tidb_general_log is used to log every query in the server in info level.
TiDBGeneralLog = "tidb_general_log"
)

// Default TiDB system variable values.
Expand All @@ -122,4 +125,10 @@ const (
DefCurretTS = 0
DefMaxChunkSize = 1024
DefDMLBatchSize = 20000
DefTiDBGeneralLog = 0
)

// Process global variables.
var (
ProcessGeneralLog uint32
)
5 changes: 5 additions & 0 deletions sessionctx/varsutil/varsutil.go
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"strconv"
"strings"
"sync/atomic"
"time"

"github.com/juju/errors"
Expand Down Expand Up @@ -53,6 +54,8 @@ func GetSessionOnlySysVars(s *variable.SessionVars, key string) (string, bool, e
switch sysVar.Name {
case variable.TiDBCurrentTS:
return fmt.Sprintf("%d", s.TxnCtx.StartTS), true, nil
case variable.TiDBGeneralLog:
return fmt.Sprintf("%d", atomic.LoadUint32(&variable.ProcessGeneralLog)), true, nil
}
sVal, ok := s.Systems[key]
if ok {
Expand Down Expand Up @@ -168,6 +171,8 @@ func SetSessionSystemVar(vars *variable.SessionVars, name string, value types.Da
return variable.ErrReadOnly
case variable.TiDBMaxChunkSize:
vars.MaxChunkSize = tidbOptPositiveInt(sVal, variable.DefMaxChunkSize)
case variable.TiDBGeneralLog:
atomic.StoreUint32(&variable.ProcessGeneralLog, uint32(tidbOptPositiveInt(sVal, variable.DefTiDBGeneralLog)))
}
vars.Systems[name] = sVal
return nil
Expand Down

0 comments on commit 0961907

Please sign in to comment.