| 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 but as a building block |
| 25 |
// for implementations of vectors of a given metric type, like GaugeVec, |
| 26 |
// CounterVec, SummaryVec, and HistogramVec. It is exported so that it can be |
| 27 |
// used for custom Metric implementations. |
| 28 |
// |
| 29 |
// To create a FooVec for custom Metric Foo, embed a pointer to MetricVec in |
| 30 |
// FooVec and initialize it with NewMetricVec. Implement wrappers for |
| 31 |
// GetMetricWithLabelValues and GetMetricWith that return (Foo, error) rather |
| 32 |
// than (Metric, error). Similarly, create a wrapper for CurryWith that returns |
| 33 |
// (*FooVec, error) rather than (*MetricVec, error). It is recommended to also |
| 34 |
// add the convenience methods WithLabelValues, With, and MustCurryWith, which |
| 35 |
// panic instead of returning errors. See also the MetricVec example. |
| 36 |
type MetricVec struct { |
| 37 |
*metricMap |
| 38 |
|
| 39 |
curry []curriedLabelValue |
| 40 |
|
| 41 |
// hashAdd and hashAddByte can be replaced for testing collision handling. |
| 42 |
hashAdd func(h uint64, s string) uint64 |
| 43 |
hashAddByte func(h uint64, b byte) uint64 |
| 44 |
} |
| 45 |
|
| 46 |
// NewMetricVec returns an initialized metricVec. |
| 47 |
func NewMetricVec(desc *Desc, newMetric func(lvs ...string) Metric) *MetricVec { |
| 48 |
return &MetricVec{ |
| 49 |
metricMap: &metricMap{ |
| 50 |
metrics: map[uint64][]metricWithLabelValues{}, |
| 51 |
desc: desc, |
| 52 |
newMetric: newMetric, |
| 53 |
}, |
| 54 |
hashAdd: hashAdd, |
| 55 |
hashAddByte: hashAddByte, |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// DeleteLabelValues removes the metric where the variable labels are the same |
| 60 |
// as those passed in as labels (same order as the VariableLabels in Desc). It |
| 61 |
// returns true if a metric was deleted. |
| 62 |
// |
| 63 |
// It is not an error if the number of label values is not the same as the |
| 64 |
// number of VariableLabels in Desc. However, such inconsistent label count can |
| 65 |
// never match an actual metric, so the method will always return false in that |
| 66 |
// case. |
| 67 |
// |
| 68 |
// Note that for more than one label value, this method is prone to mistakes |
| 69 |
// caused by an incorrect order of arguments. Consider Delete(Labels) as an |
| 70 |
// alternative to avoid that type of mistake. For higher label numbers, the |
| 71 |
// latter has a much more readable (albeit more verbose) syntax, but it comes |
| 72 |
// with a performance overhead (for creating and processing the Labels map). |
| 73 |
// See also the CounterVec example. |
| 74 |
func (m *MetricVec) DeleteLabelValues(lvs ...string) bool { |
| 75 |
lvs = constrainLabelValues(m.desc, lvs, m.curry) |
| 76 |
|
| 77 |
h, err := m.hashLabelValues(lvs) |
| 78 |
if err != nil { |
| 79 |
return false |
| 80 |
} |
| 81 |
|
| 82 |
return m.deleteByHashWithLabelValues(h, lvs, m.curry) |
| 83 |
} |
| 84 |
|
| 85 |
// Delete deletes the metric where the variable labels are the same as those |
| 86 |
// passed in as labels. It returns true if a metric was deleted. |
| 87 |
// |
| 88 |
// It is not an error if the number and names of the Labels are inconsistent |
| 89 |
// with those of the VariableLabels in Desc. However, such inconsistent Labels |
| 90 |
// can never match an actual metric, so the method will always return false in |
| 91 |
// that case. |
| 92 |
// |
| 93 |
// This method is used for the same purpose as DeleteLabelValues(...string). See |
| 94 |
// there for pros and cons of the two methods. |
| 95 |
func (m *MetricVec) Delete(labels Labels) bool { |
| 96 |
labels, closer := constrainLabels(m.desc, labels) |
| 97 |
defer closer() |
| 98 |
|
| 99 |
h, err := m.hashLabels(labels) |
| 100 |
if err != nil { |
| 101 |
return false |
| 102 |
} |
| 103 |
|
| 104 |
return m.deleteByHashWithLabels(h, labels, m.curry) |
| 105 |
} |
| 106 |
|
| 107 |
// DeletePartialMatch deletes all metrics where the variable labels contain all of those |
| 108 |
// passed in as labels. The order of the labels does not matter. |
| 109 |
// It returns the number of metrics deleted. |
| 110 |
// |
| 111 |
// Note that curried labels will never be matched if deleting from the curried vector. |
| 112 |
// To match curried labels with DeletePartialMatch, it must be called on the base vector. |
| 113 |
func (m *MetricVec) DeletePartialMatch(labels Labels) int { |
| 114 |
labels, closer := constrainLabels(m.desc, labels) |
| 115 |
defer closer() |
| 116 |
|
| 117 |
return m.deleteByLabels(labels, m.curry) |
| 118 |
} |
| 119 |
|
| 120 |
// Without explicit forwarding of Describe, Collect, Reset, those methods won't |
| 121 |
// show up in GoDoc. |
| 122 |
|
| 123 |
// Describe implements Collector. |
| 124 |
func (m *MetricVec) Describe(ch chan<- *Desc) { m.metricMap.Describe(ch) } |
| 125 |
|
| 126 |
// Collect implements Collector. |
| 127 |
func (m *MetricVec) Collect(ch chan<- Metric) { m.metricMap.Collect(ch) } |
| 128 |
|
| 129 |
// Reset deletes all metrics in this vector. |
| 130 |
func (m *MetricVec) Reset() { m.metricMap.Reset() } |
| 131 |
|
| 132 |
// CurryWith returns a vector curried with the provided labels, i.e. the |
| 133 |
// returned vector has those labels pre-set for all labeled operations performed |
| 134 |
// on it. The cardinality of the curried vector is reduced accordingly. The |
| 135 |
// order of the remaining labels stays the same (just with the curried labels |
| 136 |
// taken out of the sequence – which is relevant for the |
| 137 |
// (GetMetric)WithLabelValues methods). It is possible to curry a curried |
| 138 |
// vector, but only with labels not yet used for currying before. |
| 139 |
// |
| 140 |
// The metrics contained in the MetricVec are shared between the curried and |
| 141 |
// uncurried vectors. They are just accessed differently. Curried and uncurried |
| 142 |
// vectors behave identically in terms of collection. Only one must be |
| 143 |
// registered with a given registry (usually the uncurried version). The Reset |
| 144 |
// method deletes all metrics, even if called on a curried vector. |
| 145 |
// |
| 146 |
// Note that CurryWith is usually not called directly but through a wrapper |
| 147 |
// around MetricVec, implementing a vector for a specific Metric |
| 148 |
// implementation, for example GaugeVec. |
| 149 |
func (m *MetricVec) CurryWith(labels Labels) (*MetricVec, error) { |
| 150 |
var ( |
| 151 |
newCurry []curriedLabelValue |
| 152 |
oldCurry = m.curry |
| 153 |
iCurry int |
| 154 |
) |
| 155 |
for i, labelName := range m.desc.variableLabels.names { |
| 156 |
val, ok := labels[labelName] |
| 157 |
if iCurry < len(oldCurry) && oldCurry[iCurry].index == i { |
| 158 |
if ok { |
| 159 |
return nil, fmt.Errorf("label name %q is already curried", labelName) |
| 160 |
} |
| 161 |
newCurry = append(newCurry, oldCurry[iCurry]) |
| 162 |
iCurry++ |
| 163 |
} else { |
| 164 |
if !ok { |
| 165 |
continue // Label stays uncurried. |
| 166 |
} |
| 167 |
newCurry = append(newCurry, curriedLabelValue{ |
| 168 |
i, |
| 169 |
m.desc.variableLabels.constrain(labelName, val), |
| 170 |
}) |
| 171 |
} |
| 172 |
} |
| 173 |
if l := len(oldCurry) + len(labels) - len(newCurry); l > 0 { |
| 174 |
return nil, fmt.Errorf("%d unknown label(s) found during currying", l) |
| 175 |
} |
| 176 |
|
| 177 |
return &MetricVec{ |
| 178 |
metricMap: m.metricMap, |
| 179 |
curry: newCurry, |
| 180 |
hashAdd: m.hashAdd, |
| 181 |
hashAddByte: m.hashAddByte, |
| 182 |
}, nil |
| 183 |
} |
| 184 |
|
| 185 |
// GetMetricWithLabelValues returns the Metric for the given slice of label |
| 186 |
// values (same order as the variable labels in Desc). If that combination of |
| 187 |
// label values is accessed for the first time, a new Metric is created (by |
| 188 |
// calling the newMetric function provided during construction of the |
| 189 |
// MetricVec). |
| 190 |
// |
| 191 |
// It is possible to call this method without using the returned Metric to only |
| 192 |
// create the new Metric but leave it in its initial state. |
| 193 |
// |
| 194 |
// Keeping the Metric for later use is possible (and should be considered if |
| 195 |
// performance is critical), but keep in mind that Reset, DeleteLabelValues and |
| 196 |
// Delete can be used to delete the Metric from the MetricVec. In that case, the |
| 197 |
// Metric will still exist, but it will not be exported anymore, even if a |
| 198 |
// Metric with the same label values is created later. |
| 199 |
// |
| 200 |
// An error is returned if the number of label values is not the same as the |
| 201 |
// number of variable labels in Desc (minus any curried labels). |
| 202 |
// |
| 203 |
// Note that for more than one label value, this method is prone to mistakes |
| 204 |
// caused by an incorrect order of arguments. Consider GetMetricWith(Labels) as |
| 205 |
// an alternative to avoid that type of mistake. For higher label numbers, the |
| 206 |
// latter has a much more readable (albeit more verbose) syntax, but it comes |
| 207 |
// with a performance overhead (for creating and processing the Labels map). |
| 208 |
// |
| 209 |
// Note that GetMetricWithLabelValues is usually not called directly but through |
| 210 |
// a wrapper around MetricVec, implementing a vector for a specific Metric |
| 211 |
// implementation, for example GaugeVec. |
| 212 |
func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) { |
| 213 |
lvs = constrainLabelValues(m.desc, lvs, m.curry) |
| 214 |
h, err := m.hashLabelValues(lvs) |
| 215 |
if err != nil { |
| 216 |
return nil, err |
| 217 |
} |
| 218 |
|
| 219 |
return m.getOrCreateMetricWithLabelValues(h, lvs, m.curry), nil |
| 220 |
} |
| 221 |
|
| 222 |
// GetMetricWith returns the Metric for the given Labels map (the label names |
| 223 |
// must match those of the variable labels in Desc). If that label map is |
| 224 |
// accessed for the first time, a new Metric is created. Implications of |
| 225 |
// creating a Metric without using it and keeping the Metric for later use |
| 226 |
// are the same as for GetMetricWithLabelValues. |
| 227 |
// |
| 228 |
// An error is returned if the number and names of the Labels are inconsistent |
| 229 |
// with those of the variable labels in Desc (minus any curried labels). |
| 230 |
// |
| 231 |
// This method is used for the same purpose as |
| 232 |
// GetMetricWithLabelValues(...string). See there for pros and cons of the two |
| 233 |
// methods. |
| 234 |
// |
| 235 |
// Note that GetMetricWith is usually not called directly but through a wrapper |
| 236 |
// around MetricVec, implementing a vector for a specific Metric implementation, |
| 237 |
// for example GaugeVec. |
| 238 |
func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) { |
| 239 |
labels, closer := constrainLabels(m.desc, labels) |
| 240 |
defer closer() |
| 241 |
|
| 242 |
h, err := m.hashLabels(labels) |
| 243 |
if err != nil { |
| 244 |
return nil, err |
| 245 |
} |
| 246 |
|
| 247 |
return m.getOrCreateMetricWithLabels(h, labels, m.curry), nil |
| 248 |
} |
| 249 |
|
| 250 |
func (m *MetricVec) hashLabelValues(vals []string) (uint64, error) { |
| 251 |
if err := validateLabelValues(vals, len(m.desc.variableLabels.names)-len(m.curry)); err != nil { |
| 252 |
return 0, err |
| 253 |
} |
| 254 |
|
| 255 |
var ( |
| 256 |
h = hashNew() |
| 257 |
curry = m.curry |
| 258 |
iVals, iCurry int |
| 259 |
) |
| 260 |
for i := 0; i < len(m.desc.variableLabels.names); i++ { |
| 261 |
if iCurry < len(curry) && curry[iCurry].index == i { |
| 262 |
h = m.hashAdd(h, curry[iCurry].value) |
| 263 |
iCurry++ |
| 264 |
} else { |
| 265 |
h = m.hashAdd(h, vals[iVals]) |
| 266 |
iVals++ |
| 267 |
} |
| 268 |
h = m.hashAddByte(h, model.SeparatorByte) |
| 269 |
} |
| 270 |
return h, nil |
| 271 |
} |
| 272 |
|
| 273 |
func (m *MetricVec) hashLabels(labels Labels) (uint64, error) { |
| 274 |
if err := validateValuesInLabels(labels, len(m.desc.variableLabels.names)-len(m.curry)); err != nil { |
| 275 |
return 0, err |
| 276 |
} |
| 277 |
|
| 278 |
var ( |
| 279 |
h = hashNew() |
| 280 |
curry = m.curry |
| 281 |
iCurry int |
| 282 |
) |
| 283 |
for i, labelName := range m.desc.variableLabels.names { |
| 284 |
val, ok := labels[labelName] |
| 285 |
if iCurry < len(curry) && curry[iCurry].index == i { |
| 286 |
if ok { |
| 287 |
return 0, fmt.Errorf("label name %q is already curried", labelName) |
| 288 |
} |
| 289 |
h = m.hashAdd(h, curry[iCurry].value) |
| 290 |
iCurry++ |
| 291 |
} else { |
| 292 |
if !ok { |
| 293 |
return 0, fmt.Errorf("label name %q missing in label map", labelName) |
| 294 |
} |
| 295 |
h = m.hashAdd(h, val) |
| 296 |
} |
| 297 |
h = m.hashAddByte(h, model.SeparatorByte) |
| 298 |
} |
| 299 |
return h, nil |
| 300 |
} |
| 301 |
|
| 302 |
// metricWithLabelValues provides the metric and its label values for |
| 303 |
// disambiguation on hash collision. |
| 304 |
type metricWithLabelValues struct { |
| 305 |
values []string |
| 306 |
metric Metric |
| 307 |
} |
| 308 |
|
| 309 |
// curriedLabelValue sets the curried value for a label at the given index. |
| 310 |
type curriedLabelValue struct { |
| 311 |
index int |
| 312 |
value string |
| 313 |
} |
| 314 |
|
| 315 |
// metricMap is a helper for metricVec and shared between differently curried |
| 316 |
// metricVecs. |
| 317 |
type metricMap struct { |
| 318 |
mtx sync.RWMutex // Protects metrics. |
| 319 |
metrics map[uint64][]metricWithLabelValues |
| 320 |
desc *Desc |
| 321 |
newMetric func(labelValues ...string) Metric |
| 322 |
} |
| 323 |
|
| 324 |
// Describe implements Collector. It will send exactly one Desc to the provided |
| 325 |
// channel. |
| 326 |
func (m *metricMap) Describe(ch chan<- *Desc) { |
| 327 |
ch <- m.desc |
| 328 |
} |
| 329 |
|
| 330 |
// Collect implements Collector. |
| 331 |
func (m *metricMap) Collect(ch chan<- Metric) { |
| 332 |
m.mtx.RLock() |
| 333 |
defer m.mtx.RUnlock() |
| 334 |
|
| 335 |
for _, metrics := range m.metrics { |
| 336 |
for _, metric := range metrics { |
| 337 |
ch <- metric.metric |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
// Reset deletes all metrics in this vector. |
| 343 |
func (m *metricMap) Reset() { |
| 344 |
m.mtx.Lock() |
| 345 |
defer m.mtx.Unlock() |
| 346 |
|
| 347 |
for h := range m.metrics { |
| 348 |
delete(m.metrics, h) |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// deleteByHashWithLabelValues removes the metric from the hash bucket h. If |
| 353 |
// there are multiple matches in the bucket, use lvs to select a metric and |
| 354 |
// remove only that metric. |
| 355 |
func (m *metricMap) deleteByHashWithLabelValues( |
| 356 |
h uint64, lvs []string, curry []curriedLabelValue, |
| 357 |
) bool { |
| 358 |
m.mtx.Lock() |
| 359 |
defer m.mtx.Unlock() |
| 360 |
|
| 361 |
metrics, ok := m.metrics[h] |
| 362 |
if !ok { |
| 363 |
return false |
| 364 |
} |
| 365 |
|
| 366 |
i := findMetricWithLabelValues(metrics, lvs, curry) |
| 367 |
if i >= len(metrics) { |
| 368 |
return false |
| 369 |
} |
| 370 |
|
| 371 |
if len(metrics) > 1 { |
| 372 |
old := metrics |
| 373 |
m.metrics[h] = append(metrics[:i], metrics[i+1:]...) |
| 374 |
old[len(old)-1] = metricWithLabelValues{} |
| 375 |
} else { |
| 376 |
delete(m.metrics, h) |
| 377 |
} |
| 378 |
return true |
| 379 |
} |
| 380 |
|
| 381 |
// deleteByHashWithLabels removes the metric from the hash bucket h. If there |
| 382 |
// are multiple matches in the bucket, use lvs to select a metric and remove |
| 383 |
// only that metric. |
| 384 |
func (m *metricMap) deleteByHashWithLabels( |
| 385 |
h uint64, labels Labels, curry []curriedLabelValue, |
| 386 |
) bool { |
| 387 |
m.mtx.Lock() |
| 388 |
defer m.mtx.Unlock() |
| 389 |
|
| 390 |
metrics, ok := m.metrics[h] |
| 391 |
if !ok { |
| 392 |
return false |
| 393 |
} |
| 394 |
i := findMetricWithLabels(m.desc, metrics, labels, curry) |
| 395 |
if i >= len(metrics) { |
| 396 |
return false |
| 397 |
} |
| 398 |
|
| 399 |
if len(metrics) > 1 { |
| 400 |
old := metrics |
| 401 |
m.metrics[h] = append(metrics[:i], metrics[i+1:]...) |
| 402 |
old[len(old)-1] = metricWithLabelValues{} |
| 403 |
} else { |
| 404 |
delete(m.metrics, h) |
| 405 |
} |
| 406 |
return true |
| 407 |
} |
| 408 |
|
| 409 |
// deleteByLabels deletes a metric if the given labels are present in the metric. |
| 410 |
func (m *metricMap) deleteByLabels(labels Labels, curry []curriedLabelValue) int { |
| 411 |
m.mtx.Lock() |
| 412 |
defer m.mtx.Unlock() |
| 413 |
|
| 414 |
var numDeleted int |
| 415 |
|
| 416 |
for h, metrics := range m.metrics { |
| 417 |
i := findMetricWithPartialLabels(m.desc, metrics, labels, curry) |
| 418 |
if i >= len(metrics) { |
| 419 |
// Didn't find matching labels in this metric slice. |
| 420 |
continue |
| 421 |
} |
| 422 |
delete(m.metrics, h) |
| 423 |
numDeleted++ |
| 424 |
} |
| 425 |
|
| 426 |
return numDeleted |
| 427 |
} |
| 428 |
|
| 429 |
// findMetricWithPartialLabel returns the index of the matching metric or |
| 430 |
// len(metrics) if not found. |
| 431 |
func findMetricWithPartialLabels( |
| 432 |
desc *Desc, metrics []metricWithLabelValues, labels Labels, curry []curriedLabelValue, |
| 433 |
) int { |
| 434 |
for i, metric := range metrics { |
| 435 |
if matchPartialLabels(desc, metric.values, labels, curry) { |
| 436 |
return i |
| 437 |
} |
| 438 |
} |
| 439 |
return len(metrics) |
| 440 |
} |
| 441 |
|
| 442 |
// indexOf searches the given slice of strings for the target string and returns |
| 443 |
// the index or len(items) as well as a boolean whether the search succeeded. |
| 444 |
func indexOf(target string, items []string) (int, bool) { |
| 445 |
for i, l := range items { |
| 446 |
if l == target { |
| 447 |
return i, true |
| 448 |
} |
| 449 |
} |
| 450 |
return len(items), false |
| 451 |
} |
| 452 |
|
| 453 |
// valueMatchesVariableOrCurriedValue determines if a value was previously curried, |
| 454 |
// and returns whether it matches either the "base" value or the curried value accordingly. |
| 455 |
// It also indicates whether the match is against a curried or uncurried value. |
| 456 |
func valueMatchesVariableOrCurriedValue(targetValue string, index int, values []string, curry []curriedLabelValue) (bool, bool) { |
| 457 |
for _, curriedValue := range curry { |
| 458 |
if curriedValue.index == index { |
| 459 |
// This label was curried. See if the curried value matches our target. |
| 460 |
return curriedValue.value == targetValue, true |
| 461 |
} |
| 462 |
} |
| 463 |
// This label was not curried. See if the current value matches our target label. |
| 464 |
return values[index] == targetValue, false |
| 465 |
} |
| 466 |
|
| 467 |
// matchPartialLabels searches the current metric and returns whether all of the target label:value pairs are present. |
| 468 |
func matchPartialLabels(desc *Desc, values []string, labels Labels, curry []curriedLabelValue) bool { |
| 469 |
for l, v := range labels { |
| 470 |
// Check if the target label exists in our metrics and get the index. |
| 471 |
varLabelIndex, validLabel := indexOf(l, desc.variableLabels.names) |
| 472 |
if validLabel { |
| 473 |
// Check the value of that label against the target value. |
| 474 |
// We don't consider curried values in partial matches. |
| 475 |
matches, curried := valueMatchesVariableOrCurriedValue(v, varLabelIndex, values, curry) |
| 476 |
if matches && !curried { |
| 477 |
continue |
| 478 |
} |
| 479 |
} |
| 480 |
return false |
| 481 |
} |
| 482 |
return true |
| 483 |
} |
| 484 |
|
| 485 |
// getOrCreateMetricWithLabelValues retrieves the metric by hash and label value |
| 486 |
// or creates it and returns the new one. |
| 487 |
// |
| 488 |
// This function holds the mutex. |
| 489 |
func (m *metricMap) getOrCreateMetricWithLabelValues( |
| 490 |
hash uint64, lvs []string, curry []curriedLabelValue, |
| 491 |
) Metric { |
| 492 |
m.mtx.RLock() |
| 493 |
metric, ok := m.getMetricWithHashAndLabelValues(hash, lvs, curry) |
| 494 |
m.mtx.RUnlock() |
| 495 |
if ok { |
| 496 |
return metric |
| 497 |
} |
| 498 |
|
| 499 |
m.mtx.Lock() |
| 500 |
defer m.mtx.Unlock() |
| 501 |
metric, ok = m.getMetricWithHashAndLabelValues(hash, lvs, curry) |
| 502 |
if !ok { |
| 503 |
inlinedLVs := inlineLabelValues(lvs, curry) |
| 504 |
metric = m.newMetric(inlinedLVs...) |
| 505 |
m.metrics[hash] = append(m.metrics[hash], metricWithLabelValues{values: inlinedLVs, metric: metric}) |
| 506 |
} |
| 507 |
return metric |
| 508 |
} |
| 509 |
|
| 510 |
// getOrCreateMetricWithLabels retrieves the metric by hash and label value |
| 511 |
// or creates it and returns the new one. |
| 512 |
// |
| 513 |
// This function holds the mutex. |
| 514 |
func (m *metricMap) getOrCreateMetricWithLabels( |
| 515 |
hash uint64, labels Labels, curry []curriedLabelValue, |
| 516 |
) Metric { |
| 517 |
m.mtx.RLock() |
| 518 |
metric, ok := m.getMetricWithHashAndLabels(hash, labels, curry) |
| 519 |
m.mtx.RUnlock() |
| 520 |
if ok { |
| 521 |
return metric |
| 522 |
} |
| 523 |
|
| 524 |
m.mtx.Lock() |
| 525 |
defer m.mtx.Unlock() |
| 526 |
metric, ok = m.getMetricWithHashAndLabels(hash, labels, curry) |
| 527 |
if !ok { |
| 528 |
lvs := extractLabelValues(m.desc, labels, curry) |
| 529 |
metric = m.newMetric(lvs...) |
| 530 |
m.metrics[hash] = append(m.metrics[hash], metricWithLabelValues{values: lvs, metric: metric}) |
| 531 |
} |
| 532 |
return metric |
| 533 |
} |
| 534 |
|
| 535 |
// getMetricWithHashAndLabelValues gets a metric while handling possible |
| 536 |
// collisions in the hash space. Must be called while holding the read mutex. |
| 537 |
func (m *metricMap) getMetricWithHashAndLabelValues( |
| 538 |
h uint64, lvs []string, curry []curriedLabelValue, |
| 539 |
) (Metric, bool) { |
| 540 |
metrics, ok := m.metrics[h] |
| 541 |
if ok { |
| 542 |
if i := findMetricWithLabelValues(metrics, lvs, curry); i < len(metrics) { |
| 543 |
return metrics[i].metric, true |
| 544 |
} |
| 545 |
} |
| 546 |
return nil, false |
| 547 |
} |
| 548 |
|
| 549 |
// getMetricWithHashAndLabels gets a metric while handling possible collisions in |
| 550 |
// the hash space. Must be called while holding read mutex. |
| 551 |
func (m *metricMap) getMetricWithHashAndLabels( |
| 552 |
h uint64, labels Labels, curry []curriedLabelValue, |
| 553 |
) (Metric, bool) { |
| 554 |
metrics, ok := m.metrics[h] |
| 555 |
if ok { |
| 556 |
if i := findMetricWithLabels(m.desc, metrics, labels, curry); i < len(metrics) { |
| 557 |
return metrics[i].metric, true |
| 558 |
} |
| 559 |
} |
| 560 |
return nil, false |
| 561 |
} |
| 562 |
|
| 563 |
// findMetricWithLabelValues returns the index of the matching metric or |
| 564 |
// len(metrics) if not found. |
| 565 |
func findMetricWithLabelValues( |
| 566 |
metrics []metricWithLabelValues, lvs []string, curry []curriedLabelValue, |
| 567 |
) int { |
| 568 |
for i, metric := range metrics { |
| 569 |
if matchLabelValues(metric.values, lvs, curry) { |
| 570 |
return i |
| 571 |
} |
| 572 |
} |
| 573 |
return len(metrics) |
| 574 |
} |
| 575 |
|
| 576 |
// findMetricWithLabels returns the index of the matching metric or len(metrics) |
| 577 |
// if not found. |
| 578 |
func findMetricWithLabels( |
| 579 |
desc *Desc, metrics []metricWithLabelValues, labels Labels, curry []curriedLabelValue, |
| 580 |
) int { |
| 581 |
for i, metric := range metrics { |
| 582 |
if matchLabels(desc, metric.values, labels, curry) { |
| 583 |
return i |
| 584 |
} |
| 585 |
} |
| 586 |
return len(metrics) |
| 587 |
} |
| 588 |
|
| 589 |
func matchLabelValues(values, lvs []string, curry []curriedLabelValue) bool { |
| 590 |
if len(values) != len(lvs)+len(curry) { |
| 591 |
return false |
| 592 |
} |
| 593 |
var iLVs, iCurry int |
| 594 |
for i, v := range values { |
| 595 |
if iCurry < len(curry) && curry[iCurry].index == i { |
| 596 |
if v != curry[iCurry].value { |
| 597 |
return false |
| 598 |
} |
| 599 |
iCurry++ |
| 600 |
continue |
| 601 |
} |
| 602 |
if v != lvs[iLVs] { |
| 603 |
return false |
| 604 |
} |
| 605 |
iLVs++ |
| 606 |
} |
| 607 |
return true |
| 608 |
} |
| 609 |
|
| 610 |
func matchLabels(desc *Desc, values []string, labels Labels, curry []curriedLabelValue) bool { |
| 611 |
if len(values) != len(labels)+len(curry) { |
| 612 |
return false |
| 613 |
} |
| 614 |
iCurry := 0 |
| 615 |
for i, k := range desc.variableLabels.names { |
| 616 |
if iCurry < len(curry) && curry[iCurry].index == i { |
| 617 |
if values[i] != curry[iCurry].value { |
| 618 |
return false |
| 619 |
} |
| 620 |
iCurry++ |
| 621 |
continue |
| 622 |
} |
| 623 |
if values[i] != labels[k] { |
| 624 |
return false |
| 625 |
} |
| 626 |
} |
| 627 |
return true |
| 628 |
} |
| 629 |
|
| 630 |
func extractLabelValues(desc *Desc, labels Labels, curry []curriedLabelValue) []string { |
| 631 |
labelValues := make([]string, len(labels)+len(curry)) |
| 632 |
iCurry := 0 |
| 633 |
for i, k := range desc.variableLabels.names { |
| 634 |
if iCurry < len(curry) && curry[iCurry].index == i { |
| 635 |
labelValues[i] = curry[iCurry].value |
| 636 |
iCurry++ |
| 637 |
continue |
| 638 |
} |
| 639 |
labelValues[i] = labels[k] |
| 640 |
} |
| 641 |
return labelValues |
| 642 |
} |
| 643 |
|
| 644 |
func inlineLabelValues(lvs []string, curry []curriedLabelValue) []string { |
| 645 |
labelValues := make([]string, len(lvs)+len(curry)) |
| 646 |
var iCurry, iLVs int |
| 647 |
for i := range labelValues { |
| 648 |
if iCurry < len(curry) && curry[iCurry].index == i { |
| 649 |
labelValues[i] = curry[iCurry].value |
| 650 |
iCurry++ |
| 651 |
continue |
| 652 |
} |
| 653 |
labelValues[i] = lvs[iLVs] |
| 654 |
iLVs++ |
| 655 |
} |
| 656 |
return labelValues |
| 657 |
} |
| 658 |
|
| 659 |
var labelsPool = &sync.Pool{ |
| 660 |
New: func() interface{} { |
| 661 |
return make(Labels) |
| 662 |
}, |
| 663 |
} |
| 664 |
|
| 665 |
func constrainLabels(desc *Desc, labels Labels) (Labels, func()) { |
| 666 |
if len(desc.variableLabels.labelConstraints) == 0 { |
| 667 |
// Fast path when there's no constraints |
| 668 |
return labels, func() {} |
| 669 |
} |
| 670 |
|
| 671 |
constrainedLabels := labelsPool.Get().(Labels) |
| 672 |
for l, v := range labels { |
| 673 |
constrainedLabels[l] = desc.variableLabels.constrain(l, v) |
| 674 |
} |
| 675 |
|
| 676 |
return constrainedLabels, func() { |
| 677 |
for k := range constrainedLabels { |
| 678 |
delete(constrainedLabels, k) |
| 679 |
} |
| 680 |
labelsPool.Put(constrainedLabels) |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
func constrainLabelValues(desc *Desc, lvs []string, curry []curriedLabelValue) []string { |
| 685 |
if len(desc.variableLabels.labelConstraints) == 0 { |
| 686 |
// Fast path when there's no constraints |
| 687 |
return lvs |
| 688 |
} |
| 689 |
|
| 690 |
constrainedValues := make([]string, len(lvs)) |
| 691 |
var iCurry, iLVs int |
| 692 |
for i := 0; i < len(lvs)+len(curry); i++ { |
| 693 |
if iCurry < len(curry) && curry[iCurry].index == i { |
| 694 |
iCurry++ |
| 695 |
continue |
| 696 |
} |
| 697 |
|
| 698 |
if i < len(desc.variableLabels.names) { |
| 699 |
constrainedValues[iLVs] = desc.variableLabels.constrain( |
| 700 |
desc.variableLabels.names[i], |
| 701 |
lvs[iLVs], |
| 702 |
) |
| 703 |
} else { |
| 704 |
constrainedValues[iLVs] = lvs[iLVs] |
| 705 |
} |
| 706 |
iLVs++ |
| 707 |
} |
| 708 |
return constrainedValues |
| 709 |
} |