Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/dockerstat] Only one label/envVar is added even if multiple ones are specified #21113

Closed
paologallinaharbur opened this issue Apr 24, 2023 · 2 comments · Fixed by #21160
Assignees
Labels
bug Something isn't working

Comments

@paologallinaharbur
Copy link
Member

paologallinaharbur commented Apr 24, 2023

Component(s)

receiver/dockerstats

What happened?

When running the dockerstat receiver I noticed that setting container_labels_to_metric_labels with 2 or more labels results in just either one of the labels included as a resource attribute.

receivers:
  docker_stats:
    container_labels_to_metric_labels:
      "com.docker.compose.config-hash": "com.docker.compose.config-hash"
      "com.docker.compose.container-number": "new.name"

Steps to Reproduce

Run the exporters with 2 or more entries of container_labels_to_metric_labels

Expected Result

Both labels included

Actual Result

Only one of the two was present at a time.

Screenshot 2023-04-24 at 15 49 21

Root cause

Basically here we have a loop and we are using directly the labels variable without saving a local copy when creating resourceMetricsOptions.
Therefore when running the loop multiple resourceMetricsOptions are appended, but all shares the same value for label, i.e. the latest one applied.

The following code solves the issue:

	for k, label := range r.config.ContainerLabelsToMetricLabels {

		l := label  <- Adding this shadows the loop variable and fixes the code

		if v := container.Config.Labels[k]; v != "" {
			resourceMetricsOptions = append(resourceMetricsOptions, func(ras metadata.ResourceAttributesSettings, rm pmetric.ResourceMetrics) {
				rm.Resource().Attributes().PutStr(l, v)
			})
		}
	}

I checked into the code and envVars are affected as well when setting env_vars_to_metric_labels.

Notice that there are currently discussions regarding modifying the configuration experience and the implementation to include labels. See #13848

Collector version

691044b

@paologallinaharbur paologallinaharbur added bug Something isn't working needs triage New item requiring triage labels Apr 24, 2023
@github-actions
Copy link
Contributor

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@paologallinaharbur paologallinaharbur changed the title [receiver/dockerstat] Only one label/envVar is added even if when multiple are selected [receiver/dockerstat] Only one label/envVar is added even if multiple ones are specified Apr 24, 2023
@paologallinaharbur
Copy link
Member Author

paologallinaharbur commented Apr 24, 2023

Ideally, we would need a fix and a test covering such use-case. I can take ownership of fixing it if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants