promparse: sort all labels when parsing (#5372)

* promparse: sort all labels when parsing

Some label names might start with an uppercase letter in which case it
needs to come before __name__. Without this it means that we are not
maintaining the same invariant that all label names should be sorted in
such cases.

Amend the tests to check this problem automatically. Without the change
the tests do not pass.

Signed-off-by: Giedrius Statkevičius <giedriuswork@gmail.com>
This commit is contained in:
Giedrius Statkevičius 2019-03-16 12:10:07 +02:00 committed by Brian Brazil
parent 5f933e99d0
commit 10ec2552af
2 changed files with 7 additions and 3 deletions

View file

@ -227,9 +227,8 @@ func (p *PromParser) Metric(l *labels.Labels) string {
*l = append(*l, labels.Label{Name: s[a:b], Value: s[c:d]}) *l = append(*l, labels.Label{Name: s[a:b], Value: s[c:d]})
} }
// Sort labels. We can skip the first entry since the metric name is // Sort labels to maintain the sorted labels invariant.
// already at the right place. sort.Sort(*l)
sort.Sort((*l)[1:])
return s return s
} }

View file

@ -37,6 +37,7 @@ go_gc_duration_seconds{quantile="0.8", a="b"} 8.3835e-05
go_gc_duration_seconds{ quantile="0.9", a="b"} 8.3835e-05 go_gc_duration_seconds{ quantile="0.9", a="b"} 8.3835e-05
# Hrandom comment starting with prefix of HELP # Hrandom comment starting with prefix of HELP
# #
wind_speed{A="2",c="3"} 12345
# comment with escaped \n newline # comment with escaped \n newline
# comment with escaped \ escape character # comment with escaped \ escape character
# HELP nohelp1 # HELP nohelp1
@ -97,6 +98,10 @@ testmetric{label="\"bar\""} 1`
comment: "# Hrandom comment starting with prefix of HELP", comment: "# Hrandom comment starting with prefix of HELP",
}, { }, {
comment: "#", comment: "#",
}, {
m: `wind_speed{A="2",c="3"}`,
v: 12345,
lset: labels.FromStrings("A", "2", "__name__", "wind_speed", "c", "3"),
}, { }, {
comment: "# comment with escaped \\n newline", comment: "# comment with escaped \\n newline",
}, { }, {