Bring back zero-alloc label-value access for metric vecs

Also, fix mutex copy-by-value bug.
This commit is contained in:
beorn7 2016-08-17 14:01:11 +02:00
parent 1f823ab271
commit 434a8ed85d
7 changed files with 122 additions and 94 deletions

View File

@ -82,7 +82,7 @@ func (c *counter) Add(v float64) {
// CounterVec embeds MetricVec. See there for a full list of methods with // CounterVec embeds MetricVec. See there for a full list of methods with
// detailed documentation. // detailed documentation.
type CounterVec struct { type CounterVec struct {
MetricVec *MetricVec
} }
// NewCounterVec creates a new CounterVec based on the provided CounterOpts and // NewCounterVec creates a new CounterVec based on the provided CounterOpts and

View File

@ -58,7 +58,7 @@ func NewGauge(opts GaugeOpts) Gauge {
// (e.g. number of operations queued, partitioned by user and operation // (e.g. number of operations queued, partitioned by user and operation
// type). Create instances with NewGaugeVec. // type). Create instances with NewGaugeVec.
type GaugeVec struct { type GaugeVec struct {
MetricVec *MetricVec
} }
// NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and // NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and

View File

@ -287,7 +287,7 @@ func (h *histogram) Write(out *dto.Metric) error {
// (e.g. HTTP request latencies, partitioned by status code and method). Create // (e.g. HTTP request latencies, partitioned by status code and method). Create
// instances with NewHistogramVec. // instances with NewHistogramVec.
type HistogramVec struct { type HistogramVec struct {
MetricVec *MetricVec
} }
// NewHistogramVec creates a new HistogramVec based on the provided HistogramOpts and // NewHistogramVec creates a new HistogramVec based on the provided HistogramOpts and

View File

@ -390,7 +390,7 @@ func (s quantSort) Less(i, j int) bool {
// (e.g. HTTP request latencies, partitioned by status code and method). Create // (e.g. HTTP request latencies, partitioned by status code and method). Create
// instances with NewSummaryVec. // instances with NewSummaryVec.
type SummaryVec struct { type SummaryVec struct {
MetricVec *MetricVec
} }
// NewSummaryVec creates a new SummaryVec based on the provided SummaryOpts and // NewSummaryVec creates a new SummaryVec based on the provided SummaryOpts and

View File

@ -56,7 +56,7 @@ func NewUntyped(opts UntypedOpts) Untyped {
// labels. This is used if you want to count the same thing partitioned by // labels. This is used if you want to count the same thing partitioned by
// various dimensions. Create instances with NewUntypedVec. // various dimensions. Create instances with NewUntypedVec.
type UntypedVec struct { type UntypedVec struct {
MetricVec *MetricVec
} }
// NewUntypedVec creates a new UntypedVec based on the provided UntypedOpts and // NewUntypedVec creates a new UntypedVec based on the provided UntypedOpts and

View File

@ -37,8 +37,8 @@ type MetricVec struct {
// newMetricVec returns an initialized MetricVec. The concrete value is // newMetricVec returns an initialized MetricVec. The concrete value is
// returned for embedding into another struct. // returned for embedding into another struct.
func newMetricVec(desc *Desc, newMetric func(lvs ...string) Metric) MetricVec { func newMetricVec(desc *Desc, newMetric func(lvs ...string) Metric) *MetricVec {
return MetricVec{ return &MetricVec{
children: map[uint64][]metricWithLabelValues{}, children: map[uint64][]metricWithLabelValues{},
desc: desc, desc: desc,
newMetric: newMetric, newMetric: newMetric,
@ -102,7 +102,7 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
return nil, err return nil, err
} }
return m.getOrCreateMetric(h, lvs), nil return m.getOrCreateMetricWithLabelValues(h, lvs), nil
} }
// GetMetricWith returns the Metric for the given Labels map (the label names // GetMetricWith returns the Metric for the given Labels map (the label names
@ -123,7 +123,7 @@ func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) {
return nil, err return nil, err
} }
return m.getOrCreateMetric(h, labels), nil return m.getOrCreateMetricWithLabels(h, labels), nil
} }
// WithLabelValues works as GetMetricWithLabelValues, but panics if an error // WithLabelValues works as GetMetricWithLabelValues, but panics if an error
@ -171,7 +171,7 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool {
if err != nil { if err != nil {
return false return false
} }
return m.deleteByHash(h, lvs) return m.deleteByHashWithLabelValues(h, lvs)
} }
// Delete deletes the metric where the variable labels are the same as those // Delete deletes the metric where the variable labels are the same as those
@ -193,21 +193,40 @@ func (m *MetricVec) Delete(labels Labels) bool {
return false return false
} }
return m.deleteByHash(h, labels) return m.deleteByHashWithLabels(h, labels)
} }
// deleteByHash removes the metric from the hash bucket h. If there are // deleteByHashWithLabelValues removes the metric from the hash bucket h. If
// multiple matches in the bucket, use lvs to select a metric and remove only // there are multiple matches in the bucket, use lvs to select a metric and
// that metric. // remove only that metric.
// func (m *MetricVec) deleteByHashWithLabelValues(h uint64, lvs []string) bool {
// lvs MUST be of type Labels or []string or this method will panic.
func (m *MetricVec) deleteByHash(h uint64, lvs interface{}) bool {
metrics, ok := m.children[h] metrics, ok := m.children[h]
if !ok { if !ok {
return false return false
} }
i := m.findMetric(metrics, lvs) 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.
func (m *MetricVec) deleteByHashWithLabels(h uint64, labels Labels) bool {
metrics, ok := m.children[h]
if !ok {
return false
}
i := m.findMetricWithLabels(metrics, labels)
if i >= len(metrics) { if i >= len(metrics) {
return false return false
} }
@ -258,15 +277,13 @@ func (m *MetricVec) hashLabels(labels Labels) (uint64, error) {
return h, nil return h, nil
} }
// getOrCreateMetric retrieves the metric by hash and label value or creates it // getOrCreateMetricWithLabelValues retrieves the metric by hash and label value
// and returns the new one. // or creates it and returns the new one.
//
// lvs MUST be of type Labels or []string or this method will panic.
// //
// This function holds the mutex. // This function holds the mutex.
func (m *MetricVec) getOrCreateMetric(hash uint64, lvs interface{}) Metric { func (m *MetricVec) getOrCreateMetricWithLabelValues(hash uint64, lvs []string) Metric {
m.mtx.RLock() m.mtx.RLock()
metric, ok := m.getMetric(hash, lvs) metric, ok := m.getMetricWithLabelValues(hash, lvs)
m.mtx.RUnlock() m.mtx.RUnlock()
if ok { if ok {
return metric return metric
@ -274,103 +291,114 @@ func (m *MetricVec) getOrCreateMetric(hash uint64, lvs interface{}) Metric {
m.mtx.Lock() m.mtx.Lock()
defer m.mtx.Unlock() defer m.mtx.Unlock()
metric, ok = m.getMetric(hash, lvs) metric, ok = m.getMetricWithLabelValues(hash, lvs)
if !ok { if !ok {
lvs := m.copyLabelValues(lvs) // 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.
//
// This function holds the mutex.
func (m *MetricVec) getOrCreateMetricWithLabels(hash uint64, labels Labels) Metric {
m.mtx.RLock()
metric, ok := m.getMetricWithLabels(hash, labels)
m.mtx.RUnlock()
if ok {
return metric
}
m.mtx.Lock()
defer m.mtx.Unlock()
metric, ok = m.getMetricWithLabels(hash, labels)
if !ok {
lvs := m.extractLabelValues(labels)
metric = m.newMetric(lvs...) metric = m.newMetric(lvs...)
m.children[hash] = append(m.children[hash], metricWithLabelValues{values: lvs, metric: metric}) m.children[hash] = append(m.children[hash], metricWithLabelValues{values: lvs, metric: metric})
} }
return metric return metric
} }
// getMetric while handling possible collisions in the hash space. Must be // getMetricWithLabelValues gets a metric while handling possible collisions in
// called while holding read mutex. // the hash space. Must be called while holding read mutex.
// func (m *MetricVec) getMetricWithLabelValues(h uint64, lvs []string) (Metric, bool) {
// lvs must be of type Labels or []string.
func (m *MetricVec) getMetric(h uint64, lvs interface{}) (Metric, bool) {
metrics, ok := m.children[h] metrics, ok := m.children[h]
if ok { if ok {
return m.selectMetric(metrics, lvs) if i := m.findMetricWithLabelValues(metrics, lvs); i < len(metrics) {
}
return nil, false
}
func (m *MetricVec) selectMetric(metrics []metricWithLabelValues, lvs interface{}) (Metric, bool) {
i := m.findMetric(metrics, lvs)
if i < len(metrics) {
return metrics[i].metric, true return metrics[i].metric, true
} }
}
return nil, false return nil, false
} }
// findMetric returns the index of the matching metric or len(metrics) if not // getMetricWithLabels gets a metric while handling possible collisions in
// found. // the hash space. Must be called while holding read mutex.
func (m *MetricVec) findMetric(metrics []metricWithLabelValues, lvs interface{}) int { func (m *MetricVec) getMetricWithLabels(h uint64, labels Labels) (Metric, bool) {
metrics, ok := m.children[h]
if ok {
if i := m.findMetricWithLabels(metrics, labels); i < len(metrics) {
return metrics[i].metric, true
}
}
return nil, false
}
// findMetricWithLabelValues returns the index of the matching metric or
// len(metrics) if not found.
func (m *MetricVec) findMetricWithLabelValues(metrics []metricWithLabelValues, lvs []string) int {
for i, metric := range metrics { for i, metric := range metrics {
if m.matchLabels(metric.values, lvs) { if m.matchLabelValues(metric.values, lvs) {
return i return i
} }
} }
return len(metrics) return len(metrics)
} }
func (m *MetricVec) matchLabels(values []string, lvs interface{}) bool { // findMetricWithLabels returns the index of the matching metric or len(metrics)
switch lvs := lvs.(type) { // if not found.
case []string: func (m *MetricVec) findMetricWithLabels(metrics []metricWithLabelValues, labels Labels) int {
for i, metric := range metrics {
if m.matchLabels(metric.values, labels) {
return i
}
}
return len(metrics)
}
func (m *MetricVec) matchLabelValues(values []string, lvs []string) bool {
if len(values) != len(lvs) { if len(values) != len(lvs) {
return false return false
} }
for i, v := range values { for i, v := range values {
if v != lvs[i] { if v != lvs[i] {
return false return false
} }
} }
return true return true
case Labels:
if len(lvs) != len(values) {
return false
}
for i, k := range m.desc.variableLabels {
if values[i] != lvs[k] {
return false
}
}
return true
default:
// If we reach this condition, there is an unexpected type being used
// as a labels value. Either add branch here for the new type or fix
// the bug causing the type to be passed in.
panic("unsupported type")
}
} }
// copyLabelValues copies the labels values into common string slice format to func (m *MetricVec) matchLabels(values []string, labels Labels) bool {
// use when allocating the metric and to keep track of hash collision if len(labels) != len(values) {
// ambiguity. return false
// }
// lvs must be of type Labels or []string or this method will panic.
func (m *MetricVec) copyLabelValues(lvs interface{}) []string {
var labelValues []string
switch lvs := lvs.(type) {
case []string:
labelValues = make([]string, len(lvs))
copy(labelValues, lvs)
case Labels:
labelValues = make([]string, len(lvs))
for i, k := range m.desc.variableLabels { for i, k := range m.desc.variableLabels {
labelValues[i] = lvs[k] if values[i] != labels[k] {
return false
} }
default:
panic(fmt.Sprintf("unsupported type for lvs: %#v", lvs))
} }
return true
}
func (m *MetricVec) extractLabelValues(labels Labels) []string {
labelValues := make([]string, len(labels))
for i, k := range m.desc.variableLabels {
labelValues[i] = labels[k]
}
return labelValues return labelValues
} }

View File

@ -225,7 +225,7 @@ func TestCounterVecEndToEndWithCollision(t *testing.T) {
t.Errorf("got label value %q, want %q", got, want) t.Errorf("got label value %q, want %q", got, want)
} }
if got, want := m.GetCounter().GetValue(), 1.; got != want { if got, want := m.GetCounter().GetValue(), 1.; got != want {
t.Errorf("got value %d, want %d", got, want) t.Errorf("got value %f, want %f", got, want)
} }
m.Reset() m.Reset()
if err := vec.WithLabelValues("!0IC=VloaY").Write(m); err != nil { if err := vec.WithLabelValues("!0IC=VloaY").Write(m); err != nil {
@ -235,7 +235,7 @@ func TestCounterVecEndToEndWithCollision(t *testing.T) {
t.Errorf("got label value %q, want %q", got, want) t.Errorf("got label value %q, want %q", got, want)
} }
if got, want := m.GetCounter().GetValue(), 2.; got != want { if got, want := m.GetCounter().GetValue(), 2.; got != want {
t.Errorf("got value %d, want %d", got, want) t.Errorf("got value %f, want %f", got, want)
} }
} }