app/vminsert: prevent from adding and/or selecting labels with empty values

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/600
This commit is contained in:
Aliaksandr Valialkin 2020-07-02 23:13:13 +03:00
parent 6ebac3ab63
commit 8bb3622e9d
3 changed files with 38 additions and 13 deletions

View file

@ -11,7 +11,6 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/influx"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
@ -95,9 +94,9 @@ func insertRows(at *auth.Token, db string, rows []parser.Row, mayOverrideAccount
ctx.metricGroupBuf = append(ctx.metricGroupBuf, *measurementFieldSeparator...)
}
metricGroupPrefixLen := len(ctx.metricGroupBuf)
var labels []prompb.Label
if !hasRelabeling {
labels = ic.Labels
labels := ic.Labels
if hasRelabeling {
labels = nil
}
ic.MetricNameBuf = storage.MarshalMetricNameRaw(ic.MetricNameBuf[:0], atCopy.AccountID, atCopy.ProjectID, labels)
metricNameBufLen := len(ic.MetricNameBuf)
@ -110,15 +109,14 @@ func insertRows(at *auth.Token, db string, rows []parser.Row, mayOverrideAccount
metricGroup := bytesutil.ToUnsafeString(ctx.metricGroupBuf)
ic.Labels = ic.Labels[:labelsLen]
ic.AddLabel("", metricGroup)
if hasRelabeling {
ic.ApplyRelabeling()
labels = ic.Labels
if len(labels) == 0 {
// Skip metric without labels.
continue
}
} else {
labels = ic.Labels[labelsLen : labelsLen+1]
ic.ApplyRelabeling() // this must be called even if !hasRelabeling in order to remove labels with empty values
if len(ic.Labels) == 0 {
// Skip metric without labels.
continue
}
labels = ic.Labels
if !hasRelabeling {
labels = labels[labelsLen : labelsLen+1]
}
ic.MetricNameBuf = ic.MetricNameBuf[:metricNameBufLen]
for i := range labels {

View file

@ -81,8 +81,29 @@ func (ctx *Ctx) Reset() {
//
// The returned labels are valid until the next call to ApplyRelabeling.
func (ctx *Ctx) ApplyRelabeling(labels []prompb.Label) []prompb.Label {
// Remove labels with empty values, since such labels have no sense.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/600 .
hasEmptyValues := false
for _, label := range labels {
if len(label.Value) == 0 {
hasEmptyValues = true
break
}
}
if hasEmptyValues {
dst := labels[:0]
for _, label := range labels {
if len(label.Value) == 0 {
continue
}
dst = append(dst, label)
}
labels = dst
}
prcs := prcsGlobal.Load().(*[]promrelabel.ParsedRelabelConfig)
if len(*prcs) == 0 {
// There are no relabeling rules.
return labels
}
// Convert src to prompbmarshal.Label format suitable for relabeling.

View file

@ -761,6 +761,7 @@ func (db *indexDB) SearchTagKeys(accountID, projectID uint32, maxTagKeys int) ([
keys := make([]string, 0, len(tks))
for key := range tks {
// Do not skip empty keys, since they are converted to __name__
keys = append(keys, key)
}
@ -828,6 +829,11 @@ func (db *indexDB) SearchTagValues(accountID, projectID uint32, tagKey []byte, m
tagValues := make([]string, 0, len(tvs))
for tv := range tvs {
if len(tv) == 0 {
// Skip empty values, since they have no any meaning.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/600
continue
}
tagValues = append(tagValues, tv)
}