khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 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 | |
| 14 | package model |
| 15 | |
| 16 | import ( |
| 17 | "encoding/json" |
| 18 | "fmt" |
| 19 | "regexp" |
| 20 | "strings" |
| 21 | "unicode/utf8" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | // AlertNameLabel is the name of the label containing the an alert's name. |
| 26 | AlertNameLabel = "alertname" |
| 27 | |
| 28 | // ExportedLabelPrefix is the prefix to prepend to the label names present in |
| 29 | // exported metrics if a label of the same name is added by the server. |
| 30 | ExportedLabelPrefix = "exported_" |
| 31 | |
| 32 | // MetricNameLabel is the label name indicating the metric name of a |
| 33 | // timeseries. |
| 34 | MetricNameLabel = "__name__" |
| 35 | |
| 36 | // SchemeLabel is the name of the label that holds the scheme on which to |
| 37 | // scrape a target. |
| 38 | SchemeLabel = "__scheme__" |
| 39 | |
| 40 | // AddressLabel is the name of the label that holds the address of |
| 41 | // a scrape target. |
| 42 | AddressLabel = "__address__" |
| 43 | |
| 44 | // MetricsPathLabel is the name of the label that holds the path on which to |
| 45 | // scrape a target. |
| 46 | MetricsPathLabel = "__metrics_path__" |
| 47 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame^] | 48 | // ScrapeIntervalLabel is the name of the label that holds the scrape interval |
| 49 | // used to scrape a target. |
| 50 | ScrapeIntervalLabel = "__scrape_interval__" |
| 51 | |
| 52 | // ScrapeTimeoutLabel is the name of the label that holds the scrape |
| 53 | // timeout used to scrape a target. |
| 54 | ScrapeTimeoutLabel = "__scrape_timeout__" |
| 55 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 56 | // ReservedLabelPrefix is a prefix which is not legal in user-supplied |
| 57 | // label names. |
| 58 | ReservedLabelPrefix = "__" |
| 59 | |
| 60 | // MetaLabelPrefix is a prefix for labels that provide meta information. |
| 61 | // Labels with this prefix are used for intermediate label processing and |
| 62 | // will not be attached to time series. |
| 63 | MetaLabelPrefix = "__meta_" |
| 64 | |
| 65 | // TmpLabelPrefix is a prefix for temporary labels as part of relabelling. |
| 66 | // Labels with this prefix are used for intermediate label processing and |
| 67 | // will not be attached to time series. This is reserved for use in |
| 68 | // Prometheus configuration files by users. |
| 69 | TmpLabelPrefix = "__tmp_" |
| 70 | |
| 71 | // ParamLabelPrefix is a prefix for labels that provide URL parameters |
| 72 | // used to scrape a target. |
| 73 | ParamLabelPrefix = "__param_" |
| 74 | |
| 75 | // JobLabel is the label name indicating the job from which a timeseries |
| 76 | // was scraped. |
| 77 | JobLabel = "job" |
| 78 | |
| 79 | // InstanceLabel is the label name used for the instance label. |
| 80 | InstanceLabel = "instance" |
| 81 | |
| 82 | // BucketLabel is used for the label that defines the upper bound of a |
| 83 | // bucket of a histogram ("le" -> "less or equal"). |
| 84 | BucketLabel = "le" |
| 85 | |
| 86 | // QuantileLabel is used for the label that defines the quantile in a |
| 87 | // summary. |
| 88 | QuantileLabel = "quantile" |
| 89 | ) |
| 90 | |
| 91 | // LabelNameRE is a regular expression matching valid label names. Note that the |
| 92 | // IsValid method of LabelName performs the same check but faster than a match |
| 93 | // with this regular expression. |
| 94 | var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") |
| 95 | |
| 96 | // A LabelName is a key for a LabelSet or Metric. It has a value associated |
| 97 | // therewith. |
| 98 | type LabelName string |
| 99 | |
| 100 | // IsValid is true iff the label name matches the pattern of LabelNameRE. This |
| 101 | // method, however, does not use LabelNameRE for the check but a much faster |
| 102 | // hardcoded implementation. |
| 103 | func (ln LabelName) IsValid() bool { |
| 104 | if len(ln) == 0 { |
| 105 | return false |
| 106 | } |
| 107 | for i, b := range ln { |
| 108 | if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) { |
| 109 | return false |
| 110 | } |
| 111 | } |
| 112 | return true |
| 113 | } |
| 114 | |
| 115 | // UnmarshalYAML implements the yaml.Unmarshaler interface. |
| 116 | func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 117 | var s string |
| 118 | if err := unmarshal(&s); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | if !LabelName(s).IsValid() { |
| 122 | return fmt.Errorf("%q is not a valid label name", s) |
| 123 | } |
| 124 | *ln = LabelName(s) |
| 125 | return nil |
| 126 | } |
| 127 | |
| 128 | // UnmarshalJSON implements the json.Unmarshaler interface. |
| 129 | func (ln *LabelName) UnmarshalJSON(b []byte) error { |
| 130 | var s string |
| 131 | if err := json.Unmarshal(b, &s); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | if !LabelName(s).IsValid() { |
| 135 | return fmt.Errorf("%q is not a valid label name", s) |
| 136 | } |
| 137 | *ln = LabelName(s) |
| 138 | return nil |
| 139 | } |
| 140 | |
| 141 | // LabelNames is a sortable LabelName slice. In implements sort.Interface. |
| 142 | type LabelNames []LabelName |
| 143 | |
| 144 | func (l LabelNames) Len() int { |
| 145 | return len(l) |
| 146 | } |
| 147 | |
| 148 | func (l LabelNames) Less(i, j int) bool { |
| 149 | return l[i] < l[j] |
| 150 | } |
| 151 | |
| 152 | func (l LabelNames) Swap(i, j int) { |
| 153 | l[i], l[j] = l[j], l[i] |
| 154 | } |
| 155 | |
| 156 | func (l LabelNames) String() string { |
| 157 | labelStrings := make([]string, 0, len(l)) |
| 158 | for _, label := range l { |
| 159 | labelStrings = append(labelStrings, string(label)) |
| 160 | } |
| 161 | return strings.Join(labelStrings, ", ") |
| 162 | } |
| 163 | |
| 164 | // A LabelValue is an associated value for a LabelName. |
| 165 | type LabelValue string |
| 166 | |
| 167 | // IsValid returns true iff the string is a valid UTF8. |
| 168 | func (lv LabelValue) IsValid() bool { |
| 169 | return utf8.ValidString(string(lv)) |
| 170 | } |
| 171 | |
| 172 | // LabelValues is a sortable LabelValue slice. It implements sort.Interface. |
| 173 | type LabelValues []LabelValue |
| 174 | |
| 175 | func (l LabelValues) Len() int { |
| 176 | return len(l) |
| 177 | } |
| 178 | |
| 179 | func (l LabelValues) Less(i, j int) bool { |
| 180 | return string(l[i]) < string(l[j]) |
| 181 | } |
| 182 | |
| 183 | func (l LabelValues) Swap(i, j int) { |
| 184 | l[i], l[j] = l[j], l[i] |
| 185 | } |
| 186 | |
| 187 | // LabelPair pairs a name with a value. |
| 188 | type LabelPair struct { |
| 189 | Name LabelName |
| 190 | Value LabelValue |
| 191 | } |
| 192 | |
| 193 | // LabelPairs is a sortable slice of LabelPair pointers. It implements |
| 194 | // sort.Interface. |
| 195 | type LabelPairs []*LabelPair |
| 196 | |
| 197 | func (l LabelPairs) Len() int { |
| 198 | return len(l) |
| 199 | } |
| 200 | |
| 201 | func (l LabelPairs) Less(i, j int) bool { |
| 202 | switch { |
| 203 | case l[i].Name > l[j].Name: |
| 204 | return false |
| 205 | case l[i].Name < l[j].Name: |
| 206 | return true |
| 207 | case l[i].Value > l[j].Value: |
| 208 | return false |
| 209 | case l[i].Value < l[j].Value: |
| 210 | return true |
| 211 | default: |
| 212 | return false |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func (l LabelPairs) Swap(i, j int) { |
| 217 | l[i], l[j] = l[j], l[i] |
| 218 | } |