From 92bc1afcee907d58192b780c943c4af408739590 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 7 Nov 2020 13:02:12 +0200 Subject: [PATCH] lib/promscrape/discovery/kubernetes: reduce memory usage for labels when discovering big number of scrape targets by using string concatenation instead of fmt.Sprintf Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/825 --- lib/promscrape/discovery/kubernetes/common_types.go | 9 ++++----- lib/promscrape/discovery/kubernetes/node.go | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/promscrape/discovery/kubernetes/common_types.go b/lib/promscrape/discovery/kubernetes/common_types.go index 039fce6a94..12de389628 100644 --- a/lib/promscrape/discovery/kubernetes/common_types.go +++ b/lib/promscrape/discovery/kubernetes/common_types.go @@ -1,7 +1,6 @@ package kubernetes import ( - "fmt" "net/url" "strings" @@ -23,13 +22,13 @@ type ObjectMeta struct { func (om *ObjectMeta) registerLabelsAndAnnotations(prefix string, m map[string]string) { for _, lb := range om.Labels { ln := discoveryutils.SanitizeLabelName(lb.Name) - m[fmt.Sprintf("%s_label_%s", prefix, ln)] = lb.Value - m[fmt.Sprintf("%s_labelpresent_%s", prefix, ln)] = "true" + m[prefix + "_label_" + ln] = lb.Value + m[prefix + "_labelpresent_" + ln] = "true" } for _, a := range om.Annotations { an := discoveryutils.SanitizeLabelName(a.Name) - m[fmt.Sprintf("%s_annotation_%s", prefix, an)] = a.Value - m[fmt.Sprintf("%s_annotationpresent_%s", prefix, an)] = "true" + m[prefix + "_annotation_" + an] = a.Value + m[prefix + "_annotationpresent_" + an] = "true" } } diff --git a/lib/promscrape/discovery/kubernetes/node.go b/lib/promscrape/discovery/kubernetes/node.go index 411b259786..02ba6b2564 100644 --- a/lib/promscrape/discovery/kubernetes/node.go +++ b/lib/promscrape/discovery/kubernetes/node.go @@ -95,7 +95,7 @@ func (n *Node) appendTargetLabels(ms []map[string]string) []map[string]string { } addrTypesUsed[a.Type] = true ln := discoveryutils.SanitizeLabelName(a.Type) - m[fmt.Sprintf("__meta_kubernetes_node_address_%s", ln)] = a.Address + m["__meta_kubernetes_node_address_" + ln] = a.Address } ms = append(ms, m) return ms