Skip to content
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

slowlog: slowlog with keyspace #41886

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ go_library(
"//expression",
"//expression/aggregation",
"//infoschema",
"//keyspace",
"//kv",
"//meta",
"//meta/autoid",
Expand Down
12 changes: 12 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/keyspace"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser"
Expand Down Expand Up @@ -1631,8 +1632,19 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {

resultRows := GetResultRowsCount(stmtCtx, a.Plan)

var (
keyspaceName string
keyspaceID uint32
)
keyspaceName = keyspace.GetKeyspaceNameBySettings()
if !keyspace.IsKeyspaceNameEmpty(keyspaceName) {
keyspaceID = uint32(a.Ctx.GetStore().GetCodec().GetKeyspaceID())
}

slowItems := &variable.SlowQueryLogItems{
TxnTS: txnTS,
KeyspaceName: keyspaceName,
KeyspaceID: keyspaceID,
SQL: sql.String(),
Digest: digest.String(),
TimeTotal: costTime,
Expand Down
13 changes: 13 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,10 @@ const (
SlowLogStartPrefixStr = SlowLogRowPrefixStr + SlowLogTimeStr + SlowLogSpaceMarkStr
// SlowLogTxnStartTSStr is slow log field name.
SlowLogTxnStartTSStr = "Txn_start_ts"
// SlowLogKeyspaceName is slow log field name.
SlowLogKeyspaceName = "Keyspace_name"
// SlowLogKeyspaceID is slow log field name.
SlowLogKeyspaceID = "Keyspace_ID"
// SlowLogUserAndHostStr is the user and host field name, which is compatible with MySQL.
SlowLogUserAndHostStr = "User@Host"
// SlowLogUserStr is slow log field name.
Expand Down Expand Up @@ -2833,6 +2837,8 @@ type JSONSQLWarnForSlowLog struct {
// slow query log.
type SlowQueryLogItems struct {
TxnTS uint64
KeyspaceName string
KeyspaceID uint32
SQL string
Digest string
TimeTotal time.Duration
Expand Down Expand Up @@ -2875,6 +2881,8 @@ type SlowQueryLogItems struct {
// The slow log output is like below:
// # Time: 2019-04-28T15:24:04.309074+08:00
// # Txn_start_ts: 406315658548871171
// # Keyspace_name: keyspace_a
// # Keyspace_ID: 1
// # User@Host: root[root] @ localhost [127.0.0.1]
// # Conn_ID: 6
// # Query_time: 4.895492
Expand All @@ -2896,6 +2904,11 @@ func (s *SessionVars) SlowLogFormat(logItems *SlowQueryLogItems) string {
var buf bytes.Buffer

writeSlowLogItem(&buf, SlowLogTxnStartTSStr, strconv.FormatUint(logItems.TxnTS, 10))
if logItems.KeyspaceName != "" {
writeSlowLogItem(&buf, SlowLogKeyspaceName, logItems.KeyspaceName)
writeSlowLogItem(&buf, SlowLogKeyspaceID, fmt.Sprintf("%d", logItems.KeyspaceID))
}

if s.User != nil {
hostAddress := s.User.Hostname
if s.ConnectionInfo != nil {
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func TestSlowLogFormat(t *testing.T) {
var memMax int64 = 2333
var diskMax int64 = 6666
resultFields := `# Txn_start_ts: 406649736972468225
# Keyspace_name: keyspace_a
# Keyspace_ID: 1
# User@Host: root[root] @ 192.168.0.1 [192.168.0.1]
# Conn_ID: 1
# Exec_retry_time: 5.1 Exec_retry_count: 3
Expand Down Expand Up @@ -247,6 +249,8 @@ func TestSlowLogFormat(t *testing.T) {
_, digest := parser.NormalizeDigest(sql)
logItems := &variable.SlowQueryLogItems{
TxnTS: txnTS,
KeyspaceName: "keyspace_a",
KeyspaceID: 1,
SQL: sql,
Digest: digest.String(),
TimeTotal: costTime,
Expand Down