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

Enhance container ID extraction to support complex Docker service format #125

Merged
merged 8 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions ProcessInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ int ProcessInfo::ExtractCGroupContainerId(const std::string& content) {
const size_t docker_prefix_len = strlen(docker_prefix);
const char *system_docker_prefix = "/system.slice/docker-";
const size_t system_docker_prefix_len = strlen(system_docker_prefix);
const char *complex_docker_service_prefix = "/system.slice/docker.service/";


while ((line_end = strchr(ptr, '\n')) != nullptr) {
// Check for containerd format
Expand Down Expand Up @@ -408,6 +410,26 @@ int ProcessInfo::ExtractCGroupContainerId(const std::string& content) {
}
}

// Check for complex docker format
const char *complex_format_pos = strstr(ptr, complex_docker_service_prefix);
if (complex_format_pos != nullptr && complex_format_pos < line_end) {
// Search for '/' before line_end
const char *id_start = nullptr;
for (const char *p = line_end; p >= complex_format_pos; --p) {
if (*p == '/') {
id_start = p + 1;
break;
}
}
if (id_start != nullptr) {
// make sure we have 12 characters left in the line before i read them
if (line_end > id_start && id_start + 12 <= line_end) {
_container_id = std::string(id_start, 12); // Extract the container ID from the end of the line
return 0;
}
}
}

ptr = line_end + 1;
}

Expand Down
5 changes: 5 additions & 0 deletions ProcessInfoTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ BOOST_AUTO_TEST_CASE(container_id_extraction_test) {
BOOST_CHECK_EQUAL(ProcessInfoTests::ExtractCGroupContainerId(*processInfo, system_docker_line), 0);
BOOST_CHECK_EQUAL(processInfo->container_id(), "ebe83cd204c5");

// Test system.slice complex Docker format
std::string complex_format_line = "12:devices:/system.slice/docker.service/antares/customer/site/lwasv2-php-test/9c088fd6_lwasv2-php-test\n";
BOOST_CHECK_EQUAL(ProcessInfoTests::ExtractCGroupContainerId(*processInfo, complex_format_line), 0);
BOOST_CHECK_EQUAL(processInfo->container_id(), "9c088fd6_lwa");

// Test invalid format
std::string invalid_line = "some text without container id\n";
BOOST_CHECK_NE(ProcessInfoTests::ExtractCGroupContainerId(*processInfo, invalid_line), 0);
Expand Down
Loading