diff --git a/processmanager/helpers.go b/processmanager/helpers.go index db3de2890..a98762eff 100644 --- a/processmanager/helpers.go +++ b/processmanager/helpers.go @@ -5,6 +5,7 @@ package processmanager // import "go.opentelemetry.io/ebpf-profiler/processmanag import ( "bufio" + "bytes" "fmt" "io" "os" @@ -91,6 +92,7 @@ type FileIDMapper interface { Set(pre host.FileID, post libpf.FileID) } +// parseContainerID parses cgroup v2 container IDs func parseContainerID(cgroupFile io.Reader) string { scanner := bufio.NewScanner(cgroupFile) buf := make([]byte, 512) @@ -101,7 +103,11 @@ func parseContainerID(cgroupFile io.Reader) string { scanner.Buffer(buf, 8192) var pathParts []string for scanner.Scan() { - line := scanner.Text() + b := scanner.Bytes() + if bytes.Equal(b, []byte("0::/")) { + continue // Skip a common case + } + line := string(b) pathParts = cgroupv2ContainerIDPattern.FindStringSubmatch(line) if pathParts == nil { log.Debugf("Could not extract cgroupv2 path from line: %s", line) diff --git a/processmanager/helpers_test.go b/processmanager/helpers_test.go index 8510ca96d..90119b9f8 100644 --- a/processmanager/helpers_test.go +++ b/processmanager/helpers_test.go @@ -4,7 +4,7 @@ package processmanager // import "go.opentelemetry.io/ebpf-profiler/processmanager" import ( - "bytes" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -45,10 +45,8 @@ func TestExtractContainerID(t *testing.T) { } for _, tc := range tests { - tc := tc t.Run(tc.expectedContainerID, func(t *testing.T) { - reader := bytes.NewReader([]byte(tc.line)) - + reader := strings.NewReader(tc.line) gotContainerID := parseContainerID(reader) assert.Equal(t, tc.expectedContainerID, gotContainerID) })