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
14package prometheus
15
16import (
17	"errors"
18	"fmt"
19	"sort"
20	"strings"
21
22	"github.com/cespare/xxhash/v2"
23	//nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.
24	"github.com/golang/protobuf/proto"
25	"github.com/prometheus/common/model"
26
27	dto "github.com/prometheus/client_model/go"
28)
29
30// Desc is the descriptor used by every Prometheus Metric. It is essentially
31// the immutable meta-data of a Metric. The normal Metric implementations
32// included in this package manage their Desc under the hood. Users only have to
33// deal with Desc if they use advanced features like the ExpvarCollector or
34// custom Collectors and Metrics.
35//
36// Descriptors registered with the same registry have to fulfill certain
37// consistency and uniqueness criteria if they share the same fully-qualified
38// name: They must have the same help string and the same label names (aka label
39// dimensions) in each, constLabels and variableLabels, but they must differ in
40// the values of the constLabels.
41//
42// Descriptors that share the same fully-qualified names and the same label
43// values of their constLabels are considered equal.
44//
45// Use NewDesc to create new Desc instances.
46type Desc struct {
47	// fqName has been built from Namespace, Subsystem, and Name.
48	fqName string
49	// help provides some helpful information about this metric.
50	help string
51	// constLabelPairs contains precalculated DTO label pairs based on
52	// the constant labels.
53	constLabelPairs []*dto.LabelPair
54	// variableLabels contains names of labels for which the metric
55	// maintains variable values.
56	variableLabels []string
57	// id is a hash of the values of the ConstLabels and fqName. This
58	// must be unique among all registered descriptors and can therefore be
59	// used as an identifier of the descriptor.
60	id uint64
61	// dimHash is a hash of the label names (preset and variable) and the
62	// Help string. Each Desc with the same fqName must have the same
63	// dimHash.
64	dimHash uint64
65	// err is an error that occurred during construction. It is reported on
66	// registration time.
67	err error
68}
69
70// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc
71// and will be reported on registration time. variableLabels and constLabels can
72// be nil if no such labels should be set. fqName must not be empty.
73//
74// variableLabels only contain the label names. Their label values are variable
75// and therefore not part of the Desc. (They are managed within the Metric.)
76//
77// For constLabels, the label values are constant. Therefore, they are fully
78// specified in the Desc. See the Collector example for a usage pattern.
79func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *Desc {
80	d := &Desc{
81		fqName:         fqName,
82		help:           help,
83		variableLabels: variableLabels,
84	}
85	if !model.IsValidMetricName(model.LabelValue(fqName)) {
86		d.err = fmt.Errorf("%q is not a valid metric name", fqName)
87		return d
88	}
89	// labelValues contains the label values of const labels (in order of
90	// their sorted label names) plus the fqName (at position 0).
91	labelValues := make([]string, 1, len(constLabels)+1)
92	labelValues[0] = fqName
93	labelNames := make([]string, 0, len(constLabels)+len(variableLabels))
94	labelNameSet := map[string]struct{}{}
95	// First add only the const label names and sort them...
96	for labelName := range constLabels {
97		if !checkLabelName(labelName) {
98			d.err = fmt.Errorf("%q is not a valid label name for metric %q", labelName, fqName)
99			return d
100		}
101		labelNames = append(labelNames, labelName)
102		labelNameSet[labelName] = struct{}{}
103	}
104	sort.Strings(labelNames)
105	// ... so that we can now add const label values in the order of their names.
106	for _, labelName := range labelNames {
107		labelValues = append(labelValues, constLabels[labelName])
108	}
109	// Validate the const label values. They can't have a wrong cardinality, so
110	// use in len(labelValues) as expectedNumberOfValues.
111	if err := validateLabelValues(labelValues, len(labelValues)); err != nil {
112		d.err = err
113		return d
114	}
115	// Now add the variable label names, but prefix them with something that
116	// cannot be in a regular label name. That prevents matching the label
117	// dimension with a different mix between preset and variable labels.
118	for _, labelName := range variableLabels {
119		if !checkLabelName(labelName) {
120			d.err = fmt.Errorf("%q is not a valid label name for metric %q", labelName, fqName)
121			return d
122		}
123		labelNames = append(labelNames, "$"+labelName)
124		labelNameSet[labelName] = struct{}{}
125	}
126	if len(labelNames) != len(labelNameSet) {
127		d.err = errors.New("duplicate label names")
128		return d
129	}
130
131	xxh := xxhash.New()
132	for _, val := range labelValues {
133		xxh.WriteString(val)
134		xxh.Write(separatorByteSlice)
135	}
136	d.id = xxh.Sum64()
137	// Sort labelNames so that order doesn't matter for the hash.
138	sort.Strings(labelNames)
139	// Now hash together (in this order) the help string and the sorted
140	// label names.
141	xxh.Reset()
142	xxh.WriteString(help)
143	xxh.Write(separatorByteSlice)
144	for _, labelName := range labelNames {
145		xxh.WriteString(labelName)
146		xxh.Write(separatorByteSlice)
147	}
148	d.dimHash = xxh.Sum64()
149
150	d.constLabelPairs = make([]*dto.LabelPair, 0, len(constLabels))
151	for n, v := range constLabels {
152		d.constLabelPairs = append(d.constLabelPairs, &dto.LabelPair{
153			Name:  proto.String(n),
154			Value: proto.String(v),
155		})
156	}
157	sort.Sort(labelPairSorter(d.constLabelPairs))
158	return d
159}
160
161// NewInvalidDesc returns an invalid descriptor, i.e. a descriptor with the
162// provided error set. If a collector returning such a descriptor is registered,
163// registration will fail with the provided error. NewInvalidDesc can be used by
164// a Collector to signal inability to describe itself.
165func NewInvalidDesc(err error) *Desc {
166	return &Desc{
167		err: err,
168	}
169}
170
171func (d *Desc) String() string {
172	lpStrings := make([]string, 0, len(d.constLabelPairs))
173	for _, lp := range d.constLabelPairs {
174		lpStrings = append(
175			lpStrings,
176			fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()),
177		)
178	}
179	return fmt.Sprintf(
180		"Desc{fqName: %q, help: %q, constLabels: {%s}, variableLabels: %v}",
181		d.fqName,
182		d.help,
183		strings.Join(lpStrings, ","),
184		d.variableLabels,
185	)
186}
187