From e9d46b939545a51320afa60261021cfacec1904b Mon Sep 17 00:00:00 2001 From: Navid Yaghoobi Date: Wed, 28 Feb 2024 22:48:16 +1100 Subject: [PATCH] fix exporter panic when using cli store_labels option Signed-off-by: Navid Yaghoobi --- collector/container.go | 11 +++++++++-- collector/image.go | 8 ++++++-- collector/pod.go | 8 ++++++-- collector/utils.go | 10 ++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/collector/container.go b/collector/container.go index 44c1f445..213d2867 100644 --- a/collector/container.go +++ b/collector/container.go @@ -191,7 +191,7 @@ func (c *containerCollector) getContainerInfoDesc(rep pdcs.Container) (*promethe containerLabels := []string{"id", "name", "image", "ports", "pod_id", "pod_name"} containerLabelsValue := []string{rep.ID, rep.Name, rep.Image, rep.Ports, rep.PodID, rep.PodName} - extraLabels, extraValues := c.getExtraLabelsAndValues(rep) + extraLabels, extraValues := c.getExtraLabelsAndValues(containerLabels, rep) containerLabels = append(containerLabels, extraLabels...) containerLabelsValue = append(containerLabelsValue, extraValues...) @@ -205,11 +205,18 @@ func (c *containerCollector) getContainerInfoDesc(rep pdcs.Container) (*promethe return infoDesc, containerLabelsValue } -func (c *containerCollector) getExtraLabelsAndValues(rep pdcs.Container) ([]string, []string) { +func (c *containerCollector) getExtraLabelsAndValues( + collectorLabels []string, + rep pdcs.Container, +) ([]string, []string) { extraLabels := make([]string, 0) extraValues := make([]string, 0) for label, value := range rep.Labels { + if slicesContains(collectorLabels, label) { + continue + } + validLabel := sanitizeLabelName(label) if storeLabels { extraLabels = append(extraLabels, validLabel) diff --git a/collector/image.go b/collector/image.go index e9d6fe38..7f7debec 100644 --- a/collector/image.go +++ b/collector/image.go @@ -64,7 +64,7 @@ func (c *imageCollector) getImageInfoDesc(rep pdcs.Image) (*prometheus.Desc, []s imageLabels := []string{"id", "parent_id", "repository", "tag", "digest"} imageLabelsValue := []string{rep.ID, rep.ParentID, rep.Repository, rep.Tag, rep.Digest} - extraLabels, extraValues := c.getExtraLabelsAndValues(rep) + extraLabels, extraValues := c.getExtraLabelsAndValues(imageLabels, rep) imageLabels = append(imageLabels, extraLabels...) imageLabelsValue = append(imageLabelsValue, extraValues...) @@ -78,11 +78,15 @@ func (c *imageCollector) getImageInfoDesc(rep pdcs.Image) (*prometheus.Desc, []s return infoDesc, imageLabelsValue } -func (c *imageCollector) getExtraLabelsAndValues(rep pdcs.Image) ([]string, []string) { +func (c *imageCollector) getExtraLabelsAndValues(collectorLabels []string, rep pdcs.Image) ([]string, []string) { extraLabels := make([]string, 0) extraValues := make([]string, 0) for label, value := range rep.Labels { + if slicesContains(collectorLabels, label) { + continue + } + validLabel := sanitizeLabelName(label) if storeLabels { extraLabels = append(extraLabels, validLabel) diff --git a/collector/pod.go b/collector/pod.go index 65185047..81a885b0 100644 --- a/collector/pod.go +++ b/collector/pod.go @@ -73,7 +73,7 @@ func (c *podCollector) getPodInfoDesc(rep pdcs.Pod) (*prometheus.Desc, []string) podLabels := []string{"id", "name", "infra_id"} podLabelsValue := []string{rep.ID, rep.Name, rep.InfraID} - extraLabels, extraValues := c.getExtraLabelsAndValues(rep) + extraLabels, extraValues := c.getExtraLabelsAndValues(podLabels, rep) podLabels = append(podLabels, extraLabels...) podLabelsValue = append(podLabelsValue, extraValues...) @@ -87,11 +87,15 @@ func (c *podCollector) getPodInfoDesc(rep pdcs.Pod) (*prometheus.Desc, []string) return infoDesc, podLabelsValue } -func (c *podCollector) getExtraLabelsAndValues(rep pdcs.Pod) ([]string, []string) { +func (c *podCollector) getExtraLabelsAndValues(collectorLabels []string, rep pdcs.Pod) ([]string, []string) { extraLabels := make([]string, 0) extraValues := make([]string, 0) for label, value := range rep.Labels { + if slicesContains(collectorLabels, label) { + continue + } + validLabel := sanitizeLabelName(label) if storeLabels { extraLabels = append(extraLabels, validLabel) diff --git a/collector/utils.go b/collector/utils.go index b736c6b0..13283b7f 100644 --- a/collector/utils.go +++ b/collector/utils.go @@ -34,3 +34,13 @@ func whitelistContains(text string) bool { return false } + +func slicesContains(list []string, value string) bool { + for _, item := range list { + if item == strings.ToLower(value) { + return true + } + } + + return false +}