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

Fix container enricher with correct container ids for pods with multiple containers #34516

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
if split != -1 {
kubernetes.ShouldPut(containerFields, "runtime", cID[:split], m.Logger())

// Add splitted container.id ECS field and update kubernetes.container.id with splitted value
kubernetes.ShouldPut(containerFields, "id", cID[split+3:], m.Logger())
kubernetes.ShouldPut(event, "id", cID[split+3:], m.Logger())

}
}
if containerImage, ok := event["image"]; ok {
Expand Down
10 changes: 6 additions & 4 deletions metricbeat/module/kubernetes/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func NewContainerMetadataEnricher(
if !ok {
base.Logger().Debugf("Error while casting event: %s", ok)
}
meta := metaGen.Generate(pod)
pmeta := metaGen.Generate(pod)

statuses := make(map[string]*kubernetes.PodContainerStatus)
mapStatuses := func(s []kubernetes.PodContainerStatus) {
Expand All @@ -293,6 +293,7 @@ func NewContainerMetadataEnricher(
podStore, _ := nodeStore.AddPodStore(podId)

for _, container := range append(pod.Spec.Containers, pod.Spec.InitContainers...) {
Copy link
Contributor

@gizas gizas Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we have a for here, but just doublehcecking if we can make a test wit 3 containers. In case we miss sth and compare with kube_state.container dataset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reassure you it works because I tested it!

cmeta := mapstr.M{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

metrics := NewContainerMetrics()

if cpu, ok := container.Resources.Limits["cpu"]; ok {
Expand All @@ -314,14 +315,15 @@ func NewContainerMetadataEnricher(
// which is in the form of <container.runtime>://<container.id>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think is time to allign here also with kube_state container.id where container.runtime is not splitted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that is another issue right? Good if we can fix them both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a breaking change. For state_container metricset as far as I see, the container.id is the same form.

Copy link
Contributor

@gizas gizas Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. So it is time maybe to test here both container.ids and align them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does fixing mean? I prefer the splitted container.id

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check below the kubernetes.container.id (vs container.id)

Screenshot 2023-02-08 at 2 45 38 PM

I also prefer the splitted, so to remove it from kubernetes.container.id

Copy link
Contributor Author

@MichaelKatsoulis MichaelKatsoulis Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split := strings.Index(s.ContainerID, "://")
if split != -1 {
kubernetes2.ShouldPut(meta, "container.id", s.ContainerID[split+3:], base.Logger())
kubernetes2.ShouldPut(cmeta, "container.id", s.ContainerID[split+3:], base.Logger())

kubernetes2.ShouldPut(meta, "container.runtime", s.ContainerID[:split], base.Logger())
kubernetes2.ShouldPut(cmeta, "container.runtime", s.ContainerID[:split], base.Logger())
}
}

id := join(pod.GetObjectMeta().GetNamespace(), pod.GetObjectMeta().GetName(), container.Name)
m[id] = meta
cmeta.DeepUpdate(pmeta)
m[id] = cmeta
}
},
// delete
Expand Down