diff --git a/app/vmagent/remotewrite/relabel.go b/app/vmagent/remotewrite/relabel.go index b79021c64..93b4caee7 100644 --- a/app/vmagent/remotewrite/relabel.go +++ b/app/vmagent/remotewrite/relabel.go @@ -98,24 +98,15 @@ func (rctx *relabelCtx) applyRelabeling(tss []prompbmarshal.TimeSeries, pcs *pro ts := &tss[i] labelsLen := len(labels) labels = append(labels, ts.Labels...) - if *usePromCompatibleNaming { - // Replace unsupported Prometheus chars in label names and metric names with underscores. - tmpLabels := labels[labelsLen:] - for j := range tmpLabels { - label := &tmpLabels[j] - if label.Name == "__name__" { - label.Value = promrelabel.SanitizeMetricName(label.Value) - } else { - label.Name = promrelabel.SanitizeLabelName(label.Name) - } - } - } labels = pcs.Apply(labels, labelsLen) labels = promrelabel.FinalizeLabels(labels[:labelsLen], labels[labelsLen:]) if len(labels) == labelsLen { // Drop the current time series, since relabeling removed all the labels. continue } + if *usePromCompatibleNaming { + fixPromCompatibleNaming(labels[labelsLen:]) + } tssDst = append(tssDst, prompbmarshal.TimeSeries{ Labels: labels[labelsLen:], Samples: ts.Samples, @@ -125,11 +116,10 @@ func (rctx *relabelCtx) applyRelabeling(tss []prompbmarshal.TimeSeries, pcs *pro return tssDst } -func (rctx *relabelCtx) appendExtraLabels(tss []prompbmarshal.TimeSeries, extraLabels []prompbmarshal.Label) []prompbmarshal.TimeSeries { +func (rctx *relabelCtx) appendExtraLabels(tss []prompbmarshal.TimeSeries, extraLabels []prompbmarshal.Label) { if len(extraLabels) == 0 { - return tss + return } - tssDst := tss[:0] labels := rctx.labels[:0] for i := range tss { ts := &tss[i] @@ -147,14 +137,9 @@ func (rctx *relabelCtx) appendExtraLabels(tss []prompbmarshal.TimeSeries, extraL labels = append(labels, extraLabel) } } - labels = promrelabel.FinalizeLabels(labels[:labelsLen], labels[labelsLen:]) - tssDst = append(tssDst, prompbmarshal.TimeSeries{ - Labels: labels[labelsLen:], - Samples: ts.Samples, - }) + ts.Labels = labels[labelsLen:] } rctx.labels = labels - return tssDst } type relabelCtx struct { @@ -181,3 +166,15 @@ func putRelabelCtx(rctx *relabelCtx) { rctx.labels = rctx.labels[:0] relabelCtxPool.Put(rctx) } + +func fixPromCompatibleNaming(labels []prompbmarshal.Label) { + // Replace unsupported Prometheus chars in label names and metric names with underscores. + for i := range labels { + label := &labels[i] + if label.Name == "__name__" { + label.Value = promrelabel.SanitizeMetricName(label.Value) + } else { + label.Name = promrelabel.SanitizeLabelName(label.Name) + } + } +} diff --git a/app/vmagent/remotewrite/relabel_test.go b/app/vmagent/remotewrite/relabel_test.go index 441283f23..c84a31610 100644 --- a/app/vmagent/remotewrite/relabel_test.go +++ b/app/vmagent/remotewrite/relabel_test.go @@ -42,9 +42,9 @@ func TestAppendExtraLabels(t *testing.T) { f := func(extraLabels []prompbmarshal.Label, sTss, sExpTss string) { rctx := &relabelCtx{} tss, expTss := parseSeries(sTss), parseSeries(sExpTss) - gotTss := rctx.appendExtraLabels(tss, extraLabels) - if !reflect.DeepEqual(gotTss, expTss) { - t.Fatalf("expected to have: \n%v;\ngot: \n%v", expTss, gotTss) + rctx.appendExtraLabels(tss, extraLabels) + if !reflect.DeepEqual(tss, expTss) { + t.Fatalf("expected to have: \n%v;\ngot: \n%v", expTss, tss) } } diff --git a/app/vmagent/remotewrite/remotewrite.go b/app/vmagent/remotewrite/remotewrite.go index b4f16ad3e..62dd1a908 100644 --- a/app/vmagent/remotewrite/remotewrite.go +++ b/app/vmagent/remotewrite/remotewrite.go @@ -355,7 +355,7 @@ func Push(at *auth.Token, wr *prompbmarshal.WriteRequest) { var rctx *relabelCtx rcs := allRelabelConfigs.Load() pcsGlobal := rcs.global - if pcsGlobal.Len() > 0 || len(labelsGlobal) > 0 { + if pcsGlobal.Len() > 0 { rctx = getRelabelCtx() } tss := wr.Timeseries @@ -722,7 +722,7 @@ func (rwctx *remoteWriteCtx) pushInternal(tss []prompbmarshal.TimeSeries) { if len(labelsGlobal) > 0 { rctx := getRelabelCtx() defer putRelabelCtx(rctx) - tss = rctx.appendExtraLabels(tss, labelsGlobal) + rctx.appendExtraLabels(tss, labelsGlobal) } pss := rwctx.pss