Skip to content

Commit

Permalink
Fix multiple metric name inside braces validation
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Torres <[email protected]>
  • Loading branch information
fedetorres93 committed Sep 5, 2024
1 parent 4f8e8f4 commit a9eef7c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
18 changes: 13 additions & 5 deletions expfmt/text_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ type TextParser struct {
// These tell us if the currently processed line ends on '_count' or
// '_sum' respectively and belong to a summary/histogram, representing the sample
// count and sum of that summary/histogram.
currentIsSummaryCount, currentIsSummarySum bool
currentIsHistogramCount, currentIsHistogramSum bool
currentMetricIsInsideBraces bool
currentIsSummaryCount, currentIsSummarySum bool
currentIsHistogramCount, currentIsHistogramSum bool
currentMetricIsInsideBraces, currentMetricInsideBracesIsPresent bool
}

// TextToMetricFamilies reads 'in' as the simple and flat text-based exchange
Expand Down Expand Up @@ -147,6 +147,7 @@ func (p *TextParser) reset(in io.Reader) {
func (p *TextParser) startOfLine() stateFn {
p.lineCount++
p.currentMetricIsInsideBraces = false
p.currentMetricInsideBracesIsPresent = false
if p.skipBlankTab(); p.err != nil {
// This is the only place that we expect to see io.EOF,
// which is not an error but the signal that we are done.
Expand Down Expand Up @@ -301,17 +302,24 @@ func (p *TextParser) startLabelName() stateFn {
}
if p.currentByte != '=' {
if p.currentMetricIsInsideBraces {
if p.currentMF != nil && p.currentMF.GetName() != p.currentToken.String() {
p.parseError(fmt.Sprintf("multiple metric names %s %s", p.currentMF.GetName(), p.currentToken.String()))
if p.currentMetricInsideBracesIsPresent {
p.parseError(fmt.Sprintf("multiple metric names for metric %q", p.currentMF.GetName()))
return nil
}
switch p.currentByte {
case ',':
p.setOrCreateCurrentMF()
if p.currentMF.Type == nil {
p.currentMF.Type = dto.MetricType_UNTYPED.Enum()
}
p.currentMetric = &dto.Metric{}
p.currentMetricInsideBracesIsPresent = true
return p.startLabelName
case '}':
p.setOrCreateCurrentMF()
if p.currentMF.Type == nil {
p.currentMF.Type = dto.MetricType_UNTYPED.Enum()
}
p.currentMetric = &dto.Metric{}
p.currentMetric.Label = append(p.currentMetric.Label, p.currentLabelPairs...)
p.currentLabelPairs = nil
Expand Down
43 changes: 43 additions & 0 deletions expfmt/text_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,49 @@ request_duration_microseconds_count 2693
},
},
},
// 11: Multiple minimal metrics with quoted metric names.
{
in: `
{"name.1"} 1
{"name.2"} 1
{"name.3"} 1
`,
out: []*dto.MetricFamily{
{
Name: proto.String("name.1"),
Type: dto.MetricType_UNTYPED.Enum(),
Metric: []*dto.Metric{
{
Untyped: &dto.Untyped{
Value: proto.Float64(1),
},
},
},
},
{
Name: proto.String("name.2"),
Type: dto.MetricType_UNTYPED.Enum(),
Metric: []*dto.Metric{
{
Untyped: &dto.Untyped{
Value: proto.Float64(1),
},
},
},
},
{
Name: proto.String("name.3"),
Type: dto.MetricType_UNTYPED.Enum(),
Metric: []*dto.Metric{
{
Untyped: &dto.Untyped{
Value: proto.Float64(1),
},
},
},
},
},
},
}

for i, scenario := range scenarios {
Expand Down

0 comments on commit a9eef7c

Please sign in to comment.