Skip to content

Commit

Permalink
extfmt: allow trailing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ledmonster committed Jan 12, 2024
1 parent 21a37ca commit 0c4b9a5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions expfmt/text_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ func (p *TextParser) startTimestamp() stateFn {
if p.readTokenUntilWhitespace(); p.err != nil {
return nil // Unexpected end of input.
}
if p.currentToken.String() == "" {
return p.startOfLine
}
timestamp, err := strconv.ParseInt(p.currentToken.String(), 10, 64)
if err != nil {
// Create a more helpful error message.
Expand All @@ -488,7 +491,7 @@ func (p *TextParser) startTimestamp() stateFn {
if p.readTokenUntilNewline(false); p.err != nil {
return nil // Unexpected end of input.
}
if p.currentToken.Len() > 0 {
if strings.TrimSpace(p.currentToken.String()) != "" {
p.parseError(fmt.Sprintf("spurious string after timestamp: %q", p.currentToken.String()))
return nil
}
Expand Down Expand Up @@ -521,7 +524,7 @@ func (p *TextParser) readingType() stateFn {
if p.readTokenUntilNewline(false); p.err != nil {
return nil // Unexpected end of input.
}
metricType, ok := dto.MetricType_value[strings.ToUpper(p.currentToken.String())]
metricType, ok := dto.MetricType_value[strings.ToUpper(strings.TrimSpace(p.currentToken.String()))]
if !ok {
p.parseError(fmt.Sprintf("unknown metric type %q", p.currentToken.String()))
return nil
Expand Down
39 changes: 39 additions & 0 deletions expfmt/text_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,45 @@ request_duration_microseconds_count 2693
},
},
},
// 5: Trailing white space in TYPE declaration.
{
in: `
# TYPE name counter
name {labelname="val1"} 1
name {labelname="val2"} 0.23 1234567890
`,
out: []*dto.MetricFamily{
{
Name: proto.String("name"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Label: []*dto.LabelPair{
{
Name: proto.String("labelname"),
Value: proto.String("val1"),
},
},
Counter: &dto.Counter{
Value: proto.Float64(1),
},
},
{
Label: []*dto.LabelPair{
{
Name: proto.String("labelname"),
Value: proto.String("val2"),
},
},
Counter: &dto.Counter{
Value: proto.Float64(0.23),
},
TimestampMs: proto.Int64(1234567890),
},
},
},
},
},
}

for i, scenario := range scenarios {
Expand Down

0 comments on commit 0c4b9a5

Please sign in to comment.