Skip to content

Commit 9557708

Browse files
crazycs520winkyao
authored andcommitted
log: change slow log time format to compatible with pt-query-digest (pingcap#10290) (pingcap#10323)
1 parent a61225c commit 9557708

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

infoschema/slow_log.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,11 @@ func (st *slowQueryTuple) convertToDatumRow() []types.Datum {
319319
func ParseTime(s string) (time.Time, error) {
320320
t, err := time.Parse(logutil.SlowLogTimeFormat, s)
321321
if err != nil {
322-
err = errors.Errorf("string \"%v\" doesn't has a prefix that matches format \"%v\", err: %v", s, logutil.SlowLogTimeFormat, err)
322+
// This is for compatibility.
323+
t, err = time.Parse(logutil.OldSlowLogTimeFormat, s)
324+
if err != nil {
325+
err = errors.Errorf("string \"%v\" doesn't has a prefix that matches format \"%v\", err: %v", s, logutil.SlowLogTimeFormat, err)
326+
}
323327
}
324328
return t, err
325329
}

infoschema/slow_log_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
func (s *testSuite) TestParseSlowLogFile(c *C) {
2727
slowLog := bytes.NewBufferString(
28-
`# Time: 2019-01-24-22:32:29.313255 +0800
28+
`# Time: 2019-04-28T15:24:04.309074+08:00
2929
# Txn_start_ts: 405888132465033227
3030
# Query_time: 0.216905
3131
# Process_time: 0.021 Request_count: 1 Total_keys: 637 Processed_keys: 436
@@ -51,14 +51,14 @@ select * from t;`)
5151
}
5252
recordString += str
5353
}
54-
expectRecordString := "2019-01-24 22:32:29.313255,405888132465033227,,0,0.216905,0.021,0,0,1,637,0,,,1,42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772,t1:1,t2:2,0.1,0.2,0.03,0.05,0.6,0.8,70724,select * from t;"
54+
expectRecordString := "2019-04-28 15:24:04.309074,405888132465033227,,0,0.216905,0.021,0,0,1,637,0,,,1,42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772,t1:1,t2:2,0.1,0.2,0.03,0.05,0.6,0.8,70724,select * from t;"
5555
c.Assert(expectRecordString, Equals, recordString)
5656

5757
// fix sql contain '# ' bug
5858
slowLog = bytes.NewBufferString(
59-
`# Time: 2019-01-24-22:32:29.313255 +0800
59+
`# Time: 2019-04-28T15:24:04.309074+08:00
6060
select a# from t;
61-
# Time: 2019-01-24-22:32:29.313255 +0800
61+
# Time: 2019-01-24T22:32:29.313255+08:00
6262
# Txn_start_ts: 405888132465033227
6363
# Query_time: 0.216905
6464
# Process_time: 0.021 Request_count: 1 Total_keys: 637 Processed_keys: 436
@@ -70,16 +70,34 @@ select * from t;
7070
scanner = bufio.NewScanner(slowLog)
7171
_, err = infoschema.ParseSlowLog(loc, scanner)
7272
c.Assert(err, IsNil)
73+
74+
// test for time format compatibility.
75+
slowLog = bytes.NewBufferString(
76+
`# Time: 2019-04-28T15:24:04.309074+08:00
77+
select * from t;
78+
# Time: 2019-04-24-19:41:21.716221 +0800
79+
select * from t;
80+
`)
81+
scanner = bufio.NewScanner(slowLog)
82+
rows, err = infoschema.ParseSlowLog(loc, scanner)
83+
c.Assert(err, IsNil)
84+
c.Assert(len(rows) == 2, IsTrue)
85+
t0Str, err := rows[0][0].ToString()
86+
c.Assert(err, IsNil)
87+
c.Assert(t0Str, Equals, "2019-04-28 15:24:04.309074")
88+
t1Str, err := rows[1][0].ToString()
89+
c.Assert(err, IsNil)
90+
c.Assert(t1Str, Equals, "2019-04-24 19:41:21.716221")
7391
}
7492

7593
func (s *testSuite) TestSlowLogParseTime(c *C) {
76-
t1Str := "2019-01-24-22:32:29.313255 +0800"
77-
t2Str := "2019-01-24-22:32:29.313255"
94+
t1Str := "2019-01-24T22:32:29.313255+08:00"
95+
t2Str := "2019-01-24T22:32:29.313255"
7896
t1, err := infoschema.ParseTime(t1Str)
7997
c.Assert(err, IsNil)
8098
loc, err := time.LoadLocation("Asia/Shanghai")
8199
c.Assert(err, IsNil)
82-
t2, err := time.ParseInLocation("2006-01-02-15:04:05.999999999", t2Str, loc)
100+
t2, err := time.ParseInLocation("2006-01-02T15:04:05.999999999", t2Str, loc)
83101
c.Assert(err, IsNil)
84102
c.Assert(t1.Unix(), Equals, t2.Unix())
85103
t1Format := t1.In(loc).Format(logutil.SlowLogTimeFormat)

infoschema/tables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func (s *testSuite) TestSlowQuery(c *C) {
296296
f, err := os.OpenFile(slowLogFileName, os.O_CREATE|os.O_WRONLY, 0644)
297297
c.Assert(err, IsNil)
298298
defer os.Remove(slowLogFileName)
299-
_, err = f.Write([]byte(`# Time: 2019-02-12-19:33:56.571953 +0800
299+
_, err = f.Write([]byte(`# Time: 2019-02-12T19:33:56.571953+08:00
300300
# Txn_start_ts: 406315658548871171
301301
# User: root@127.0.0.1
302302
# Conn_ID: 6

sessionctx/variable/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ const (
804804

805805
// SlowLogFormat uses for formatting slow log.
806806
// The slow log output is like below:
807-
// # Time: 2019-02-12-19:33:56.571953 +0800
807+
// # Time: 2019-04-28T15:24:04.309074+08:00
808808
// # Txn_start_ts: 406315658548871171
809809
// # User: root@127.0.0.1
810810
// # Conn_ID: 6

util/logutil/log.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"runtime"
2323
"sort"
2424
"strings"
25+
"time"
2526

2627
"github.com/pingcap/errors"
2728
zaplog "github.com/pingcap/log"
@@ -208,7 +209,9 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) {
208209

209210
const (
210211
// SlowLogTimeFormat is the time format for slow log.
211-
SlowLogTimeFormat = "2006-01-02-15:04:05.999999999 -0700"
212+
SlowLogTimeFormat = time.RFC3339Nano
213+
// OldSlowLogTimeFormat is the first version of the the time format for slow log, This is use for compatibility.
214+
OldSlowLogTimeFormat = "2006-01-02-15:04:05.999999999 -0700"
212215
)
213216

214217
type slowLogFormatter struct{}

0 commit comments

Comments
 (0)