github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/scrape/model/labels.go (about) 1 // Copyright 2013 The Prometheus Authors 2 // Copyright 2021 The Pyroscope Authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package model 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "strings" 22 "unicode/utf8" 23 ) 24 25 // revive:disable:max-public-structs preserve original implementation 26 27 const ( 28 // AlertNameLabel is the name of the label containing the an alert's name. 29 AlertNameLabel = "alertname" 30 31 // ExportedLabelPrefix is the prefix to prepend to the label names present in 32 // exported metrics if a label of the same name is added by the server. 33 ExportedLabelPrefix = "exported_" 34 35 // MetricNameLabel is the label name indicating the metric name of a 36 // timeseries. 37 MetricNameLabel = "__name__" 38 39 // SchemeLabel is the name of the label that holds the scheme on which to 40 // scrape a target. 41 SchemeLabel = "__scheme__" 42 43 // AddressLabel is the name of the label that holds the address of 44 // a scrape target. 45 AddressLabel = "__address__" 46 47 // MetricsPathLabel is the name of the label that holds the path on which to 48 // scrape a target. 49 MetricsPathLabel = "__metrics_path__" 50 51 // ScrapeIntervalLabel is the name of the label that holds the scrape interval 52 // used to scrape a target. 53 ScrapeIntervalLabel = "__scrape_interval__" 54 55 // ScrapeTimeoutLabel is the name of the label that holds the scrape 56 // timeout used to scrape a target. 57 ScrapeTimeoutLabel = "__scrape_timeout__" 58 59 // ReservedLabelPrefix is a prefix which is not legal in user-supplied 60 // label names. 61 ReservedLabelPrefix = "__" 62 63 // MetaLabelPrefix is a prefix for labels that provide meta information. 64 // Labels with this prefix are used for intermediate label processing and 65 // will not be attached to time series. 66 MetaLabelPrefix = "__meta_" 67 68 // TmpLabelPrefix is a prefix for temporary labels as part of relabelling. 69 // Labels with this prefix are used for intermediate label processing and 70 // will not be attached to time series. This is reserved for use in 71 // Prometheus configuration files by users. 72 TmpLabelPrefix = "__tmp_" 73 74 // ParamLabelPrefix is a prefix for labels that provide URL parameters 75 // used to scrape a target. 76 ParamLabelPrefix = "__param_" 77 78 // JobLabel is the label name indicating the job from which a timeseries 79 // was scraped. 80 JobLabel = "job" 81 82 // InstanceLabel is the label name used for the instance label. 83 InstanceLabel = "instance" 84 85 // BucketLabel is used for the label that defines the upper bound of a 86 // bucket of a histogram ("le" -> "less or equal"). 87 BucketLabel = "le" 88 89 // QuantileLabel is used for the label that defines the quantile in a 90 // summary. 91 QuantileLabel = "quantile" 92 93 AppNameLabel = "__name__" 94 SpyNameLabel = "__spy_name__" 95 ProfileLabelPrefix = "__profile_" 96 ProfilePathLabel = ProfileLabelPrefix + "path__" 97 ProfileNameLabel = ProfileLabelPrefix + "name__" 98 ) 99 100 // A LabelName is a key for a LabelSet or Metric. It has a value associated 101 // therewith. 102 type LabelName string 103 104 // IsValid is true iff the label name matches the pattern of LabelNameRE. This 105 // method, however, does not use LabelNameRE for the check but a much faster 106 // hardcoded implementation. 107 func (ln LabelName) IsValid() bool { 108 if len(ln) == 0 { 109 return false 110 } 111 for i, b := range ln { 112 if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) { 113 return false 114 } 115 } 116 return true 117 } 118 119 // UnmarshalYAML implements the yaml.Unmarshaler interface. 120 func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error { 121 var s string 122 if err := unmarshal(&s); err != nil { 123 return err 124 } 125 if !LabelName(s).IsValid() { 126 return fmt.Errorf("%q is not a valid label name", s) 127 } 128 *ln = LabelName(s) 129 return nil 130 } 131 132 // UnmarshalJSON implements the json.Unmarshaler interface. 133 func (ln *LabelName) UnmarshalJSON(b []byte) error { 134 var s string 135 if err := json.Unmarshal(b, &s); err != nil { 136 return err 137 } 138 if !LabelName(s).IsValid() { 139 return fmt.Errorf("%q is not a valid label name", s) 140 } 141 *ln = LabelName(s) 142 return nil 143 } 144 145 // LabelNames is a sortable LabelName slice. In implements sort.Interface. 146 type LabelNames []LabelName 147 148 func (l LabelNames) Len() int { 149 return len(l) 150 } 151 152 func (l LabelNames) Less(i, j int) bool { 153 return l[i] < l[j] 154 } 155 156 func (l LabelNames) Swap(i, j int) { 157 l[i], l[j] = l[j], l[i] 158 } 159 160 func (l LabelNames) String() string { 161 labelStrings := make([]string, 0, len(l)) 162 for _, label := range l { 163 labelStrings = append(labelStrings, string(label)) 164 } 165 return strings.Join(labelStrings, ", ") 166 } 167 168 // A LabelValue is an associated value for a LabelName. 169 type LabelValue string 170 171 // IsValid returns true iff the string is a valid UTF8. 172 func (lv LabelValue) IsValid() bool { 173 return utf8.ValidString(string(lv)) 174 } 175 176 // LabelValues is a sortable LabelValue slice. It implements sort.Interface. 177 type LabelValues []LabelValue 178 179 func (l LabelValues) Len() int { 180 return len(l) 181 } 182 183 func (l LabelValues) Less(i, j int) bool { 184 return string(l[i]) < string(l[j]) 185 } 186 187 func (l LabelValues) Swap(i, j int) { 188 l[i], l[j] = l[j], l[i] 189 } 190 191 // LabelPair pairs a name with a value. 192 type LabelPair struct { 193 Name LabelName 194 Value LabelValue 195 } 196 197 // LabelPairs is a sortable slice of LabelPair pointers. It implements 198 // sort.Interface. 199 type LabelPairs []*LabelPair 200 201 func (l LabelPairs) Len() int { 202 return len(l) 203 } 204 205 func (l LabelPairs) Less(i, j int) bool { 206 switch { 207 case l[i].Name > l[j].Name: 208 return false 209 case l[i].Name < l[j].Name: 210 return true 211 case l[i].Value > l[j].Value: 212 return false 213 case l[i].Value < l[j].Value: 214 return true 215 default: 216 return false 217 } 218 } 219 220 func (l LabelPairs) Swap(i, j int) { 221 l[i], l[j] = l[j], l[i] 222 }