Skip to content

Commit

Permalink
Fix ContainerID retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
AliDatadog committed Jan 11, 2024
2 parents 515814d + 2d5462e commit ed10c5c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
10 changes: 8 additions & 2 deletions statsd/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ const (
taskSource = "[0-9a-f]{32}-\\d+"

containerdSandboxPrefix = "sandboxes"
containerRegexpStr = "([0-9a-f]{64})|([0-9a-f]{8}(-[0-9a-f]{4}){4}$)"
cIDRegexpStr = `([^\s/]+)/(` + containerRegexpStr + `)/[\S]*hostname`

// ContainerRegexpStr defines the regexp used to match container IDs
// ([0-9a-f]{64}) is standard container id used pretty much everywhere
// ([0-9a-f]{32}-[0-9]{10}) is container id used by AWS ECS
// ([0-9a-f]{8}(-[0-9a-f]{4}){4}$) is container id used by Garden
containerRegexpStr = "([0-9a-f]{64})|([0-9a-f]{32}-[0-9]{10})|([0-9a-f]{8}(-[0-9a-f]{4}){4}$)"
// cIDRegexpStr defines the regexp used to match container IDs in /proc/self/mountinfo
cIDRegexpStr = `.*/([^\s/]+)/(` + containerRegexpStr + `)/[\S]*hostname`

// From https://github.com/torvalds/linux/blob/5859a2b1991101d6b978f3feb5325dad39421f29/include/linux/proc_ns.h#L41-L49
// Currently, host namespace inode number are hardcoded, which can be used to detect
Expand Down
1 change: 1 addition & 0 deletions statsd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func TestParseMountinfo(t *testing.T) {
2035 2209 0:255 / /proc/scsi ro,relatime - tmpfs tmpfs ro
2036 2213 0:256 / /sys/firmware ro,relatime - tmpfs tmpfs ro
`: "fc7038bc73a8d3850c66ddbfb0b2901afa378bfcbb942cc384b051767e4ac6b0",
`1258 1249 254:1 /docker/volumes/0919c2d87ec8ba99f3c85fdada5fe26eca73b2fce73a5974d6030f30bf91cbaf/_data/lib/containerd/io.containerd.grpc.v1.cri/sandboxes/ca30bb64884083e29b1dc08a1081dd2df123f13f045dadb64dc346e56c0b6871/hostname /etc/hostname rw,relatime - ext4 /dev/vda1 rw,discard`: "",
} {
id := parseMountinfo(strings.NewReader(input))
assert.Equal(t, expectedResult, id)
Expand Down

0 comments on commit ed10c5c

Please sign in to comment.