validate ConstLabels values in NewDesc

This commit is contained in:
Marco Jantke 2017-08-19 23:31:56 +02:00
parent 703c4a9c6f
commit 7ee20d77cb
2 changed files with 24 additions and 0 deletions

View File

@ -122,6 +122,12 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
for _, labelName := range labelNames { for _, labelName := range labelNames {
labelValues = append(labelValues, constLabels[labelName]) labelValues = append(labelValues, constLabels[labelName])
} }
// Validate the const label values. They can't have a wrong cardinality, so
// use in len(labelValues) as expectedNumberOfValues.
if err := validateLabelValues(labelValues, len(labelValues)); err != nil {
d.err = err
return d
}
// Now add the variable label names, but prefix them with something that // Now add the variable label names, but prefix them with something that
// cannot be in a regular label name. That prevents matching the label // cannot be in a regular label name. That prevents matching the label
// dimension with a different mix between preset and variable labels. // dimension with a different mix between preset and variable labels.
@ -137,6 +143,7 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
d.err = errors.New("duplicate label names") d.err = errors.New("duplicate label names")
return d return d
} }
vh := hashNew() vh := hashNew()
for _, val := range labelValues { for _, val := range labelValues {
vh = hashAdd(vh, val) vh = hashAdd(vh, val)

17
prometheus/desc_test.go Normal file
View File

@ -0,0 +1,17 @@
package prometheus
import (
"testing"
)
func TestNewDescInvalidLabelValues(t *testing.T) {
desc := NewDesc(
"sample_label",
"sample label",
nil,
Labels{"a": "\xFF"},
)
if desc.err == nil {
t.Errorf("NewDesc: expected error because: %s", desc.err)
}
}