Skip to content

Commit c1e4d3b

Browse files
author
Bobby
committed
Tolerate trailing whitespace in text parser
Signed-off-by: Bobby <boby@codelabs.co.id>
1 parent 2120573 commit c1e4d3b

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

expfmt/text_parse.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,21 @@ func (p *TextParser) readingValue() stateFn {
602602
if p.currentByte == '\n' {
603603
return p.startOfLine
604604
}
605-
return p.startTimestamp
605+
if isBlankOrTab(p.currentByte) {
606+
p.skipBlankTabIfCurrentBlankTab()
607+
if p.err != nil {
608+
return nil // Unexpected end of input.
609+
}
610+
if p.currentByte == '\n' {
611+
return p.startOfLine
612+
}
613+
}
614+
return p.readingTimestamp
606615
}
607616

608-
// startTimestamp represents the state where the next byte read from p.buf is
609-
// the start of the timestamp (or whitespace leading up to it).
610-
func (p *TextParser) startTimestamp() stateFn {
611-
if p.skipBlankTab(); p.err != nil {
612-
return nil // Unexpected end of input.
613-
}
617+
// readingTimestamp represents the state where the last byte read (now in
618+
// p.currentByte) is the first byte of the timestamp.
619+
func (p *TextParser) readingTimestamp() stateFn {
614620
if p.readTokenUntilWhitespace(); p.err != nil {
615621
return nil // Unexpected end of input.
616622
}
@@ -621,6 +627,18 @@ func (p *TextParser) startTimestamp() stateFn {
621627
return nil
622628
}
623629
p.currentMetric.TimestampMs = proto.Int64(timestamp)
630+
if p.currentByte == '\n' {
631+
return p.startOfLine
632+
}
633+
if isBlankOrTab(p.currentByte) {
634+
p.skipBlankTabIfCurrentBlankTab()
635+
if p.err != nil {
636+
return nil // Unexpected end of input.
637+
}
638+
if p.currentByte == '\n' {
639+
return p.startOfLine
640+
}
641+
}
624642
if p.readTokenUntilNewline(false); p.err != nil {
625643
return nil // Unexpected end of input.
626644
}

expfmt/text_parse_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,35 @@ request_duration_microseconds_count 2693
957957
},
958958
},
959959
},
960+
// 18: Trailing blanks and tabs after sample values and timestamps.
961+
{
962+
in: "trailing_value_space 1 \t \ntrailing_timestamp_space 2 123 \t \n",
963+
out: []*dto.MetricFamily{
964+
{
965+
Name: proto.String("trailing_value_space"),
966+
Type: dto.MetricType_UNTYPED.Enum(),
967+
Metric: []*dto.Metric{
968+
{
969+
Untyped: &dto.Untyped{
970+
Value: proto.Float64(1),
971+
},
972+
},
973+
},
974+
},
975+
{
976+
Name: proto.String("trailing_timestamp_space"),
977+
Type: dto.MetricType_UNTYPED.Enum(),
978+
Metric: []*dto.Metric{
979+
{
980+
Untyped: &dto.Untyped{
981+
Value: proto.Float64(2),
982+
},
983+
TimestampMs: proto.Int64(123),
984+
},
985+
},
986+
},
987+
},
988+
},
960989
}
961990

962991
for i, scenario := range scenarios {

0 commit comments

Comments
 (0)