khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | // Copyright 2014 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 prometheus |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "sync" |
| 19 | |
| 20 | "github.com/prometheus/common/model" |
| 21 | ) |
| 22 | |
| 23 | // metricVec is a Collector to bundle metrics of the same name that differ in |
| 24 | // their label values. metricVec is not used directly (and therefore |
| 25 | // unexported). It is used as a building block for implementations of vectors of |
| 26 | // a given metric type, like GaugeVec, CounterVec, SummaryVec, and HistogramVec. |
| 27 | // It also handles label currying. It uses basicMetricVec internally. |
| 28 | type metricVec struct { |
| 29 | *metricMap |
| 30 | |
| 31 | curry []curriedLabelValue |
| 32 | |
| 33 | // hashAdd and hashAddByte can be replaced for testing collision handling. |
| 34 | hashAdd func(h uint64, s string) uint64 |
| 35 | hashAddByte func(h uint64, b byte) uint64 |
| 36 | } |
| 37 | |
| 38 | // newMetricVec returns an initialized metricVec. |
| 39 | func newMetricVec(desc *Desc, newMetric func(lvs ...string) Metric) *metricVec { |
| 40 | return &metricVec{ |
| 41 | metricMap: &metricMap{ |
| 42 | metrics: map[uint64][]metricWithLabelValues{}, |
| 43 | desc: desc, |
| 44 | newMetric: newMetric, |
| 45 | }, |
| 46 | hashAdd: hashAdd, |
| 47 | hashAddByte: hashAddByte, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // DeleteLabelValues removes the metric where the variable labels are the same |
| 52 | // as those passed in as labels (same order as the VariableLabels in Desc). It |
| 53 | // returns true if a metric was deleted. |
| 54 | // |
| 55 | // It is not an error if the number of label values is not the same as the |
| 56 | // number of VariableLabels in Desc. However, such inconsistent label count can |
| 57 | // never match an actual metric, so the method will always return false in that |
| 58 | // case. |
| 59 | // |
| 60 | // Note that for more than one label value, this method is prone to mistakes |
| 61 | // caused by an incorrect order of arguments. Consider Delete(Labels) as an |
| 62 | // alternative to avoid that type of mistake. For higher label numbers, the |
| 63 | // latter has a much more readable (albeit more verbose) syntax, but it comes |
| 64 | // with a performance overhead (for creating and processing the Labels map). |
| 65 | // See also the CounterVec example. |
| 66 | func (m *metricVec) DeleteLabelValues(lvs ...string) bool { |
| 67 | h, err := m.hashLabelValues(lvs) |
| 68 | if err != nil { |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | return m.metricMap.deleteByHashWithLabelValues(h, lvs, m.curry) |
| 73 | } |
| 74 | |
| 75 | // Delete deletes the metric where the variable labels are the same as those |
| 76 | // passed in as labels. It returns true if a metric was deleted. |
| 77 | // |
| 78 | // It is not an error if the number and names of the Labels are inconsistent |
| 79 | // with those of the VariableLabels in Desc. However, such inconsistent Labels |
| 80 | // can never match an actual metric, so the method will always return false in |
| 81 | // that case. |
| 82 | // |
| 83 | // This method is used for the same purpose as DeleteLabelValues(...string). See |
| 84 | // there for pros and cons of the two methods. |
| 85 | func (m *metricVec) Delete(labels Labels) bool { |
| 86 | h, err := m.hashLabels(labels) |
| 87 | if err != nil { |
| 88 | return false |
| 89 | } |
| 90 | |
| 91 | return m.metricMap.deleteByHashWithLabels(h, labels, m.curry) |
| 92 | } |
| 93 | |
| 94 | func (m *metricVec) curryWith(labels Labels) (*metricVec, error) { |
| 95 | var ( |
| 96 | newCurry []curriedLabelValue |
| 97 | oldCurry = m.curry |
| 98 | iCurry int |
| 99 | ) |
| 100 | for i, label := range m.desc.variableLabels { |
| 101 | val, ok := labels[label] |
| 102 | if iCurry < len(oldCurry) && oldCurry[iCurry].index == i { |
| 103 | if ok { |
| 104 | return nil, fmt.Errorf("label name %q is already curried", label) |
| 105 | } |
| 106 | newCurry = append(newCurry, oldCurry[iCurry]) |
| 107 | iCurry++ |
| 108 | } else { |
| 109 | if !ok { |
| 110 | continue // Label stays uncurried. |
| 111 | } |
| 112 | newCurry = append(newCurry, curriedLabelValue{i, val}) |
| 113 | } |
| 114 | } |
| 115 | if l := len(oldCurry) + len(labels) - len(newCurry); l > 0 { |
| 116 | return nil, fmt.Errorf("%d unknown label(s) found during currying", l) |
| 117 | } |
| 118 | |
| 119 | return &metricVec{ |
| 120 | metricMap: m.metricMap, |
| 121 | curry: newCurry, |
| 122 | hashAdd: m.hashAdd, |
| 123 | hashAddByte: m.hashAddByte, |
| 124 | }, nil |
| 125 | } |
| 126 | |
| 127 | func (m *metricVec) getMetricWithLabelValues(lvs ...string) (Metric, error) { |
| 128 | h, err := m.hashLabelValues(lvs) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | |
| 133 | return m.metricMap.getOrCreateMetricWithLabelValues(h, lvs, m.curry), nil |
| 134 | } |
| 135 | |
| 136 | func (m *metricVec) getMetricWith(labels Labels) (Metric, error) { |
| 137 | h, err := m.hashLabels(labels) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | |
| 142 | return m.metricMap.getOrCreateMetricWithLabels(h, labels, m.curry), nil |
| 143 | } |
| 144 | |
| 145 | func (m *metricVec) hashLabelValues(vals []string) (uint64, error) { |
| 146 | if err := validateLabelValues(vals, len(m.desc.variableLabels)-len(m.curry)); err != nil { |
| 147 | return 0, err |
| 148 | } |
| 149 | |
| 150 | var ( |
| 151 | h = hashNew() |
| 152 | curry = m.curry |
| 153 | iVals, iCurry int |
| 154 | ) |
| 155 | for i := 0; i < len(m.desc.variableLabels); i++ { |
| 156 | if iCurry < len(curry) && curry[iCurry].index == i { |
| 157 | h = m.hashAdd(h, curry[iCurry].value) |
| 158 | iCurry++ |
| 159 | } else { |
| 160 | h = m.hashAdd(h, vals[iVals]) |
| 161 | iVals++ |
| 162 | } |
| 163 | h = m.hashAddByte(h, model.SeparatorByte) |
| 164 | } |
| 165 | return h, nil |
| 166 | } |
| 167 | |
| 168 | func (m *metricVec) hashLabels(labels Labels) (uint64, error) { |
| 169 | if err := validateValuesInLabels(labels, len(m.desc.variableLabels)-len(m.curry)); err != nil { |
| 170 | return 0, err |
| 171 | } |
| 172 | |
| 173 | var ( |
| 174 | h = hashNew() |
| 175 | curry = m.curry |
| 176 | iCurry int |
| 177 | ) |
| 178 | for i, label := range m.desc.variableLabels { |
| 179 | val, ok := labels[label] |
| 180 | if iCurry < len(curry) && curry[iCurry].index == i { |
| 181 | if ok { |
| 182 | return 0, fmt.Errorf("label name %q is already curried", label) |
| 183 | } |
| 184 | h = m.hashAdd(h, curry[iCurry].value) |
| 185 | iCurry++ |
| 186 | } else { |
| 187 | if !ok { |
| 188 | return 0, fmt.Errorf("label name %q missing in label map", label) |
| 189 | } |
| 190 | h = m.hashAdd(h, val) |
| 191 | } |
| 192 | h = m.hashAddByte(h, model.SeparatorByte) |
| 193 | } |
| 194 | return h, nil |
| 195 | } |
| 196 | |
| 197 | // metricWithLabelValues provides the metric and its label values for |
| 198 | // disambiguation on hash collision. |
| 199 | type metricWithLabelValues struct { |
| 200 | values []string |
| 201 | metric Metric |
| 202 | } |
| 203 | |
| 204 | // curriedLabelValue sets the curried value for a label at the given index. |
| 205 | type curriedLabelValue struct { |
| 206 | index int |
| 207 | value string |
| 208 | } |
| 209 | |
| 210 | // metricMap is a helper for metricVec and shared between differently curried |
| 211 | // metricVecs. |
| 212 | type metricMap struct { |
| 213 | mtx sync.RWMutex // Protects metrics. |
| 214 | metrics map[uint64][]metricWithLabelValues |
| 215 | desc *Desc |
| 216 | newMetric func(labelValues ...string) Metric |
| 217 | } |
| 218 | |
| 219 | // Describe implements Collector. It will send exactly one Desc to the provided |
| 220 | // channel. |
| 221 | func (m *metricMap) Describe(ch chan<- *Desc) { |
| 222 | ch <- m.desc |
| 223 | } |
| 224 | |
| 225 | // Collect implements Collector. |
| 226 | func (m *metricMap) Collect(ch chan<- Metric) { |
| 227 | m.mtx.RLock() |
| 228 | defer m.mtx.RUnlock() |
| 229 | |
| 230 | for _, metrics := range m.metrics { |
| 231 | for _, metric := range metrics { |
| 232 | ch <- metric.metric |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Reset deletes all metrics in this vector. |
| 238 | func (m *metricMap) Reset() { |
| 239 | m.mtx.Lock() |
| 240 | defer m.mtx.Unlock() |
| 241 | |
| 242 | for h := range m.metrics { |
| 243 | delete(m.metrics, h) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // deleteByHashWithLabelValues removes the metric from the hash bucket h. If |
| 248 | // there are multiple matches in the bucket, use lvs to select a metric and |
| 249 | // remove only that metric. |
| 250 | func (m *metricMap) deleteByHashWithLabelValues( |
| 251 | h uint64, lvs []string, curry []curriedLabelValue, |
| 252 | ) bool { |
| 253 | m.mtx.Lock() |
| 254 | defer m.mtx.Unlock() |
| 255 | |
| 256 | metrics, ok := m.metrics[h] |
| 257 | if !ok { |
| 258 | return false |
| 259 | } |
| 260 | |
| 261 | i := findMetricWithLabelValues(metrics, lvs, curry) |
| 262 | if i >= len(metrics) { |
| 263 | return false |
| 264 | } |
| 265 | |
| 266 | if len(metrics) > 1 { |
| 267 | m.metrics[h] = append(metrics[:i], metrics[i+1:]...) |
| 268 | } else { |
| 269 | delete(m.metrics, h) |
| 270 | } |
| 271 | return true |
| 272 | } |
| 273 | |
| 274 | // deleteByHashWithLabels removes the metric from the hash bucket h. If there |
| 275 | // are multiple matches in the bucket, use lvs to select a metric and remove |
| 276 | // only that metric. |
| 277 | func (m *metricMap) deleteByHashWithLabels( |
| 278 | h uint64, labels Labels, curry []curriedLabelValue, |
| 279 | ) bool { |
| 280 | m.mtx.Lock() |
| 281 | defer m.mtx.Unlock() |
| 282 | |
| 283 | metrics, ok := m.metrics[h] |
| 284 | if !ok { |
| 285 | return false |
| 286 | } |
| 287 | i := findMetricWithLabels(m.desc, metrics, labels, curry) |
| 288 | if i >= len(metrics) { |
| 289 | return false |
| 290 | } |
| 291 | |
| 292 | if len(metrics) > 1 { |
| 293 | m.metrics[h] = append(metrics[:i], metrics[i+1:]...) |
| 294 | } else { |
| 295 | delete(m.metrics, h) |
| 296 | } |
| 297 | return true |
| 298 | } |
| 299 | |
| 300 | // getOrCreateMetricWithLabelValues retrieves the metric by hash and label value |
| 301 | // or creates it and returns the new one. |
| 302 | // |
| 303 | // This function holds the mutex. |
| 304 | func (m *metricMap) getOrCreateMetricWithLabelValues( |
| 305 | hash uint64, lvs []string, curry []curriedLabelValue, |
| 306 | ) Metric { |
| 307 | m.mtx.RLock() |
| 308 | metric, ok := m.getMetricWithHashAndLabelValues(hash, lvs, curry) |
| 309 | m.mtx.RUnlock() |
| 310 | if ok { |
| 311 | return metric |
| 312 | } |
| 313 | |
| 314 | m.mtx.Lock() |
| 315 | defer m.mtx.Unlock() |
| 316 | metric, ok = m.getMetricWithHashAndLabelValues(hash, lvs, curry) |
| 317 | if !ok { |
| 318 | inlinedLVs := inlineLabelValues(lvs, curry) |
| 319 | metric = m.newMetric(inlinedLVs...) |
| 320 | m.metrics[hash] = append(m.metrics[hash], metricWithLabelValues{values: inlinedLVs, metric: metric}) |
| 321 | } |
| 322 | return metric |
| 323 | } |
| 324 | |
| 325 | // getOrCreateMetricWithLabelValues retrieves the metric by hash and label value |
| 326 | // or creates it and returns the new one. |
| 327 | // |
| 328 | // This function holds the mutex. |
| 329 | func (m *metricMap) getOrCreateMetricWithLabels( |
| 330 | hash uint64, labels Labels, curry []curriedLabelValue, |
| 331 | ) Metric { |
| 332 | m.mtx.RLock() |
| 333 | metric, ok := m.getMetricWithHashAndLabels(hash, labels, curry) |
| 334 | m.mtx.RUnlock() |
| 335 | if ok { |
| 336 | return metric |
| 337 | } |
| 338 | |
| 339 | m.mtx.Lock() |
| 340 | defer m.mtx.Unlock() |
| 341 | metric, ok = m.getMetricWithHashAndLabels(hash, labels, curry) |
| 342 | if !ok { |
| 343 | lvs := extractLabelValues(m.desc, labels, curry) |
| 344 | metric = m.newMetric(lvs...) |
| 345 | m.metrics[hash] = append(m.metrics[hash], metricWithLabelValues{values: lvs, metric: metric}) |
| 346 | } |
| 347 | return metric |
| 348 | } |
| 349 | |
| 350 | // getMetricWithHashAndLabelValues gets a metric while handling possible |
| 351 | // collisions in the hash space. Must be called while holding the read mutex. |
| 352 | func (m *metricMap) getMetricWithHashAndLabelValues( |
| 353 | h uint64, lvs []string, curry []curriedLabelValue, |
| 354 | ) (Metric, bool) { |
| 355 | metrics, ok := m.metrics[h] |
| 356 | if ok { |
| 357 | if i := findMetricWithLabelValues(metrics, lvs, curry); i < len(metrics) { |
| 358 | return metrics[i].metric, true |
| 359 | } |
| 360 | } |
| 361 | return nil, false |
| 362 | } |
| 363 | |
| 364 | // getMetricWithHashAndLabels gets a metric while handling possible collisions in |
| 365 | // the hash space. Must be called while holding read mutex. |
| 366 | func (m *metricMap) getMetricWithHashAndLabels( |
| 367 | h uint64, labels Labels, curry []curriedLabelValue, |
| 368 | ) (Metric, bool) { |
| 369 | metrics, ok := m.metrics[h] |
| 370 | if ok { |
| 371 | if i := findMetricWithLabels(m.desc, metrics, labels, curry); i < len(metrics) { |
| 372 | return metrics[i].metric, true |
| 373 | } |
| 374 | } |
| 375 | return nil, false |
| 376 | } |
| 377 | |
| 378 | // findMetricWithLabelValues returns the index of the matching metric or |
| 379 | // len(metrics) if not found. |
| 380 | func findMetricWithLabelValues( |
| 381 | metrics []metricWithLabelValues, lvs []string, curry []curriedLabelValue, |
| 382 | ) int { |
| 383 | for i, metric := range metrics { |
| 384 | if matchLabelValues(metric.values, lvs, curry) { |
| 385 | return i |
| 386 | } |
| 387 | } |
| 388 | return len(metrics) |
| 389 | } |
| 390 | |
| 391 | // findMetricWithLabels returns the index of the matching metric or len(metrics) |
| 392 | // if not found. |
| 393 | func findMetricWithLabels( |
| 394 | desc *Desc, metrics []metricWithLabelValues, labels Labels, curry []curriedLabelValue, |
| 395 | ) int { |
| 396 | for i, metric := range metrics { |
| 397 | if matchLabels(desc, metric.values, labels, curry) { |
| 398 | return i |
| 399 | } |
| 400 | } |
| 401 | return len(metrics) |
| 402 | } |
| 403 | |
| 404 | func matchLabelValues(values []string, lvs []string, curry []curriedLabelValue) bool { |
| 405 | if len(values) != len(lvs)+len(curry) { |
| 406 | return false |
| 407 | } |
| 408 | var iLVs, iCurry int |
| 409 | for i, v := range values { |
| 410 | if iCurry < len(curry) && curry[iCurry].index == i { |
| 411 | if v != curry[iCurry].value { |
| 412 | return false |
| 413 | } |
| 414 | iCurry++ |
| 415 | continue |
| 416 | } |
| 417 | if v != lvs[iLVs] { |
| 418 | return false |
| 419 | } |
| 420 | iLVs++ |
| 421 | } |
| 422 | return true |
| 423 | } |
| 424 | |
| 425 | func matchLabels(desc *Desc, values []string, labels Labels, curry []curriedLabelValue) bool { |
| 426 | if len(values) != len(labels)+len(curry) { |
| 427 | return false |
| 428 | } |
| 429 | iCurry := 0 |
| 430 | for i, k := range desc.variableLabels { |
| 431 | if iCurry < len(curry) && curry[iCurry].index == i { |
| 432 | if values[i] != curry[iCurry].value { |
| 433 | return false |
| 434 | } |
| 435 | iCurry++ |
| 436 | continue |
| 437 | } |
| 438 | if values[i] != labels[k] { |
| 439 | return false |
| 440 | } |
| 441 | } |
| 442 | return true |
| 443 | } |
| 444 | |
| 445 | func extractLabelValues(desc *Desc, labels Labels, curry []curriedLabelValue) []string { |
| 446 | labelValues := make([]string, len(labels)+len(curry)) |
| 447 | iCurry := 0 |
| 448 | for i, k := range desc.variableLabels { |
| 449 | if iCurry < len(curry) && curry[iCurry].index == i { |
| 450 | labelValues[i] = curry[iCurry].value |
| 451 | iCurry++ |
| 452 | continue |
| 453 | } |
| 454 | labelValues[i] = labels[k] |
| 455 | } |
| 456 | return labelValues |
| 457 | } |
| 458 | |
| 459 | func inlineLabelValues(lvs []string, curry []curriedLabelValue) []string { |
| 460 | labelValues := make([]string, len(lvs)+len(curry)) |
| 461 | var iCurry, iLVs int |
| 462 | for i := range labelValues { |
| 463 | if iCurry < len(curry) && curry[iCurry].index == i { |
| 464 | labelValues[i] = curry[iCurry].value |
| 465 | iCurry++ |
| 466 | continue |
| 467 | } |
| 468 | labelValues[i] = lvs[iLVs] |
| 469 | iLVs++ |
| 470 | } |
| 471 | return labelValues |
| 472 | } |