Skip to content

Commit

Permalink
containermetadata: make default pattern more restrictive (#39)
Browse files Browse the repository at this point in the history
the default pattern would match systemd service names which only
contain letter a-f (see added test which fails with the current code)

this commit extracts the container regex pattern used by the
datadog-agent, and uses it as a default pattern.

this makes the pattern more restrictive, avoiding the addition of
an incorrect container_id tags for some systemd services
  • Loading branch information
Gandem authored Nov 14, 2024
1 parent 3ab1bc8 commit d73e5de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion containermetadata/containermetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ var (
dockerBuildkitPattern = regexp.MustCompile(`\d+:.*:/.*/*docker/buildkit/([0-9a-z]+)`)
lxcPattern = regexp.MustCompile(`\d+::/lxc\.(monitor|payload)\.([a-zA-Z]+)/`)
containerdPattern = regexp.MustCompile(`\d+:.+:/([a-zA-Z0-9_-]+)/+([a-zA-Z0-9_-]+)`)
defaultPattern = regexp.MustCompile(`^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$)`)
// The inner container ID pattern is extracted from:
// https://github.com/DataDog/datadog-agent/blob/6e43db2/pkg/util/cgroups/reader.go#L24C24-L24C90
defaultPattern = regexp.MustCompile(`^.*/(?:.*[-:])?([0-9a-f]{64})|([0-9a-f]{32}-\\d+)|([0-9a-f]{8}(-[0-9a-f]{4}){4}$)(?:\.|\s*$)`)

containerIDPattern = regexp.MustCompile(`.+://([0-9a-f]{64})`)

Expand Down
6 changes: 6 additions & 0 deletions containermetadata/containermetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func TestExtractContainerIDFromFile(t *testing.T) {
containerdClient: &containerd.Client{},
},
},
{
name: "systemd",
cgroupname: "testdata/cgroupv1systemd",
expContainerID: "",
expEnv: envUndefined,
},
}

defaultHandler := &containerMetadataProvider{
Expand Down
14 changes: 14 additions & 0 deletions containermetadata/testdata/cgroupv1systemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
13:devices:/system.slice/dd.taskapi.service
12:pids:/system.slice/dd.taskapi.service
11:cpu,cpuacct:/system.slice/dd.taskapi.service
10:cpuset:/
9:memory:/system.slice/dd.taskapi.service
8:rdma:/
7:freezer:/
6:misc:/
5:hugetlb:/
4:blkio:/system.slice/dd.taskapi.service
3:perf_event:/
2:net_cls,net_prio:/
1:name=systemd:/system.slice/dd.taskapi.service
0::/system.slice/dd.taskapi.service

0 comments on commit d73e5de

Please sign in to comment.