2015-02-02 17:14:36 +03:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-07 22:08:33 +04:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2016-08-13 02:20:05 +03:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
2014-05-07 22:08:33 +04:00
|
|
|
)
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
// metricVec is a Collector to bundle metrics of the same name that differ in
|
|
|
|
// their label values. metricVec is not used directly (and therefore
|
|
|
|
// unexported). It is used as a building block for implementations of vectors of
|
|
|
|
// a given metric type, like GaugeVec, CounterVec, SummaryVec, HistogramVec, and
|
|
|
|
// UntypedVec.
|
|
|
|
type metricVec struct {
|
2015-11-09 16:52:51 +03:00
|
|
|
mtx sync.RWMutex // Protects the children.
|
2016-08-13 02:20:05 +03:00
|
|
|
children map[uint64][]metricWithLabelValues
|
2014-05-07 22:08:33 +04:00
|
|
|
desc *Desc
|
|
|
|
|
2016-08-13 02:20:05 +03:00
|
|
|
newMetric func(labelValues ...string) Metric
|
|
|
|
hashAdd func(h uint64, s string) uint64 // replace hash function for testing collision handling
|
|
|
|
hashAddByte func(h uint64, b byte) uint64
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
2016-08-11 06:03:15 +03:00
|
|
|
// newMetricVec returns an initialized MetricVec. The concrete value is
|
|
|
|
// returned for embedding into another struct.
|
2017-06-28 18:55:59 +03:00
|
|
|
func newMetricVec(desc *Desc, newMetric func(lvs ...string) Metric) *metricVec {
|
|
|
|
return &metricVec{
|
2016-08-13 02:20:05 +03:00
|
|
|
children: map[uint64][]metricWithLabelValues{},
|
|
|
|
desc: desc,
|
|
|
|
newMetric: newMetric,
|
|
|
|
hashAdd: hashAdd,
|
|
|
|
hashAddByte: hashAddByte,
|
2016-08-11 06:03:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 02:20:05 +03:00
|
|
|
// metricWithLabelValues provides the metric and its label values for
|
|
|
|
// disambiguation on hash collision.
|
|
|
|
type metricWithLabelValues struct {
|
2016-08-12 03:06:03 +03:00
|
|
|
values []string
|
|
|
|
metric Metric
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
// Describe implements Collector. The length of the returned slice
|
|
|
|
// is always one.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) Describe(ch chan<- *Desc) {
|
2014-05-07 22:08:33 +04:00
|
|
|
ch <- m.desc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements Collector.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) Collect(ch chan<- Metric) {
|
2014-05-07 22:08:33 +04:00
|
|
|
m.mtx.RLock()
|
|
|
|
defer m.mtx.RUnlock()
|
|
|
|
|
2016-08-12 03:06:03 +03:00
|
|
|
for _, metrics := range m.children {
|
|
|
|
for _, metric := range metrics {
|
|
|
|
ch <- metric.metric
|
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) getMetricWithLabelValues(lvs ...string) (Metric, error) {
|
2014-05-07 22:08:33 +04:00
|
|
|
h, err := m.hashLabelValues(lvs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-09 17:00:46 +03:00
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
return m.getOrCreateMetricWithLabelValues(h, lvs), nil
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) getMetricWith(labels Labels) (Metric, error) {
|
2014-05-07 22:08:33 +04:00
|
|
|
h, err := m.hashLabels(labels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-09 17:00:46 +03:00
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
return m.getOrCreateMetricWithLabels(h, labels), nil
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) withLabelValues(lvs ...string) Metric {
|
|
|
|
metric, err := m.getMetricWithLabelValues(lvs...)
|
2014-05-07 22:08:33 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return metric
|
|
|
|
}
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) with(labels Labels) Metric {
|
|
|
|
metric, err := m.getMetricWith(labels)
|
2014-05-07 22:08:33 +04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return metric
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLabelValues removes the metric where the variable labels are the same
|
2014-09-19 15:20:59 +04:00
|
|
|
// as those passed in as labels (same order as the VariableLabels in Desc). It
|
2014-05-07 22:08:33 +04:00
|
|
|
// returns true if a metric was deleted.
|
|
|
|
//
|
|
|
|
// It is not an error if the number of label values is not the same as the
|
2017-06-28 18:55:59 +03:00
|
|
|
// number of VariableLabels in Desc. However, such inconsistent label count can
|
|
|
|
// never match an actual metric, so the method will always return false in that
|
2014-05-07 22:08:33 +04:00
|
|
|
// case.
|
|
|
|
//
|
|
|
|
// Note that for more than one label value, this method is prone to mistakes
|
|
|
|
// caused by an incorrect order of arguments. Consider Delete(Labels) as an
|
|
|
|
// alternative to avoid that type of mistake. For higher label numbers, the
|
|
|
|
// latter has a much more readable (albeit more verbose) syntax, but it comes
|
|
|
|
// with a performance overhead (for creating and processing the Labels map).
|
|
|
|
// See also the CounterVec example.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) DeleteLabelValues(lvs ...string) bool {
|
2014-05-07 22:08:33 +04:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
h, err := m.hashLabelValues(lvs)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-08-17 15:01:11 +03:00
|
|
|
return m.deleteByHashWithLabelValues(h, lvs)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes the metric where the variable labels are the same as those
|
|
|
|
// passed in as labels. It returns true if a metric was deleted.
|
|
|
|
//
|
|
|
|
// It is not an error if the number and names of the Labels are inconsistent
|
2017-06-28 18:55:59 +03:00
|
|
|
// with those of the VariableLabels in Desc. However, such inconsistent Labels
|
|
|
|
// can never match an actual metric, so the method will always return false in
|
|
|
|
// that case.
|
2014-05-07 22:08:33 +04:00
|
|
|
//
|
|
|
|
// This method is used for the same purpose as DeleteLabelValues(...string). See
|
|
|
|
// there for pros and cons of the two methods.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) Delete(labels Labels) bool {
|
2014-05-07 22:08:33 +04:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
h, err := m.hashLabels(labels)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
return m.deleteByHashWithLabels(h, labels)
|
2016-08-12 03:06:03 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
// deleteByHashWithLabelValues removes the metric from the hash bucket h. If
|
|
|
|
// there are multiple matches in the bucket, use lvs to select a metric and
|
|
|
|
// remove only that metric.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) deleteByHashWithLabelValues(h uint64, lvs []string) bool {
|
2016-08-12 03:06:03 +03:00
|
|
|
metrics, ok := m.children[h]
|
|
|
|
if !ok {
|
2014-05-07 22:08:33 +04:00
|
|
|
return false
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
i := m.findMetricWithLabelValues(metrics, lvs)
|
|
|
|
if i >= len(metrics) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metrics) > 1 {
|
|
|
|
m.children[h] = append(metrics[:i], metrics[i+1:]...)
|
|
|
|
} else {
|
|
|
|
delete(m.children, h)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteByHashWithLabels removes the metric from the hash bucket h. If there
|
|
|
|
// are multiple matches in the bucket, use lvs to select a metric and remove
|
|
|
|
// only that metric.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) deleteByHashWithLabels(h uint64, labels Labels) bool {
|
2016-08-17 15:01:11 +03:00
|
|
|
metrics, ok := m.children[h]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
i := m.findMetricWithLabels(metrics, labels)
|
2016-08-12 03:06:03 +03:00
|
|
|
if i >= len(metrics) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(metrics) > 1 {
|
|
|
|
m.children[h] = append(metrics[:i], metrics[i+1:]...)
|
|
|
|
} else {
|
|
|
|
delete(m.children, h)
|
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset deletes all metrics in this vector.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) Reset() {
|
2014-05-07 22:08:33 +04:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
for h := range m.children {
|
|
|
|
delete(m.children, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) hashLabelValues(vals []string) (uint64, error) {
|
2014-05-07 22:08:33 +04:00
|
|
|
if len(vals) != len(m.desc.variableLabels) {
|
|
|
|
return 0, errInconsistentCardinality
|
|
|
|
}
|
2015-11-09 16:52:51 +03:00
|
|
|
h := hashNew()
|
2014-05-07 22:08:33 +04:00
|
|
|
for _, val := range vals {
|
2016-08-12 03:06:03 +03:00
|
|
|
h = m.hashAdd(h, val)
|
2016-08-13 02:20:05 +03:00
|
|
|
h = m.hashAddByte(h, model.SeparatorByte)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
2015-11-09 16:52:51 +03:00
|
|
|
return h, nil
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) hashLabels(labels Labels) (uint64, error) {
|
2014-05-07 22:08:33 +04:00
|
|
|
if len(labels) != len(m.desc.variableLabels) {
|
|
|
|
return 0, errInconsistentCardinality
|
|
|
|
}
|
2015-11-09 16:52:51 +03:00
|
|
|
h := hashNew()
|
2014-05-07 22:08:33 +04:00
|
|
|
for _, label := range m.desc.variableLabels {
|
|
|
|
val, ok := labels[label]
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("label name %q missing in label map", label)
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
h = m.hashAdd(h, val)
|
2016-08-13 02:20:05 +03:00
|
|
|
h = m.hashAddByte(h, model.SeparatorByte)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
2015-11-09 16:52:51 +03:00
|
|
|
return h, nil
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
// getOrCreateMetricWithLabelValues retrieves the metric by hash and label value
|
|
|
|
// or creates it and returns the new one.
|
2016-08-13 02:20:05 +03:00
|
|
|
//
|
2016-08-17 15:01:11 +03:00
|
|
|
// This function holds the mutex.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) getOrCreateMetricWithLabelValues(hash uint64, lvs []string) Metric {
|
2016-08-17 15:01:11 +03:00
|
|
|
m.mtx.RLock()
|
2017-06-28 18:55:59 +03:00
|
|
|
metric, ok := m.getMetricWithHashAndLabelValues(hash, lvs)
|
2016-08-17 15:01:11 +03:00
|
|
|
m.mtx.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return metric
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2017-06-28 18:55:59 +03:00
|
|
|
metric, ok = m.getMetricWithHashAndLabelValues(hash, lvs)
|
2016-08-17 15:01:11 +03:00
|
|
|
if !ok {
|
|
|
|
// Copy to avoid allocation in case wo don't go down this code path.
|
|
|
|
copiedLVs := make([]string, len(lvs))
|
|
|
|
copy(copiedLVs, lvs)
|
|
|
|
metric = m.newMetric(copiedLVs...)
|
|
|
|
m.children[hash] = append(m.children[hash], metricWithLabelValues{values: copiedLVs, metric: metric})
|
|
|
|
}
|
|
|
|
return metric
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOrCreateMetricWithLabelValues retrieves the metric by hash and label value
|
|
|
|
// or creates it and returns the new one.
|
2016-08-13 02:20:05 +03:00
|
|
|
//
|
|
|
|
// This function holds the mutex.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) getOrCreateMetricWithLabels(hash uint64, labels Labels) Metric {
|
2016-08-13 02:20:05 +03:00
|
|
|
m.mtx.RLock()
|
2017-06-28 18:55:59 +03:00
|
|
|
metric, ok := m.getMetricWithHashAndLabels(hash, labels)
|
2016-08-13 02:20:05 +03:00
|
|
|
m.mtx.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return metric
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
2017-06-28 18:55:59 +03:00
|
|
|
metric, ok = m.getMetricWithHashAndLabels(hash, labels)
|
2014-05-07 22:08:33 +04:00
|
|
|
if !ok {
|
2016-08-17 15:01:11 +03:00
|
|
|
lvs := m.extractLabelValues(labels)
|
2016-08-13 02:20:05 +03:00
|
|
|
metric = m.newMetric(lvs...)
|
|
|
|
m.children[hash] = append(m.children[hash], metricWithLabelValues{values: lvs, metric: metric})
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
return metric
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
// getMetricWithHashAndLabelValues gets a metric while handling possible
|
|
|
|
// collisions in the hash space. Must be called while holding the read mutex.
|
|
|
|
func (m *metricVec) getMetricWithHashAndLabelValues(h uint64, lvs []string) (Metric, bool) {
|
2016-08-12 03:06:03 +03:00
|
|
|
metrics, ok := m.children[h]
|
|
|
|
if ok {
|
2016-08-17 15:01:11 +03:00
|
|
|
if i := m.findMetricWithLabelValues(metrics, lvs); i < len(metrics) {
|
|
|
|
return metrics[i].metric, true
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
// getMetricWithHashAndLabels gets a metric while handling possible collisions in
|
2016-08-17 15:01:11 +03:00
|
|
|
// the hash space. Must be called while holding read mutex.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) getMetricWithHashAndLabels(h uint64, labels Labels) (Metric, bool) {
|
2016-08-17 15:01:11 +03:00
|
|
|
metrics, ok := m.children[h]
|
|
|
|
if ok {
|
|
|
|
if i := m.findMetricWithLabels(metrics, labels); i < len(metrics) {
|
|
|
|
return metrics[i].metric, true
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
// findMetricWithLabelValues returns the index of the matching metric or
|
|
|
|
// len(metrics) if not found.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) findMetricWithLabelValues(metrics []metricWithLabelValues, lvs []string) int {
|
2016-08-12 03:06:03 +03:00
|
|
|
for i, metric := range metrics {
|
2016-08-17 15:01:11 +03:00
|
|
|
if m.matchLabelValues(metric.values, lvs) {
|
2016-08-13 02:20:05 +03:00
|
|
|
return i
|
2016-08-12 03:06:03 +03:00
|
|
|
}
|
2016-08-13 02:20:05 +03:00
|
|
|
}
|
|
|
|
return len(metrics)
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
|
2016-08-17 15:01:11 +03:00
|
|
|
// findMetricWithLabels returns the index of the matching metric or len(metrics)
|
|
|
|
// if not found.
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) findMetricWithLabels(metrics []metricWithLabelValues, labels Labels) int {
|
2016-08-17 15:01:11 +03:00
|
|
|
for i, metric := range metrics {
|
|
|
|
if m.matchLabels(metric.values, labels) {
|
|
|
|
return i
|
2016-08-13 02:20:05 +03:00
|
|
|
}
|
2016-08-17 15:01:11 +03:00
|
|
|
}
|
|
|
|
return len(metrics)
|
|
|
|
}
|
2016-08-13 02:20:05 +03:00
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) matchLabelValues(values []string, lvs []string) bool {
|
2016-08-17 15:01:11 +03:00
|
|
|
if len(values) != len(lvs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range values {
|
|
|
|
if v != lvs[i] {
|
2016-08-13 02:20:05 +03:00
|
|
|
return false
|
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
}
|
2016-08-17 15:01:11 +03:00
|
|
|
return true
|
2016-08-13 02:20:05 +03:00
|
|
|
}
|
2016-08-12 03:06:03 +03:00
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) matchLabels(values []string, labels Labels) bool {
|
2016-08-17 15:01:11 +03:00
|
|
|
if len(labels) != len(values) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, k := range m.desc.variableLabels {
|
|
|
|
if values[i] != labels[k] {
|
|
|
|
return false
|
2016-08-13 02:20:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 15:01:11 +03:00
|
|
|
return true
|
|
|
|
}
|
2016-08-13 02:20:05 +03:00
|
|
|
|
2017-06-28 18:55:59 +03:00
|
|
|
func (m *metricVec) extractLabelValues(labels Labels) []string {
|
2016-08-17 15:01:11 +03:00
|
|
|
labelValues := make([]string, len(labels))
|
|
|
|
for i, k := range m.desc.variableLabels {
|
|
|
|
labelValues[i] = labels[k]
|
|
|
|
}
|
2016-08-13 02:20:05 +03:00
|
|
|
return labelValues
|
2016-08-12 03:06:03 +03:00
|
|
|
}
|