From 20c99e7aa07352b599bd0517cd5ca945f4af0407 Mon Sep 17 00:00:00 2001 From: Jarod Watkins Date: Mon, 9 Nov 2020 12:08:19 -0500 Subject: [PATCH] Add unique label validation (#263) * Add unique label validation Originally created as a PR in the [client_golang](https://github.com/prometheus/client_golang/pull/812) repo however was suggested to move it here. Signed-off-by: Jarod Watkins --- expfmt/text_parse.go | 11 +++++++++++ expfmt/text_parse_test.go | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/expfmt/text_parse.go b/expfmt/text_parse.go index 342e5940..b6079b31 100644 --- a/expfmt/text_parse.go +++ b/expfmt/text_parse.go @@ -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 } diff --git a/expfmt/text_parse_test.go b/expfmt/text_parse_test.go index ba23c55d..949bbd64 100644 --- a/expfmt/text_parse_test.go +++ b/expfmt/text_parse_test.go @@ -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 {