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 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 model 15 16import ( 17 "fmt" 18 "sort" 19 "strings" 20) 21 22var separator = []byte{0} 23 24// A Metric is similar to a LabelSet, but the key difference is that a Metric is 25// a singleton and refers to one and only one stream of samples. 26type Metric LabelSet 27 28// Equal compares the metrics. 29func (m Metric) Equal(o Metric) bool { 30 return LabelSet(m).Equal(LabelSet(o)) 31} 32 33// Before compares the metrics' underlying label sets. 34func (m Metric) Before(o Metric) bool { 35 return LabelSet(m).Before(LabelSet(o)) 36} 37 38// Clone returns a copy of the Metric. 39func (m Metric) Clone() Metric { 40 clone := Metric{} 41 for k, v := range m { 42 clone[k] = v 43 } 44 return clone 45} 46 47func (m Metric) String() string { 48 metricName, hasName := m[MetricNameLabel] 49 numLabels := len(m) - 1 50 if !hasName { 51 numLabels = len(m) 52 } 53 labelStrings := make([]string, 0, numLabels) 54 for label, value := range m { 55 if label != MetricNameLabel { 56 labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value)) 57 } 58 } 59 60 switch numLabels { 61 case 0: 62 if hasName { 63 return string(metricName) 64 } 65 return "{}" 66 default: 67 sort.Strings(labelStrings) 68 return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", ")) 69 } 70} 71 72// Fingerprint returns a Metric's Fingerprint. 73func (m Metric) Fingerprint() Fingerprint { 74 return LabelSet(m).Fingerprint() 75} 76 77// FastFingerprint returns a Metric's Fingerprint calculated by a faster hashing 78// algorithm, which is, however, more susceptible to hash collisions. 79func (m Metric) FastFingerprint() Fingerprint { 80 return LabelSet(m).FastFingerprint() 81} 82