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

Conversation

SeanHood
Copy link

@SeanHood SeanHood commented Apr 16, 2025

Description

Running the container detector on certain Docker setups returns the incorrect container.id.

For example: 08767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605.scope where it should be 7be92808767a667f35c8505cbf40d14e931ef6db5b0210329cf193b15ba9d605

I believe this is from some combination of Cgroups v1 and systemd cgroup driver.

The output I have included in the test can be reproduced:

$ docker run --rm --cgroupns=host -it python:3 cat /proc/self/cgroup
0::/system.slice/docker-dcc3d0d5ddcfe3ce60dfe0978aa3598ee4a82f18531a276aeb4264923b16310b.scope

For the fix, I borrowed the regex which is used by opentelemetry-go uses for parsing the container ID from this file: https://github.com/open-telemetry/opentelemetry-go/blob/7639c93baffc820f9f19ab1f9d1c085b0b45e071/sdk/resource/container.go#L21

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Start up a container:

$ docker run --rm --cgroupns=host -v $(pwd):/code -it python:3 bash
root@f445652dca46:/# pip install opentelemetry-sdk
root@f445652dca46:/# cd /code/src/opentelemetry/resource/detector
root@f445652dca46:/code/src/opentelemetry/resource/detector# cat /proc/self/cgroup
0::/system.slice/docker-f445652dca462c6355cc7216a036942ae4e1dc9c2917f9e43d7003d7d23394dc.scope

Before change:

root@f445652dca46:/code/src/opentelemetry/resource/detector# python3
>>> import container
>>> container._get_container_id_v1()
'2dca462c6355cc7216a036942ae4e1dc9c2917f9e43d7003d7d23394dc.scope'

After change:

>>> import container
>>>
>>> container._get_container_id_v1()
'f445652dca462c6355cc7216a036942ae4e1dc9c2917f9e43d7003d7d23394dc'

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@SeanHood SeanHood requested a review from a team as a code owner April 16, 2025 22:59
Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

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

Thanks. Please add a changelog entry.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants