Remove local REs for label and metric names and use fast checks
All was a mess, we had duplicates of the REs for label name and metric names here, and we sometimes used them, sometimes we used those from common/model. Now we are consistently using the fast checking functions from common/model. (Tests for leading colons are included there, see https://github.com/prometheus/common/pull/66 .)
This commit is contained in:
parent
e56081f7b9
commit
dcbc5caa16
|
@ -16,20 +16,15 @@ package prometheus
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
"github.com/prometheus/common/model"
|
||||||
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
metricNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_:]*$`)
|
|
||||||
labelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
|
|
||||||
)
|
|
||||||
|
|
||||||
// reservedLabelPrefix is a prefix which is not legal in user-supplied
|
// reservedLabelPrefix is a prefix which is not legal in user-supplied
|
||||||
// label names.
|
// label names.
|
||||||
const reservedLabelPrefix = "__"
|
const reservedLabelPrefix = "__"
|
||||||
|
@ -103,7 +98,7 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
|
||||||
d.err = errors.New("empty help string")
|
d.err = errors.New("empty help string")
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
if !metricNameRE.MatchString(fqName) {
|
if !model.IsValidMetricName(model.LabelValue(fqName)) {
|
||||||
d.err = fmt.Errorf("%q is not a valid metric name", fqName)
|
d.err = fmt.Errorf("%q is not a valid metric name", fqName)
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
@ -200,6 +195,6 @@ func (d *Desc) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkLabelName(l string) bool {
|
func checkLabelName(l string) bool {
|
||||||
return labelNameRE.MatchString(l) &&
|
return model.LabelName(l).IsValid() &&
|
||||||
!strings.HasPrefix(l, reservedLabelPrefix)
|
!strings.HasPrefix(l, reservedLabelPrefix)
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ func push(job string, grouping map[string]string, pushURL string, g prometheus.G
|
||||||
}
|
}
|
||||||
urlComponents := []string{url.QueryEscape(job)}
|
urlComponents := []string{url.QueryEscape(job)}
|
||||||
for ln, lv := range grouping {
|
for ln, lv := range grouping {
|
||||||
if !model.LabelNameRE.MatchString(ln) {
|
if !model.LabelName(ln).IsValid() {
|
||||||
return fmt.Errorf("grouping label has invalid name: %s", ln)
|
return fmt.Errorf("grouping label has invalid name: %s", ln)
|
||||||
}
|
}
|
||||||
if strings.Contains(lv, "/") {
|
if strings.Contains(lv, "/") {
|
||||||
|
|
Loading…
Reference in New Issue