Skip to content

Commit

Permalink
Merge pull request #190 from navidys/duplicated_labels
Browse files Browse the repository at this point in the history
fix exporter panic when using cli store_labels option
  • Loading branch information
navidys authored Feb 28, 2024
2 parents ae8df61 + e9d46b9 commit 89f2a1b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
11 changes: 9 additions & 2 deletions collector/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions collector/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions collector/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions collector/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 89f2a1b

Please sign in to comment.