datadog-agent icon indicating copy to clipboard operation
datadog-agent copied to clipboard

cgroups: register sub-cgroup inodes for DogStatsD origin detection on cgroupv2

Open metrictwo opened this issue 5 months ago • 3 comments

Summary

On AL2023/CRI-O with cgroupv2 and cgroup namespace isolation, DogStatsD origin detection silently drops all pod-level tags for every metric. This PR fixes the root cause.

Root cause

CRI-O creates cgroups with this layout:

/sys/fs/cgroup/kubepods/.../crio-<HASH>.scope/       ← inode A (registered)
/sys/fs/cgroup/kubepods/.../crio-<HASH>.scope/container/  ← inode B (skipped)

When a container process runs inside a cgroup namespace, its namespace root is the container/ subdirectory. The DogStatsD client reports inode B via |c:in-<inode>. However, readerV2.parseCgroups() uses WalkDir which visits .scope/ first and registers inode A. The existing overwrite guard (if _, exists := res[id]; !exists) then skips container/, so inode B is never entered into cgroupByInode. Every origin detection lookup misses, and GetCgroupByInode returns nil — silently, with no logging at any level.

Fix

Return a second value map[uint64]string (sub-cgroup inode → container ID) from parseCgroups(). When the overwrite guard fires on a duplicate container ID, record the sub-directory's inode in this map instead of discarding it. In RefreshCgroups, register both the primary inode and all sub-cgroup inodes in cgroupByInode, both pointing to the same Cgroup. Stats collection is unaffected (still uses the parent .scope/ path); only inode-based origin detection gains the extra entries.

Validation

Validated on a Kubernetes cluster running AL2023 + CRI-O + cgroupv2 with cgroup namespace isolation enabled. Before the fix, GetCgroupByInode returned nil for every DogStatsD packet. After the fix, lookups resolve correctly and pod-level tags (pod_name, kube_namespace, etc.) appear on all metrics.

Diff

pkg/util/cgroups/reader.go   | changed readerImpl interface, updated RefreshCgroups
pkg/util/cgroups/readerv1.go | updated signature to return nil for sub-inodes (cgroupv1 unaffected)
pkg/util/cgroups/readerv2.go | collect sub-cgroup inodes during WalkDir
3 files changed, 33 insertions(+), 9 deletions(-)

metrictwo avatar Jun 29 '26 15:06 metrictwo

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

github-actions[bot] avatar Jun 29 '26 15:06 github-actions[bot]

I have read the CLA Document and I hereby sign the CLA

metrictwo avatar Jun 30 '26 12:06 metrictwo

Oops, closure was accidental, ignore.

metrictwo avatar Jun 30 '26 15:06 metrictwo

@frank-spano I'm not sure how we got into this state, but it looke like it needs another stamp from you?

metrictwo avatar Jul 03 '26 10:07 metrictwo

Bump on this :)

metrictwo avatar Jul 15 '26 14:07 metrictwo