Skip to content

Commit 79e0df8

Browse files
author
昝杰
authored
Merge branch 'master' into master
2 parents d549e74 + 3e7f9d3 commit 79e0df8

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

log/slow/parser.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var (
5454
headerRe = regexp.MustCompile(`^#\s+[A-Z]`)
5555
metricsRe = regexp.MustCompile(`(\w+): (\S+|\z)`)
5656
adminRe = regexp.MustCompile(`command: (.+)`)
57-
setRe = regexp.MustCompile(`^SET (?:last_insert_id|insert_id|timestamp)`)
57+
setRe = regexp.MustCompile(`^SET (last_insert_id|insert_id|timestamp)\s*=\s*(.*?)\s*;`)
5858
useRe = regexp.MustCompile(`^(?i)use `)
5959
)
6060

@@ -343,11 +343,26 @@ func (p *SlowLogParser) parseQuery(line string) {
343343
// In case we are on a group of lines like in test23, lines 27~28, the
344344
// query will be "use dbnameb" since the user executed a use command
345345
p.event.Query = line
346-
} else if setRe.MatchString(line) {
346+
} else if m := setRe.FindAllStringSubmatch(line, -1); len(m) > 0 {
347+
var name, val string
348+
if len(m[0]) > 2 {
349+
name = m[0][1]
350+
val = m[0][2]
351+
}
352+
347353
if p.opt.Debug {
348-
l.Println("set var")
354+
l.Println("set var", name)
355+
}
356+
357+
switch name {
358+
// @todo: use other params that are being set
359+
case "timestamp":
360+
t, err := strconv.ParseInt(val, 10, 64)
361+
if err == nil && p.event.Ts.IsZero() {
362+
p.event.Ts = time.Unix(t, 0)
363+
}
349364
}
350-
// @todo ignore or use these lines?
365+
351366
} else {
352367
if p.opt.Debug {
353368
l.Println("query")

log/slow/parser_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func TestParseSlowLog002(t *testing.T) {
166166
Host: "",
167167
Offset: 337,
168168
OffsetEnd: 813,
169+
Ts: time.Unix(1197996507, 0),
169170
TimeMetrics: map[string]float64{
170171
"Query_time": 0.726052,
171172
"Lock_time": 0.000091,
@@ -194,6 +195,7 @@ VALUES ('', 'Exact')`,
194195
Host: "",
195196
Offset: 813,
196197
OffsetEnd: 1332,
198+
Ts: time.Unix(1197996507, 0),
197199
TimeMetrics: map[string]float64{
198200
"InnoDB_queue_wait": 0.000000,
199201
"Lock_time": 0.000077,
@@ -332,6 +334,7 @@ WHERE fillze='899'`,
332334
Host: "",
333335
Offset: 2859,
334336
OffsetEnd: 3372,
337+
Ts: time.Unix(1197996508, 0),
335338
TimeMetrics: map[string]float64{
336339
"Query_time": 0.000530,
337340
"InnoDB_IO_r_wait": 0.000000,
@@ -880,6 +883,7 @@ func TestParserSlowLog011(t *testing.T) {
880883
User: "user1",
881884
RateType: "query",
882885
RateLimit: 2,
886+
Ts: time.Unix(1385600731, 0),
883887
TimeMetrics: map[string]float64{
884888
"Query_time": 0.000237,
885889
"Lock_time": 0.000122,
@@ -922,6 +926,7 @@ func TestParserSlowLog011(t *testing.T) {
922926
User: "user1",
923927
RateType: "query",
924928
RateLimit: 2,
929+
Ts: time.Unix(1385600731, 0),
925930
TimeMetrics: map[string]float64{
926931
"Query_time": 0.000165,
927932
"Lock_time": 0.000048,
@@ -969,6 +974,7 @@ func TestParserSlowLog012(t *testing.T) {
969974
User: "msandbox",
970975
Offset: 0,
971976
OffsetEnd: 185,
977+
Ts: time.Unix(1397442852, 0),
972978
TimeMetrics: map[string]float64{
973979
"Query_time": 0.000214,
974980
"Lock_time": 0.000086,
@@ -987,6 +993,7 @@ func TestParserSlowLog012(t *testing.T) {
987993
User: "msandbox",
988994
Offset: 185,
989995
OffsetEnd: 375,
996+
Ts: time.Unix(1397442852, 0),
990997
TimeMetrics: map[string]float64{
991998
"Query_time": 0.000016,
992999
"Lock_time": 0.000000,
@@ -1149,6 +1156,7 @@ func TestParserSlowLog014(t *testing.T) {
11491156
User: "root",
11501157
Host: "localhost",
11511158
Db: "db1",
1159+
Ts: time.Unix(1398555955, 0),
11521160
TimeMetrics: map[string]float64{
11531161
"InnoDB_IO_r_wait": 0,
11541162
"InnoDB_queue_wait": 0,
@@ -1195,6 +1203,7 @@ func TestParserSlowLog014(t *testing.T) {
11951203
User: "root",
11961204
Host: "localhost",
11971205
Db: "db1",
1206+
Ts: time.Unix(1398555955, 0),
11981207
TimeMetrics: map[string]float64{
11991208
"InnoDB_IO_r_wait": 0,
12001209
"InnoDB_queue_wait": 0,
@@ -1237,6 +1246,7 @@ func TestParserSlowLog014(t *testing.T) {
12371246
User: "root",
12381247
Host: "localhost",
12391248
Db: "db1",
1249+
Ts: time.Unix(1398555955, 0),
12401250
TimeMetrics: map[string]float64{
12411251
"InnoDB_IO_r_wait": 0,
12421252
"InnoDB_queue_wait": 0,
@@ -1279,6 +1289,7 @@ func TestParserSlowLog014(t *testing.T) {
12791289
User: "root",
12801290
Host: "localhost",
12811291
Db: "db1",
1292+
Ts: time.Unix(1398555955, 0),
12821293
TimeMetrics: map[string]float64{
12831294
"InnoDB_IO_r_wait": 0,
12841295
"InnoDB_queue_wait": 0,
@@ -1364,6 +1375,7 @@ func TestParseSlow016(t *testing.T) {
13641375
Host: "localhost",
13651376
Offset: 159,
13661377
OffsetEnd: 413,
1378+
Ts: time.Unix(1400193480, 0),
13671379
TimeMetrics: map[string]float64{
13681380
"Query_time": 0.003953,
13691381
"Lock_time": 0.000059,
@@ -1391,6 +1403,7 @@ func TestParseSlow017(t *testing.T) {
13911403
Host: "localhost",
13921404
Offset: 26,
13931405
OffsetEnd: 280,
1406+
Ts: time.Unix(1400193480, 0),
13941407
TimeMetrics: map[string]float64{
13951408
"Query_time": 0.003953,
13961409
"Lock_time": 0.000059,
@@ -1417,6 +1430,7 @@ func TestParseSlow019(t *testing.T) {
14171430
Host: "localhost",
14181431
Offset: 0,
14191432
OffsetEnd: 641,
1433+
Ts: time.Unix(1413583349, 0),
14201434
TimeMetrics: map[string]float64{
14211435
"Lock_time": 0.0001,
14221436
"Query_time": 0.004599,
@@ -1452,6 +1466,7 @@ func TestParseSlow019(t *testing.T) {
14521466
Offset: 641,
14531467
OffsetEnd: 1273,
14541468
Db: "cod7_plos15",
1469+
Ts: time.Unix(1413583349, 0),
14551470
TimeMetrics: map[string]float64{
14561471
"Lock_time": 0,
14571472
"Query_time": 2.2e-05,
@@ -1487,6 +1502,7 @@ func TestParseSlow019(t *testing.T) {
14871502
Offset: 1273,
14881503
OffsetEnd: 2005,
14891504
Db: "cod7_plos15",
1505+
Ts: time.Unix(1413583349, 0),
14901506
TimeMetrics: map[string]float64{
14911507
"InnoDB_IO_r_wait": 0,
14921508
"InnoDB_queue_wait": 0,
@@ -1528,6 +1544,7 @@ func TestParseSlow019(t *testing.T) {
15281544
Host: "localhost",
15291545
Offset: 2005,
15301546
OffsetEnd: 2621,
1547+
Ts: time.Unix(1413583349, 0),
15311548
TimeMetrics: map[string]float64{
15321549
"Lock_time": 0.000115,
15331550
"Query_time": 0.011565,
@@ -1573,6 +1590,7 @@ func TestParseSlow023(t *testing.T) {
15731590
User: "bookblogs",
15741591
Host: "localhost",
15751592
Db: "dbnamea",
1593+
Ts: time.Unix(1415210700, 0),
15761594
TimeMetrics: map[string]float64{
15771595
"Query_time": 0.321092,
15781596
"Lock_time": 3.8e-05,
@@ -1594,6 +1612,7 @@ func TestParseSlow023(t *testing.T) {
15941612
User: "bookblogs",
15951613
Host: "localhost",
15961614
Db: "",
1615+
Ts: time.Unix(1415210700, 0),
15971616
TimeMetrics: map[string]float64{
15981617
"Lock_time": 0,
15991618
"Query_time": 0.253052,
@@ -1615,6 +1634,7 @@ func TestParseSlow023(t *testing.T) {
16151634
User: "percona-agent",
16161635
Host: "localhost",
16171636
Db: "",
1637+
Ts: time.Unix(1415210700, 0),
16181638
TimeMetrics: map[string]float64{
16191639
"Query_time": 0.31645,
16201640
"Lock_time": 0,
@@ -1636,6 +1656,7 @@ func TestParseSlow023(t *testing.T) {
16361656
User: "bookblogs",
16371657
Host: "localhost",
16381658
Db: "",
1659+
Ts: time.Unix(1415210700, 0),
16391660
TimeMetrics: map[string]float64{
16401661
"Query_time": 3.7e-05,
16411662
"Lock_time": 0,
@@ -1657,6 +1678,7 @@ func TestParseSlow023(t *testing.T) {
16571678
User: "bookblogs",
16581679
Host: "localhost",
16591680
Db: "",
1681+
Ts: time.Unix(1415210700, 0),
16601682
TimeMetrics: map[string]float64{
16611683
"Query_time": 0.000297,
16621684
"Lock_time": 0.000141,
@@ -1677,6 +1699,7 @@ func TestParseSlow023(t *testing.T) {
16771699
User: "backup",
16781700
Host: "localhost",
16791701
Db: "dbnameb",
1702+
Ts: time.Unix(1415210700, 0),
16801703
TimeMetrics: map[string]float64{
16811704
"Lock_time": 0,
16821705
"Query_time": 0.000558,
@@ -1698,6 +1721,7 @@ func TestParseSlow023(t *testing.T) {
16981721
User: "backup",
16991722
Host: "localhost",
17001723
Db: "",
1724+
Ts: time.Unix(1415210700, 0),
17011725
TimeMetrics: map[string]float64{
17021726
"Query_time": 0.000204,
17031727
"Lock_time": 0,
@@ -1719,6 +1743,7 @@ func TestParseSlow023(t *testing.T) {
17191743
User: "bookblogs",
17201744
Host: "localhost",
17211745
Db: "",
1746+
Ts: time.Unix(1415210700, 0),
17221747
TimeMetrics: map[string]float64{
17231748
"Query_time": 0.000164,
17241749
"Lock_time": 5.9e-05,

0 commit comments

Comments
 (0)