Skip to content

ProcessManager: Extract containerID with the rest of process metadata#577

Merged
christos68k merged 9 commits intomainfrom
ck/cid
Jul 9, 2025
Merged

ProcessManager: Extract containerID with the rest of process metadata#577
christos68k merged 9 commits intomainfrom
ck/cid

Conversation

@christos68k
Copy link
Copy Markdown
Member

@christos68k christos68k commented Jul 3, 2025

Summary

This is an alternate solution to #548, see here and here for more context.

It includes all the commits that @florianl did in #548 (which is now closed), we can review and merge #577 standalone.

florianl and others added 8 commits June 25, 2025 10:57
This is a follow up to #535.

While the tracer part of the project reports the cgroup v2 path for each sample, the
reporter is expected to report the container ID. The container ID then can be used to
associate the sample to more detailed resource information.

In the context of OTel collector, the container ID then can be used like this:

```
  k8sattributes:
    auth_type: "serviceAccount"
    passthrough: false
    filter:
      node_from_env_var: KUBERNETES_NODE_NAME
    extract:
      metadata:
        - k8s.pod.name
        - k8s.pod.uid
        - k8s.deployment.name
        - k8s.namespace.name
        - k8s.node.name
        - k8s.pod.start_time
        - service.namespace
        - service.name
        - service.version
        - service.instance.id
      labels:
        - tag_name: app.label.component
          key: app.kubernetes.io/component
          from: pod
      otel_annotations: true
    pod_association:
      - sources:
          - from: resource_attribute
            name: container.id
```

As the cgroupv2 path contains further information, that could be beneficial for other
reporters, the extraction of the container ID happens in the reporter.

Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
@florianl
Copy link
Copy Markdown
Member

florianl commented Jul 3, 2025

Repeating my #548 (comment) here as well:

While it seems tempting to move process data extraction to processmanager, it is not the purpose of processmanager in the first place. Therefore, I suggest:

Copy link
Copy Markdown
Member

@florianl florianl left a comment

Choose a reason for hiding this comment

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

I think, @fabled summarized the situation well in #548 (comment) and so I think, this option is a good interim solution until there is a plugin mechanism to extract such information by configuration.

@christos68k
Copy link
Copy Markdown
Member Author

I think, @fabled summarized the situation well in #548 (comment) and so I think, this option is a good interim solution until there is a plugin mechanism to extract such information by configuration.

OK let's close #548, review #577 (I still need to do another pass and test some more) and I'll merge after (I'll also add you as a co-author to the squashed commit).

@florianl
Copy link
Copy Markdown
Member

florianl commented Jul 3, 2025

Closing in favor of #577

@florianl florianl closed this Jul 3, 2025
@florianl florianl mentioned this pull request Jul 3, 2025
@christos68k christos68k reopened this Jul 3, 2025
@christos68k
Copy link
Copy Markdown
Member Author

Closing in favor of #577

This is #577 😄

@florianl
Copy link
Copy Markdown
Member

florianl commented Jul 3, 2025

This is #577 😄

sry 🙏

@christos68k christos68k enabled auto-merge (squash) July 3, 2025 20:02
@christos68k christos68k self-assigned this Jul 3, 2025
@Anthony-Tafoya
Copy link
Copy Markdown

Anthony-Tafoya commented Jul 7, 2025

Hello, I understand this is still in review. However, tried to run the profiler from the commit here and my container.id started to show up as empty for all entries. Thought it would be relevant to drop this here:

Screenshot 2025-07-06 at 7 59 57 PM

@florianl
Copy link
Copy Markdown
Member

florianl commented Jul 7, 2025

@Anthony-Tafoya can you provide the full cgroupv2 path of one of your containers? You can fetch it from /proc/<PID>/cgroup.

@florianl florianl self-requested a review July 7, 2025 07:08
@Anthony-Tafoya
Copy link
Copy Markdown

After taking a further look, this works fine. I was testing on a docker compose setup (so I would not have to deploy every small yaml change). Before I was seeing the login session’s cgroup like /user.slice/user‑1000.slice/session‑1.scope and /../... On my kubernetes cluster this works as expected (grabs the containerID). Apologies for the confusion

@christos68k christos68k requested review from fabled and rockdaboot July 7, 2025 19:18
Comment thread processmanager/helpers.go Outdated
@florianl florianl self-requested a review July 8, 2025 06:06
}

for _, tc := range tests {
tc := tc
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
tc := tc

for _, tc := range tests {
tc := tc
t.Run(tc.expectedContainerID, func(t *testing.T) {
reader := bytes.NewReader([]byte(tc.line))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
reader := bytes.NewReader([]byte(tc.line))
reader := strings.NewReader(tc.line)

Comment thread processmanager/helpers.go
Set(pre host.FileID, post libpf.FileID)
}

func parseContainerID(cgroupFile io.Reader) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the tests only contain cgroupV2 strings, should we document this function to support cgroupV2 only? Otherwise, we should add cgroupV1 test cases.
IMO, we don't need to support for v1.

Comment thread processmanager/helpers.go
scanner.Buffer(buf, 8192)
var pathParts []string
for scanner.Scan() {
line := scanner.Text()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Skip regex processing and heap allocations for the very common case of "0::/".

Suggested change
line := scanner.Text()
b := scanner.Bytes()
if bytes.Equal(b, []byte("0::/")) {
continue // Skip a common case
}
line := string(b)

Copy link
Copy Markdown
Contributor

@rockdaboot rockdaboot left a comment

Choose a reason for hiding this comment

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

LGTM, just some minor comments

@christos68k christos68k merged commit dba0c0e into main Jul 9, 2025
27 checks passed
@christos68k christos68k deleted the ck/cid branch July 9, 2025 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants