forked from elastic/apm-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor cgroup parsing to use regexes, heavily inspired by elastic/a…
…pm-agent-go#342 (as in, pretty much copied from elastic/apm-agent-go#342)
- Loading branch information
Showing
5 changed files
with
84 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import re | ||
|
||
CGROUP_PATH = "/proc/self/cgroup" | ||
|
||
SYSTEMD_SCOPE_SUFFIX = ".scope" | ||
|
||
kubepods_regexp = re.compile( | ||
r"(?:^/kubepods/[^/]+/pod([^/]+)$)|(?:^/kubepods\.slice/kubepods-[^/]+\.slice/kubepods-[^/]+-pod([^/]+)\.slice$)" | ||
) | ||
|
||
container_id_regexp = re.compile("^[0-9A-Fa-f]{64}$") | ||
|
||
|
||
def get_cgroup_container_metadata(): | ||
""" | ||
Reads docker/kubernetes metadata (container id, pod id) from /proc/self/cgroup | ||
The result is a nested dictionary with the detected IDs, e.g. | ||
{ | ||
"container": {"id": "2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63"}, | ||
"pod": {"uid": "90d81341_92de_11e7_8cf2_507b9d4141fa"} | ||
} | ||
:return: a dictionary with the detected ids or {} | ||
""" | ||
if not os.path.exists(CGROUP_PATH): | ||
return {} | ||
with open(CGROUP_PATH) as f: | ||
return parse_cgroups(f) or {} | ||
|
||
|
||
def parse_cgroups(filehandle): | ||
""" | ||
Reads lines from a file handle and tries to parse docker container IDs and kubernetes Pod IDs. | ||
See tests.utils.docker_tests.test_cgroup_parsing for a set of test cases | ||
:param filehandle: | ||
:return: nested dictionary or None | ||
""" | ||
for line in filehandle: | ||
parts = line.strip().split(":") | ||
if len(parts) != 3: | ||
continue | ||
cgroup_path = parts[2] | ||
|
||
# Depending on the filesystem driver used for cgroup | ||
# management, the paths in /proc/pid/cgroup will have | ||
# one of the following formats in a Docker container: | ||
# | ||
# systemd: /system.slice/docker-<container-ID>.scope | ||
# cgroupfs: /docker/<container-ID> | ||
# | ||
# In a Kubernetes pod, the cgroup path will look like: | ||
# | ||
# systemd: | ||
# /kubepods.slice/kubepods-<QoS-class>.slice/kubepods-<QoS-class>-pod<pod-UID>.slice/<container-iD>.scope | ||
# cgroupfs: | ||
# /kubepods/<QoS-class>/pod<pod-UID>/<container-iD> | ||
|
||
directory, container_id = os.path.split(cgroup_path) | ||
if container_id.endswith(SYSTEMD_SCOPE_SUFFIX): | ||
container_id = container_id[: -len(SYSTEMD_SCOPE_SUFFIX)] | ||
if "-" in container_id: | ||
container_id = container_id.split("-", 1)[1] | ||
kubepods_match = kubepods_regexp.match(directory) | ||
if kubepods_match: | ||
pod_id = kubepods_match.group(1) | ||
if not pod_id: | ||
pod_id = kubepods_match.group(2) | ||
return {"container": {"id": container_id}, "pod": {"uid": pod_id}} | ||
elif container_id_regexp.match(container_id): | ||
return {"container": {"id": container_id}} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters