| 1 |
// Copyright 2016 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package prometheus |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"sort" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/cespare/xxhash/v2" |
| 22 |
dto "github.com/prometheus/client_model/go" |
| 23 |
"github.com/prometheus/common/model" |
| 24 |
"google.golang.org/protobuf/proto" |
| 25 |
|
| 26 |
"github.com/prometheus/client_golang/prometheus/internal" |
| 27 |
) |
| 28 |
|
| 29 |
// Desc is the descriptor used by every Prometheus Metric. It is essentially |
| 30 |
// the immutable meta-data of a Metric. The normal Metric implementations |
| 31 |
// included in this package manage their Desc under the hood. Users only have to |
| 32 |
// deal with Desc if they use advanced features like the ExpvarCollector or |
| 33 |
// custom Collectors and Metrics. |
| 34 |
// |
| 35 |
// Descriptors registered with the same registry have to fulfill certain |
| 36 |
// consistency and uniqueness criteria if they share the same fully-qualified |
| 37 |
// name: They must have the same help string and the same label names (aka label |
| 38 |
// dimensions) in each, constLabels and variableLabels, but they must differ in |
| 39 |
// the values of the constLabels. |
| 40 |
// |
| 41 |
// Descriptors that share the same fully-qualified names and the same label |
| 42 |
// values of their constLabels are considered equal. |
| 43 |
// |
| 44 |
// Use NewDesc to create new Desc instances. |
| 45 |
type Desc struct { |
| 46 |
// fqName has been built from Namespace, Subsystem, and Name. |
| 47 |
fqName string |
| 48 |
// help provides some helpful information about this metric. |
| 49 |
help string |
| 50 |
// constLabelPairs contains precalculated DTO label pairs based on |
| 51 |
// the constant labels. |
| 52 |
constLabelPairs []*dto.LabelPair |
| 53 |
// variableLabels contains names of labels and normalization function for |
| 54 |
// which the metric maintains variable values. |
| 55 |
variableLabels *compiledLabels |
| 56 |
// id is a hash of the values of the ConstLabels and fqName. This |
| 57 |
// must be unique among all registered descriptors and can therefore be |
| 58 |
// used as an identifier of the descriptor. |
| 59 |
id uint64 |
| 60 |
// dimHash is a hash of the label names (preset and variable) and the |
| 61 |
// Help string. Each Desc with the same fqName must have the same |
| 62 |
// dimHash. |
| 63 |
dimHash uint64 |
| 64 |
// err is an error that occurred during construction. It is reported on |
| 65 |
// registration time. |
| 66 |
err error |
| 67 |
} |
| 68 |
|
| 69 |
// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc |
| 70 |
// and will be reported on registration time. variableLabels and constLabels can |
| 71 |
// be nil if no such labels should be set. fqName must not be empty. |
| 72 |
// |
| 73 |
// variableLabels only contain the label names. Their label values are variable |
| 74 |
// and therefore not part of the Desc. (They are managed within the Metric.) |
| 75 |
// |
| 76 |
// For constLabels, the label values are constant. Therefore, they are fully |
| 77 |
// specified in the Desc. See the Collector example for a usage pattern. |
| 78 |
func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *Desc { |
| 79 |
return V2.NewDesc(fqName, help, UnconstrainedLabels(variableLabels), constLabels) |
| 80 |
} |
| 81 |
|
| 82 |
// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc |
| 83 |
// and will be reported on registration time. variableLabels and constLabels can |
| 84 |
// be nil if no such labels should be set. fqName must not be empty. |
| 85 |
// |
| 86 |
// variableLabels only contain the label names and normalization functions. Their |
| 87 |
// label values are variable and therefore not part of the Desc. (They are managed |
| 88 |
// within the Metric.) |
| 89 |
// |
| 90 |
// For constLabels, the label values are constant. Therefore, they are fully |
| 91 |
// specified in the Desc. See the Collector example for a usage pattern. |
| 92 |
func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, constLabels Labels) *Desc { |
| 93 |
d := &Desc{ |
| 94 |
fqName: fqName, |
| 95 |
help: help, |
| 96 |
variableLabels: variableLabels.compile(), |
| 97 |
} |
| 98 |
//nolint:staticcheck // TODO: Don't use deprecated model.NameValidationScheme. |
| 99 |
if !model.NameValidationScheme.IsValidMetricName(fqName) { |
| 100 |
d.err = fmt.Errorf("%q is not a valid metric name", fqName) |
| 101 |
return d |
| 102 |
} |
| 103 |
// labelValues contains the label values of const labels (in order of |
| 104 |
// their sorted label names) plus the fqName (at position 0). |
| 105 |
labelValues := make([]string, 1, len(constLabels)+1) |
| 106 |
labelValues[0] = fqName |
| 107 |
labelNames := make([]string, 0, len(constLabels)+len(d.variableLabels.names)) |
| 108 |
labelNameSet := map[string]struct{}{} |
| 109 |
// First add only the const label names and sort them... |
| 110 |
for labelName := range constLabels { |
| 111 |
if !checkLabelName(labelName) { |
| 112 |
d.err = fmt.Errorf("%q is not a valid label name for metric %q", labelName, fqName) |
| 113 |
return d |
| 114 |
} |
| 115 |
labelNames = append(labelNames, labelName) |
| 116 |
labelNameSet[labelName] = struct{}{} |
| 117 |
} |
| 118 |
sort.Strings(labelNames) |
| 119 |
// ... so that we can now add const label values in the order of their names. |
| 120 |
for _, labelName := range labelNames { |
| 121 |
labelValues = append(labelValues, constLabels[labelName]) |
| 122 |
} |
| 123 |
// Validate the const label values. They can't have a wrong cardinality, so |
| 124 |
// use in len(labelValues) as expectedNumberOfValues. |
| 125 |
if err := validateLabelValues(labelValues, len(labelValues)); err != nil { |
| 126 |
d.err = err |
| 127 |
return d |
| 128 |
} |
| 129 |
// Now add the variable label names, but prefix them with something that |
| 130 |
// cannot be in a regular label name. That prevents matching the label |
| 131 |
// dimension with a different mix between preset and variable labels. |
| 132 |
for _, label := range d.variableLabels.names { |
| 133 |
if !checkLabelName(label) { |
| 134 |
d.err = fmt.Errorf("%q is not a valid label name for metric %q", label, fqName) |
| 135 |
return d |
| 136 |
} |
| 137 |
labelNames = append(labelNames, "$"+label) |
| 138 |
labelNameSet[label] = struct{}{} |
| 139 |
} |
| 140 |
if len(labelNames) != len(labelNameSet) { |
| 141 |
d.err = fmt.Errorf("duplicate label names in constant and variable labels for metric %q", fqName) |
| 142 |
return d |
| 143 |
} |
| 144 |
|
| 145 |
xxh := xxhash.New() |
| 146 |
for _, val := range labelValues { |
| 147 |
xxh.WriteString(val) |
| 148 |
xxh.Write(separatorByteSlice) |
| 149 |
} |
| 150 |
d.id = xxh.Sum64() |
| 151 |
// Sort labelNames so that order doesn't matter for the hash. |
| 152 |
sort.Strings(labelNames) |
| 153 |
// Now hash together (in this order) the help string and the sorted |
| 154 |
// label names. |
| 155 |
xxh.Reset() |
| 156 |
xxh.WriteString(help) |
| 157 |
xxh.Write(separatorByteSlice) |
| 158 |
for _, labelName := range labelNames { |
| 159 |
xxh.WriteString(labelName) |
| 160 |
xxh.Write(separatorByteSlice) |
| 161 |
} |
| 162 |
d.dimHash = xxh.Sum64() |
| 163 |
|
| 164 |
d.constLabelPairs = make([]*dto.LabelPair, 0, len(constLabels)) |
| 165 |
for n, v := range constLabels { |
| 166 |
d.constLabelPairs = append(d.constLabelPairs, &dto.LabelPair{ |
| 167 |
Name: proto.String(n), |
| 168 |
Value: proto.String(v), |
| 169 |
}) |
| 170 |
} |
| 171 |
sort.Sort(internal.LabelPairSorter(d.constLabelPairs)) |
| 172 |
return d |
| 173 |
} |
| 174 |
|
| 175 |
// NewInvalidDesc returns an invalid descriptor, i.e. a descriptor with the |
| 176 |
// provided error set. If a collector returning such a descriptor is registered, |
| 177 |
// registration will fail with the provided error. NewInvalidDesc can be used by |
| 178 |
// a Collector to signal inability to describe itself. |
| 179 |
func NewInvalidDesc(err error) *Desc { |
| 180 |
return &Desc{ |
| 181 |
err: err, |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func (d *Desc) String() string { |
| 186 |
lpStrings := make([]string, 0, len(d.constLabelPairs)) |
| 187 |
for _, lp := range d.constLabelPairs { |
| 188 |
lpStrings = append( |
| 189 |
lpStrings, |
| 190 |
fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()), |
| 191 |
) |
| 192 |
} |
| 193 |
vlStrings := []string{} |
| 194 |
if d.variableLabels != nil { |
| 195 |
vlStrings = make([]string, 0, len(d.variableLabels.names)) |
| 196 |
for _, vl := range d.variableLabels.names { |
| 197 |
if fn, ok := d.variableLabels.labelConstraints[vl]; ok && fn != nil { |
| 198 |
vlStrings = append(vlStrings, fmt.Sprintf("c(%s)", vl)) |
| 199 |
} else { |
| 200 |
vlStrings = append(vlStrings, vl) |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
return fmt.Sprintf( |
| 205 |
"Desc{fqName: %q, help: %q, constLabels: {%s}, variableLabels: {%s}}", |
| 206 |
d.fqName, |
| 207 |
d.help, |
| 208 |
strings.Join(lpStrings, ","), |
| 209 |
strings.Join(vlStrings, ","), |
| 210 |
) |
| 211 |
} |