vec.go 13 KB

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