Skip to content

Commit

Permalink
lib/promscrape: clean references to label name and label value string…
Browse files Browse the repository at this point in the history
…s after applying per-target relabeling

This should reduce memory usage when per-target relabeling creates big number of temporary labels
with long names and/or values.

See #825
  • Loading branch information
valyala committed Nov 7, 2020
1 parent 9e83335 commit 83df20b
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@
* FEATURE: vmagent: reduce memory usage when `kubernetes_sd_config` discovers big number of scrape targets (e.g. hundreds of thouthands) and the majority of these targets (99%)
are dropped during relabeling. Previously labels for all the dropped targets were displayed at `/api/v1/targets` page. Now only up to `-promscrape.maxDroppedTargets` such
targets are displayed. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/878 for details.
* FEATURE: vmagent: reduce memory usage when scraping big number of targets with big number of temporary labels starting with `__`.
See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/825
* FEATURE: vmagent: add `/ready` HTTP endpoint, which returns 200 OK status code when all the service discovery has been initialized.
This may be useful during rolling upgrades. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/875

Expand Down
8 changes: 2 additions & 6 deletions app/vmagent/common/push_ctx.go
Expand Up @@ -5,6 +5,7 @@ import (
"sync"

"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
)

// PushCtx is a context used for populating WriteRequest.
Expand All @@ -28,12 +29,7 @@ func (ctx *PushCtx) Reset() {
}
ctx.WriteRequest.Timeseries = ctx.WriteRequest.Timeseries[:0]

labels := ctx.Labels
for i := range labels {
label := &labels[i]
label.Name = ""
label.Value = ""
}
promrelabel.CleanLabels(ctx.Labels)
ctx.Labels = ctx.Labels[:0]

ctx.Samples = ctx.Samples[:0]
Expand Down
9 changes: 3 additions & 6 deletions app/vmagent/influx/request_handler.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/influx"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
"github.com/VictoriaMetrics/metrics"
Expand Down Expand Up @@ -135,12 +136,8 @@ type pushCtx struct {
func (ctx *pushCtx) reset() {
ctx.ctx.Reset()

commonLabels := ctx.commonLabels
for i := range commonLabels {
label := &commonLabels[i]
label.Name = ""
label.Value = ""
}
promrelabel.CleanLabels(ctx.commonLabels)
ctx.commonLabels = ctx.commonLabels[:0]

ctx.metricGroupBuf = ctx.metricGroupBuf[:0]
ctx.buf = ctx.buf[:0]
Expand Down
7 changes: 2 additions & 5 deletions app/vmagent/remotewrite/pendingseries.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/persistentqueue"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
"github.com/VictoriaMetrics/metrics"
"github.com/golang/snappy"
)
Expand Down Expand Up @@ -104,11 +105,7 @@ func (wr *writeRequest) reset() {
}
wr.tss = wr.tss[:0]

for i := range wr.labels {
label := &wr.labels[i]
label.Name = ""
label.Value = ""
}
promrelabel.CleanLabels(wr.labels)
wr.labels = wr.labels[:0]

wr.samples = wr.samples[:0]
Expand Down
7 changes: 1 addition & 6 deletions app/vmagent/remotewrite/relabel.go
Expand Up @@ -117,12 +117,7 @@ type relabelCtx struct {
}

func (rctx *relabelCtx) reset() {
labels := rctx.labels
for i := range labels {
label := &labels[i]
label.Name = ""
label.Value = ""
}
promrelabel.CleanLabels(rctx.labels)
rctx.labels = rctx.labels[:0]
}

Expand Down
7 changes: 1 addition & 6 deletions app/vminsert/relabel/relabel.go
Expand Up @@ -69,12 +69,7 @@ type Ctx struct {

// Reset resets ctx.
func (ctx *Ctx) Reset() {
labels := ctx.tmpLabels
for i := range labels {
label := &labels[i]
label.Name = ""
label.Value = ""
}
promrelabel.CleanLabels(ctx.tmpLabels)
ctx.tmpLabels = ctx.tmpLabels[:0]
}

Expand Down
11 changes: 11 additions & 0 deletions lib/promrelabel/relabel.go
Expand Up @@ -336,3 +336,14 @@ func GetLabelValueByName(labels []prompbmarshal.Label, name string) string {
}
return label.Value
}

// CleanLabels sets label.Name and label.Value to an empty string for all the labels.
//
// This should help GC cleaning up label.Name and label.Value strings.
func CleanLabels(labels []prompbmarshal.Label) {
for i := range labels {
label := &labels[i]
label.Name = ""
label.Value = ""
}
}
5 changes: 5 additions & 0 deletions lib/promscrape/config.go
Expand Up @@ -643,6 +643,11 @@ func appendScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, target string, ex
promrelabel.SortLabels(originalLabels)
labels = promrelabel.ApplyRelabelConfigs(labels, 0, swc.relabelConfigs, false)
labels = promrelabel.RemoveMetaLabels(labels[:0], labels)
// Remove references to already deleted labels, so GC could clean strings for label name and label value.
// This should reduce memory usage when relabeling creates big number of temporary labels with long names and/or values.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/825 for details.
promrelabel.CleanLabels(labels[len(labels):cap(labels)])

if len(labels) == 0 {
// Drop target without labels.
droppedTargetsMap.Register(originalLabels)
Expand Down

0 comments on commit 83df20b

Please sign in to comment.