Skip to content

Commit

Permalink
Enhance container ID extraction to support complex Docker service for…
Browse files Browse the repository at this point in the history
…mat (#125)


Added another docker format parse that is in use by app services
  • Loading branch information
irit80 authored Jan 24, 2025
1 parent be5086b commit 9deb39d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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

0 comments on commit 9deb39d

Please sign in to comment.