2018-08-22 13:08:52 +03:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-08-25 15:50:43 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Labels represents a collection of label name -> value mappings. This type is
|
|
|
|
// commonly used with the With(Labels) and GetMetricWith(Labels) methods of
|
|
|
|
// metric vector Collectors, e.g.:
|
2022-11-08 02:14:19 +03:00
|
|
|
//
|
|
|
|
// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
|
2017-08-25 15:50:43 +03:00
|
|
|
//
|
|
|
|
// The other use-case is the specification of constant label pairs in Opts or to
|
|
|
|
// create a Desc.
|
|
|
|
type Labels map[string]string
|
|
|
|
|
2023-06-27 14:21:36 +03:00
|
|
|
// LabelConstraint normalizes label values.
|
|
|
|
type LabelConstraint func(string) string
|
|
|
|
|
2022-12-13 15:47:52 +03:00
|
|
|
// ConstrainedLabels represents a label name and its constrain function
|
|
|
|
// to normalize label values. This type is commonly used when constructing
|
|
|
|
// metric vector Collectors.
|
|
|
|
type ConstrainedLabel struct {
|
|
|
|
Name string
|
2023-06-27 14:21:36 +03:00
|
|
|
Constraint LabelConstraint
|
2022-12-13 15:47:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConstrainableLabels is an interface that allows creating of labels that can
|
|
|
|
// be optionally constrained.
|
|
|
|
//
|
|
|
|
// prometheus.V2().NewCounterVec(CounterVecOpts{
|
|
|
|
// CounterOpts: {...}, // Usual CounterOpts fields
|
|
|
|
// VariableLabels: []ConstrainedLabels{
|
|
|
|
// {Name: "A"},
|
|
|
|
// {Name: "B", Constraint: func(v string) string { ... }},
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
type ConstrainableLabels interface {
|
2023-06-27 14:21:36 +03:00
|
|
|
compile() *compiledLabels
|
2022-12-13 15:47:52 +03:00
|
|
|
labelNames() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConstrainedLabels represents a collection of label name -> constrain function
|
|
|
|
// to normalize label values. This type is commonly used when constructing
|
|
|
|
// metric vector Collectors.
|
|
|
|
type ConstrainedLabels []ConstrainedLabel
|
|
|
|
|
2023-06-27 14:21:36 +03:00
|
|
|
func (cls ConstrainedLabels) compile() *compiledLabels {
|
|
|
|
compiled := &compiledLabels{
|
|
|
|
names: make([]string, len(cls)),
|
|
|
|
labelConstraints: map[string]LabelConstraint{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, label := range cls {
|
|
|
|
compiled.names[i] = label.Name
|
|
|
|
if label.Constraint != nil {
|
|
|
|
compiled.labelConstraints[label.Name] = label.Constraint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return compiled
|
2022-12-13 15:47:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cls ConstrainedLabels) labelNames() []string {
|
|
|
|
names := make([]string, len(cls))
|
|
|
|
for i, label := range cls {
|
|
|
|
names[i] = label.Name
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnconstrainedLabels represents collection of label without any constraint on
|
|
|
|
// their value. Thus, it is simply a collection of label names.
|
|
|
|
//
|
|
|
|
// UnconstrainedLabels([]string{ "A", "B" })
|
|
|
|
//
|
|
|
|
// is equivalent to
|
|
|
|
//
|
|
|
|
// ConstrainedLabels {
|
|
|
|
// { Name: "A" },
|
|
|
|
// { Name: "B" },
|
|
|
|
// }
|
|
|
|
type UnconstrainedLabels []string
|
|
|
|
|
2023-06-27 14:21:36 +03:00
|
|
|
func (uls UnconstrainedLabels) compile() *compiledLabels {
|
|
|
|
return &compiledLabels{
|
|
|
|
names: uls,
|
2022-12-13 15:47:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uls UnconstrainedLabels) labelNames() []string {
|
|
|
|
return uls
|
|
|
|
}
|
|
|
|
|
2023-06-27 14:21:36 +03:00
|
|
|
type compiledLabels struct {
|
|
|
|
names []string
|
|
|
|
labelConstraints map[string]LabelConstraint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cls *compiledLabels) compile() *compiledLabels {
|
|
|
|
return cls
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cls *compiledLabels) labelNames() []string {
|
|
|
|
return cls.names
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cls *compiledLabels) constrain(labelName, value string) string {
|
|
|
|
if fn, ok := cls.labelConstraints[labelName]; ok && fn != nil {
|
|
|
|
return fn(value)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2017-08-25 15:50:43 +03:00
|
|
|
// reservedLabelPrefix is a prefix which is not legal in user-supplied
|
|
|
|
// label names.
|
|
|
|
const reservedLabelPrefix = "__"
|
|
|
|
|
|
|
|
var errInconsistentCardinality = errors.New("inconsistent label cardinality")
|
|
|
|
|
2018-11-02 19:01:14 +03:00
|
|
|
func makeInconsistentCardinalityError(fqName string, labels, labelValues []string) error {
|
|
|
|
return fmt.Errorf(
|
2022-08-03 07:30:51 +03:00
|
|
|
"%w: %q has %d variable labels named %q but %d values %q were provided",
|
2018-11-02 19:01:14 +03:00
|
|
|
errInconsistentCardinality, fqName,
|
|
|
|
len(labels), labels,
|
|
|
|
len(labelValues), labelValues,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:58:59 +03:00
|
|
|
func validateValuesInLabels(labels Labels, expectedNumberOfValues int) error {
|
2017-08-25 15:50:43 +03:00
|
|
|
if len(labels) != expectedNumberOfValues {
|
2018-11-02 19:01:14 +03:00
|
|
|
return fmt.Errorf(
|
2022-08-03 07:30:51 +03:00
|
|
|
"%w: expected %d label values but got %d in %#v",
|
2018-11-02 19:01:14 +03:00
|
|
|
errInconsistentCardinality, expectedNumberOfValues,
|
|
|
|
len(labels), labels,
|
|
|
|
)
|
2017-08-25 15:50:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, val := range labels {
|
|
|
|
if !utf8.ValidString(val) {
|
|
|
|
return fmt.Errorf("label %s: value %q is not valid UTF-8", name, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:58:59 +03:00
|
|
|
func validateLabelValues(vals []string, expectedNumberOfValues int) error {
|
2017-08-25 15:50:43 +03:00
|
|
|
if len(vals) != expectedNumberOfValues {
|
2023-10-04 19:09:15 +03:00
|
|
|
// The call below makes vals escape, copy them to avoid that.
|
|
|
|
vals := append([]string(nil), vals...)
|
2018-11-02 19:01:14 +03:00
|
|
|
return fmt.Errorf(
|
2022-08-03 07:30:51 +03:00
|
|
|
"%w: expected %d label values but got %d in %#v",
|
2018-11-02 19:01:14 +03:00
|
|
|
errInconsistentCardinality, expectedNumberOfValues,
|
|
|
|
len(vals), vals,
|
|
|
|
)
|
2017-08-25 15:50:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range vals {
|
|
|
|
if !utf8.ValidString(val) {
|
|
|
|
return fmt.Errorf("label value %q is not valid UTF-8", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkLabelName(l string) bool {
|
|
|
|
return model.LabelName(l).IsValid() && !strings.HasPrefix(l, reservedLabelPrefix)
|
|
|
|
}
|