-
Notifications
You must be signed in to change notification settings - Fork 234
added parsing of cgroups file #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
631c124
make joining of server url with API endpoint path a bit smarter
beniwohli 26467a5
add more test cases
beniwohli 396bf5c
added parsing of cgroups file
beniwohli 51fd517
add docker/kubernetes data to system dict
beniwohli b0a1b93
mock socket.gethostname() to ensure consistent behaviour
beniwohli 7585e0a
refactor cgroup parsing to use regexes, heavily inspired by elastic/a…
beniwohli aa72770
added test for hostname detection if k8s metadata is set via environment
graphaelli 0b6d3d0
Merge branch 'master' into read-cgroups
beniwohli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 contains hidden or 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 hidden or 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,37 @@ | ||
| import mock | ||
| import pytest | ||
|
|
||
| from elasticapm.utils import cgroup, compat | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "test_input,expected", | ||
| [ | ||
| ( | ||
| "12:devices:/docker/051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76", | ||
| {"container": {"id": "051e2ee0bce99116029a13df4a9e943137f19f957f38ac02d6bad96f9b700f76"}}, | ||
| ), | ||
| ( | ||
| "1:name=systemd:/system.slice/docker-cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411.scope", | ||
| {"container": {"id": "cde7c2bab394630a42d73dc610b9c57415dced996106665d427f6d0566594411"}}, | ||
| ), | ||
| ( | ||
| "1:name=systemd:/kubepods/besteffort/pode9b90526-f47d-11e8-b2a5-080027b9f4fb/15aa6e53-b09a-40c7-8558-c6c31e36c88a", | ||
| { | ||
| "container": {"id": "15aa6e53-b09a-40c7-8558-c6c31e36c88a"}, | ||
| "pod": {"uid": "e9b90526-f47d-11e8-b2a5-080027b9f4fb"}, | ||
| }, | ||
| ), | ||
| ( | ||
| "1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod90d81341_92de_11e7_8cf2_507b9d4141fa.slice/crio-2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63.scope", | ||
| { | ||
| "container": {"id": "2227daf62df6694645fee5df53c1f91271546a9560e8600a525690ae252b7f63"}, | ||
| "pod": {"uid": "90d81341_92de_11e7_8cf2_507b9d4141fa"}, | ||
| }, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_cgroup_parsing(test_input, expected): | ||
| f = compat.StringIO(test_input) | ||
| result = cgroup.parse_cgroups(f) | ||
| assert result == expected |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth a test to ensure system hostname is used as pod name when kubernetes info is not detected but partially set by environ? Seems like overkill but figured I'd mention it to prove I reviewed this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would have been the ideal chance to use that new fancy review-as-commit thingy :D set you as author of the commit instead. Not that it makes a huge difference after I squash-merge this :D