Skip to content

Commit 20c99e7

Browse files
authored
Add unique label validation (#263)
* 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 <[email protected]>
1 parent 317b7b1 commit 20c99e7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

expfmt/text_parse.go

+11
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ func (p *TextParser) startLabelName() stateFn {
299299
p.parseError(fmt.Sprintf("expected '=' after label name, found %q", p.currentByte))
300300
return nil
301301
}
302+
// Check for duplicate label names.
303+
labels := make(map[string]struct{})
304+
for _, l := range p.currentMetric.Label {
305+
lName := l.GetName()
306+
if _, exists := labels[lName]; !exists {
307+
labels[lName] = struct{}{}
308+
} else {
309+
p.parseError(fmt.Sprintf("duplicate label names for metric %q", p.currentMF.GetName()))
310+
return nil
311+
}
312+
}
302313
return p.startLabelValue
303314
}
304315

expfmt/text_parse_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,11 @@ metric{quantile="0x1p-3"} 3.14
635635
`,
636636
err: "text format parsing error in line 3: expected float as value for 'quantile' label",
637637
},
638+
// 33: Check duplicate label.
639+
{
640+
in: `metric{label="bla",label="bla"} 3.14`,
641+
err: "text format parsing error in line 1: duplicate label names for metric",
642+
},
638643
}
639644

640645
for i, scenario := range scenarios {

0 commit comments

Comments
 (0)