Skip to content

Commit

Permalink
fix(dockerstats) ToMetricLabels behaviour
Browse files Browse the repository at this point in the history
Only one label/envVar was added even if multiple ones were specified
by ContainerLabelsToMetricLabels or EnvVarsToMetricLabels.
Adding a local variable shadowing the loop one solves the issue.

Signed-off-by: paologallinaharbur <paologallina1992@gmail.com>
  • Loading branch information
paologallinaharbur committed May 15, 2023
1 parent d67cbca commit db08c45
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .chloggen/fix_dockerstats_to_metric_label_loop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: dockerstats

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Only one label/envVar was added even if multiple ones were specified by ContainerLabelsToMetricLabels or EnvVarsToMetricLabels

# One or more tracking issues related to the change
issues: [21113]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
2 changes: 2 additions & 0 deletions receiver/dockerstatsreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ func (r *receiver) recordContainerStats(now pcommon.Timestamp, containerStats *d
metadata.WithContainerName(strings.TrimPrefix(container.Name, "/")))

for k, label := range r.config.EnvVarsToMetricLabels {
label := label
if v := container.EnvMap[k]; v != "" {
resourceMetricsOptions = append(resourceMetricsOptions, func(ras metadata.ResourceAttributesConfig, rm pmetric.ResourceMetrics) {
rm.Resource().Attributes().PutStr(label, v)
})
}
}
for k, label := range r.config.ContainerLabelsToMetricLabels {
label := label
if v := container.Config.Labels[k]; v != "" {
resourceMetricsOptions = append(resourceMetricsOptions, func(ras metadata.ResourceAttributesConfig, rm pmetric.ResourceMetrics) {
rm.Resource().Attributes().PutStr(label, v)
Expand Down
10 changes: 8 additions & 2 deletions receiver/dockerstatsreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,14 @@ func TestScrapeV2(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Endpoint = tc.mockDockerEngine.URL
cfg.EnvVarsToMetricLabels = map[string]string{"ENV_VAR": "env-var-metric-label"}
cfg.ContainerLabelsToMetricLabels = map[string]string{"container.label": "container-metric-label"}
cfg.EnvVarsToMetricLabels = map[string]string{
"ENV_VAR": "env-var-metric-label",
"ENV_VAR_2": "env-var-metric-label-2",
}
cfg.ContainerLabelsToMetricLabels = map[string]string{
"container.label": "container-metric-label",
"container.label.2": "container-metric-label-2",
}
cfg.MetricsBuilderConfig.Metrics = allMetricsEnabled

receiver := newReceiver(receivertest.NewNopCreateSettings(), cfg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Entrypoint": null,
"Env": [
"ENV_VAR=env-var",
"ENV_VAR_2=env-var-2",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"ExposedPorts": {
Expand All @@ -20,7 +21,8 @@
"Hostname": "10b703fb312b",
"Image": "ubuntu",
"Labels": {
"container.label": "container-label"
"container.label": "container-label",
"container.label.2": "container-label-2"
},
"OnBuild": null,
"OpenStdin": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"Image": "ubuntu",
"ImageID": "sha256:825d55fb6340083b06e69e02e823a02918f3ffb575ed2a87026d4645a7fd9e1b",
"Labels": {
"container.label": "container-label"
"container.label": "container-label",
"container.label.2": "container-label-2"
},
"Mounts": [],
"Names": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ resourceMetrics:
- key: env-var-metric-label
value:
stringValue: env-var
- key: env-var-metric-label-2
value:
stringValue: env-var-2
- key: container-metric-label
value:
stringValue: container-label
- key: container-metric-label-2
value:
stringValue: container-label-2
schemaUrl: https://opentelemetry.io/schemas/1.6.1
scopeMetrics:
- metrics:
Expand Down

0 comments on commit db08c45

Please sign in to comment.