app/{vmagent,vminsert}: properly preserve db tag from query string passed to Influx line protocol query

Previously `db` tag from the query string wasn't added to metrics after encountering `db` tag in the Influx line

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/653
This commit is contained in:
Aliaksandr Valialkin 2020-07-28 21:23:01 +03:00
parent 62ed38c6f0
commit 0f63da3698
2 changed files with 8 additions and 4 deletions

View file

@ -63,17 +63,18 @@ func insertRows(db string, rows []parser.Row) error {
for i := range rows { for i := range rows {
r := &rows[i] r := &rows[i]
commonLabels = commonLabels[:0] commonLabels = commonLabels[:0]
hasDBKey := false
for j := range r.Tags { for j := range r.Tags {
tag := &r.Tags[j] tag := &r.Tags[j]
if tag.Key == "db" { if tag.Key == "db" {
db = "" hasDBKey = true
} }
commonLabels = append(commonLabels, prompbmarshal.Label{ commonLabels = append(commonLabels, prompbmarshal.Label{
Name: tag.Key, Name: tag.Key,
Value: tag.Value, Value: tag.Value,
}) })
} }
if len(db) > 0 { if len(db) > 0 && !hasDBKey {
commonLabels = append(commonLabels, prompbmarshal.Label{ commonLabels = append(commonLabels, prompbmarshal.Label{
Name: "db", Name: "db",
Value: db, Value: db,

View file

@ -66,14 +66,17 @@ func insertRows(db string, rows []parser.Row) error {
for i := range rows { for i := range rows {
r := &rows[i] r := &rows[i]
ic.Labels = ic.Labels[:0] ic.Labels = ic.Labels[:0]
hasDBKey := false
for j := range r.Tags { for j := range r.Tags {
tag := &r.Tags[j] tag := &r.Tags[j]
if tag.Key == "db" { if tag.Key == "db" {
db = "" hasDBKey = true
} }
ic.AddLabel(tag.Key, tag.Value) ic.AddLabel(tag.Key, tag.Value)
} }
ic.AddLabel("db", db) if !hasDBKey {
ic.AddLabel("db", db)
}
ctx.metricGroupBuf = ctx.metricGroupBuf[:0] ctx.metricGroupBuf = ctx.metricGroupBuf[:0]
if !*skipMeasurement { if !*skipMeasurement {
ctx.metricGroupBuf = append(ctx.metricGroupBuf, r.Measurement...) ctx.metricGroupBuf = append(ctx.metricGroupBuf, r.Measurement...)