Skip to content

Commit 8f7bab5

Browse files
SeanHoodemdnetoxrmx
authored
Fixes container detector for systemd & cgroupv1 with Docker (#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]>
1 parent ccf9cab commit 8f7bab5

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5151
- `opentelemetry-instrumentation-botocore` Add type check when extracting tool use from Bedrock request message content
5252
([#3548](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3548))
5353
- `opentelemetry-instrumentation-dbapi` Respect suppress_instrumentation functionality ([#3460](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3460))
54+
- `opentelemetry-resource-detector-container` Correctly parse container id when using systemd and cgroupsv1
55+
([#3429](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3429))
5456

5557
### Breaking changes
5658

resource/opentelemetry-resource-detector-container/src/opentelemetry/resource/detector/container/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import re
1516
from logging import getLogger
1617

1718
from opentelemetry.sdk.resources import Resource, ResourceDetector
@@ -31,9 +32,14 @@ def _get_container_id_v1():
3132
) as container_info_file:
3233
for raw_line in container_info_file.readlines():
3334
line = raw_line.strip()
34-
if len(line) > _CONTAINER_ID_LENGTH:
35-
container_id = line[-_CONTAINER_ID_LENGTH:]
35+
36+
match = re.search(
37+
r"^.*/(?:.*[-:])?([0-9a-f]+)(?:\.|\s*$)", line
38+
)
39+
if match is not None:
40+
container_id = match.group(1)
3641
break
42+
3743
except FileNotFoundError as exception:
3844
logger.warning("Failed to get container id. Exception: %s", exception)
3945
return container_id

resource/opentelemetry-resource-detector-container/tests/test_container.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def test_container_id_detect_from_cgroup_file(self, mock_cgroup_file):
5151
actual.attributes.copy(), MockContainerResourceAttributes
5252
)
5353

54+
@patch(
55+
"builtins.open",
56+
new_callable=mock_open,
57+
read_data=f"""0::/system.slice/docker-{MockContainerResourceAttributes[ResourceAttributes.CONTAINER_ID]}.scope
58+
""",
59+
)
60+
def test_container_id_detect_from_cgroup_file_with_suffix(
61+
self, mock_cgroup_file
62+
):
63+
actual = ContainerResourceDetector().detect()
64+
self.assertDictEqual(
65+
actual.attributes.copy(), MockContainerResourceAttributes
66+
)
67+
5468
@patch(
5569
"opentelemetry.resource.detector.container._get_container_id_v1",
5670
return_value=None,

0 commit comments

Comments
 (0)