Skip to content

Commit 5ce2371

Browse files
author
yibin
committed
Fix the parse problematic slow log panic issue
Signed-off-by: yibin <[email protected]>
1 parent 14e99ea commit 5ce2371

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pkg/executor/slow_query.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ func splitByColon(line string) (fields []string, values []string) {
594594
fields = append(fields, line[start:current])
595595
parseKey = false
596596
current += 2 // bypass ": "
597+
if current >= lineLength {
598+
// last empty value
599+
values = append(values, "")
600+
}
597601
} else {
598602
start = current
599603
if current < lineLength && (line[current] == '{' || line[current] == '[') {
@@ -607,6 +611,13 @@ func splitByColon(line string) (fields []string, values []string) {
607611
for current < lineLength && line[current] != ' ' {
608612
current++
609613
}
614+
// Meet empty value cases: "Key: Key:"
615+
if current > 0 && line[current-1] == ':' {
616+
values = append(values, "")
617+
current = start
618+
parseKey = true
619+
continue
620+
}
610621
}
611622
values = append(values, line[start:min(current, len(line))])
612623
parseKey = true
@@ -616,6 +627,10 @@ func splitByColon(line string) (fields []string, values []string) {
616627
logutil.BgLogger().Warn("slow query parse slow log error", zap.String("Error", errMsg), zap.String("Log", line))
617628
return nil, nil
618629
}
630+
if len(fields) != len(values) {
631+
logutil.BgLogger().Warn("slow query parse slow log error", zap.Int("field_count", len(fields)), zap.Int("value_count", len(values)), zap.String("Log", line))
632+
return nil, nil
633+
}
619634
return fields, values
620635
}
621636

pkg/executor/slow_query_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ func TestSplitbyColon(t *testing.T) {
540540
{
541541
"123a",
542542
[]string{"123a"},
543-
[]string{},
543+
[]string{""},
544544
},
545545
{
546546
"1a: 2b",
@@ -593,9 +593,16 @@ func TestSplitbyColon(t *testing.T) {
593593
[]string{"Time"},
594594
[]string{"2021-09-08T14:39:54.506967433+08:00"},
595595
},
596+
{
597+
598+
"Cop_proc_avg: 0 Cop_proc_addr: Cop_proc_max: Cop_proc_min: ",
599+
[]string{"Cop_proc_avg", "Cop_proc_addr", "Cop_proc_max", "Cop_proc_min"},
600+
[]string{"0", "", "", ""},
601+
},
596602
}
597603
for _, c := range cases {
598604
resFields, resValues := splitByColon(c.line)
605+
logutil.BgLogger().Info(c.line)
599606
require.Equal(t, c.fields, resFields)
600607
require.Equal(t, c.values, resValues)
601608
}

0 commit comments

Comments
 (0)