Skip to content

Conversation

@SeanHood
Copy link
Contributor

@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
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Apr 17, 2025
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.

@xrmx xrmx enabled auto-merge (squash) June 3, 2025 15:24
@xrmx xrmx merged commit 8f7bab5 into open-telemetry:main Jun 3, 2025
625 checks passed
@github-project-automation github-project-automation bot moved this from Ready for review to Done in @xrmx's Python PR digest Jun 3, 2025
devmonkey22 pushed a commit to devmonkey22/opentelemetry-python-contrib that referenced this pull request Aug 5, 2025
…emetry#3429)

* Fixes container detector for systemd & cgroupv1 with Docker

* Update CHANGELOG

---------

Co-authored-by: Emídio Neto <[email protected]>
Co-authored-by: Riccardo Magliocchetti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants