Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion processmanager/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package processmanager // import "go.opentelemetry.io/ebpf-profiler/processmanag

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions processmanager/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package processmanager // import "go.opentelemetry.io/ebpf-profiler/processmanager"

import (
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
})
Expand Down