diff --git a/statsd/container_linux.go b/statsd/container_linux.go index 9c54f4fd..78a81ac2 100644 --- a/statsd/container_linux.go +++ b/statsd/container_linux.go @@ -46,6 +46,9 @@ var ( expContainerID = regexp.MustCompile(fmt.Sprintf(`(%s|%s|%s)(?:.scope)?$`, uuidSource, containerSource, taskSource)) cIDMountInfoRegexp = regexp.MustCompile(cIDRegexpStr) + + // initContainerID initializes the container ID. + initContainerID = internalInitContainerID ) // parseContainerID finds the first container ID reading from r and returns it. @@ -135,7 +138,7 @@ func parseCgroupMountPath(r io.Reader) string { tokens := strings.Fields(line) if len(tokens) >= 3 { fsType := tokens[2] - if strings.HasPrefix(fsType, "cgroup") { + if fsType == "cgroup2" { return tokens[1] // line is formatted as "cgroup /sys/fs/cgroup/... cgroup..."" } } @@ -156,7 +159,7 @@ func parseCgroupNodePath(r io.Reader) string { // getCgroupInode returns the cgroup inode prefixed by "in-" and is used by the agent to retrieve the container ID func getCgroupInode(mountsPath, cgroupPath string) string { - // Retrieve a cgroup mount point from /proc/self/mounts + // Retrieve a cgroup mount point from /proc/mounts f, err := os.Open(mountsPath) if err != nil { return "" @@ -199,9 +202,20 @@ func isHostCgroupNamespace() bool { return inode == hostCgroupNamespaceInode } -// initContainerID initializes the container ID. +func isHostMountNamespace() bool { + fi, err := os.Stat("/proc/self/ns/mnt") + if err != nil { + return false + } + + inode := fi.Sys().(*syscall.Stat_t).Ino + + return inode == hostMntNamespaceInode +} + +// internalInitContainerID initializes the container ID. // It can either be provided by the user or read from cgroups. -func initContainerID(userProvidedID string, cgroupFallback bool) { +func internalInitContainerID(userProvidedID string, cgroupFallback bool) { initOnce.Do(func() { if userProvidedID != "" { containerID = userProvidedID diff --git a/statsd/container_stub.go b/statsd/container_stub.go index 8489f436..5a143d19 100644 --- a/statsd/container_stub.go +++ b/statsd/container_stub.go @@ -3,7 +3,7 @@ package statsd -func initContainerID(userProvidedID string, cgroupFallback bool) { +var initContainerID = func(userProvidedID string, cgroupFallback bool) { initOnce.Do(func() { if userProvidedID != "" { containerID = userProvidedID diff --git a/statsd/container_test.go b/statsd/container_test.go index 1405f368..6475398e 100644 --- a/statsd/container_test.go +++ b/statsd/container_test.go @@ -17,6 +17,21 @@ import ( "github.com/stretchr/testify/require" ) +// initContainerID initializes the container ID. +func init() { + initContainerID = dummyInitContainerId +} + +// dummyInitContainerId initializes the container ID if provided +func dummyInitContainerId(userProvidedID string, _ bool) { + initOnce.Do(func() { + if userProvidedID != "" { + containerID = userProvidedID + return + } + }) +} + func TestParseContainerID(t *testing.T) { for input, expectedResult := range map[string]string{ `other_line @@ -478,14 +493,21 @@ func TestGetCgroupInode(t *testing.T) { expectedResult: "in-%d", // Will be formatted with inode number }, { - description: "hybrid cgroup - should match the first which does not exist", + description: "should not match cgroup", + procMountsContent: "cgroup %s cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0\n", + cgroupNodeDir: "system.slice/docker-abcdef0123456789abcdef0123456789.scope", + procSelfCgroupContent: "0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope\n", + expectedResult: "", + }, + { + description: "hybrid cgroup - should match only cgroup2", procMountsContent: `other_line cgroup /sys/fs/cgroup/memory cgroup foo,bar 0 0 cgroup2 %s cgroup2 rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0 `, cgroupNodeDir: "system.slice/docker-abcdef0123456789abcdef0123456789.scope", procSelfCgroupContent: "0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope\n", - expectedResult: "", // Will be formatted with inode number + expectedResult: "in-%d", // Will be formatted with inode number }, { description: "Non-matching entry in /proc/self/cgroup",