Skip to content

Fixes container detector for systemd & cgroupv1 with Docker #3429

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from logging import getLogger

from opentelemetry.sdk.resources import Resource, ResourceDetector
Expand All @@ -31,9 +32,14 @@ def _get_container_id_v1():
) as container_info_file:
for raw_line in container_info_file.readlines():
line = raw_line.strip()
if len(line) > _CONTAINER_ID_LENGTH:
container_id = line[-_CONTAINER_ID_LENGTH:]

match = re.search(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment here mentioning the issue?

r"^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$)", line
)
if match is not None:
container_id = match.group(1)
break

except FileNotFoundError as exception:
logger.warning("Failed to get container id. Exception: %s", exception)
return container_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ def test_container_id_detect_from_cgroup_file(self, mock_cgroup_file):
actual.attributes.copy(), MockContainerResourceAttributes
)

@patch(
"builtins.open",
new_callable=mock_open,
read_data=f"""0::/system.slice/docker-{MockContainerResourceAttributes[ResourceAttributes.CONTAINER_ID]}.scope
""",
)
def test_container_id_detect_from_cgroup_file_with_suffix(
self, mock_cgroup_file
):
actual = ContainerResourceDetector().detect()
self.assertDictEqual(
actual.attributes.copy(), MockContainerResourceAttributes
)

@patch(
"opentelemetry.resource.detector.container._get_container_id_v1",
return_value=None,
Expand Down
Loading