Skip to content
Merged
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(
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