1 // Copyright 2013 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
6 // http://www.apache.org/licenses/LICENSE-2.0
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.
24 // MetricNameRE is a regular expression matching valid metric
25 // names. Note that the IsValidMetricName function performs the same
26 // check but faster than a match with this regular expression.
27 MetricNameRE = regexp.MustCompile(`^[a-zA-Z_:][a-zA-Z0-9_:]*$`)
30 // A Metric is similar to a LabelSet, but the key difference is that a Metric is
31 // a singleton and refers to one and only one stream of samples.
34 // Equal compares the metrics.
35 func (m Metric) Equal(o Metric) bool {
36 return LabelSet(m).Equal(LabelSet(o))
39 // Before compares the metrics' underlying label sets.
40 func (m Metric) Before(o Metric) bool {
41 return LabelSet(m).Before(LabelSet(o))
44 // Clone returns a copy of the Metric.
45 func (m Metric) Clone() Metric {
46 clone := make(Metric, len(m))
53 func (m Metric) String() string {
54 metricName, hasName := m[MetricNameLabel]
55 numLabels := len(m) - 1
59 labelStrings := make([]string, 0, numLabels)
60 for label, value := range m {
61 if label != MetricNameLabel {
62 labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
69 return string(metricName)
73 sort.Strings(labelStrings)
74 return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", "))
78 // Fingerprint returns a Metric's Fingerprint.
79 func (m Metric) Fingerprint() Fingerprint {
80 return LabelSet(m).Fingerprint()
83 // FastFingerprint returns a Metric's Fingerprint calculated by a faster hashing
84 // algorithm, which is, however, more susceptible to hash collisions.
85 func (m Metric) FastFingerprint() Fingerprint {
86 return LabelSet(m).FastFingerprint()
89 // IsValidMetricName returns true iff name matches the pattern of MetricNameRE.
90 // This function, however, does not use MetricNameRE for the check but a much
91 // faster hardcoded implementation.
92 func IsValidMetricName(n LabelValue) bool {
97 if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || b == ':' || (b >= '0' && b <= '9' && i > 0)) {