Skip to content

Commit

Permalink
Add unique label validation (#263)
Browse files Browse the repository at this point in the history
* Add unique label validation

Originally created as a PR in the
[client_golang](prometheus/client_golang#812)
repo however was suggested to move it here.

Signed-off-by: Jarod Watkins <jarod@42lines.net>
  • Loading branch information
ipstatic committed Nov 9, 2020
1 parent 317b7b1 commit 20c99e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions expfmt/text_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ func (p *TextParser) startLabelName() stateFn {
p.parseError(fmt.Sprintf("expected '=' after label name, found %q", p.currentByte))
return nil
}
// Check for duplicate label names.
labels := make(map[string]struct{})
for _, l := range p.currentMetric.Label {
lName := l.GetName()
if _, exists := labels[lName]; !exists {
labels[lName] = struct{}{}
} else {
p.parseError(fmt.Sprintf("duplicate label names for metric %q", p.currentMF.GetName()))
return nil
}
}
return p.startLabelValue
}

Expand Down
5 changes: 5 additions & 0 deletions expfmt/text_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ metric{quantile="0x1p-3"} 3.14
`,
err: "text format parsing error in line 3: expected float as value for 'quantile' label",
},
// 33: Check duplicate label.
{
in: `metric{label="bla",label="bla"} 3.14`,
err: "text format parsing error in line 1: duplicate label names for metric",
},
}

for i, scenario := range scenarios {
Expand Down

0 comments on commit 20c99e7

Please sign in to comment.