@@ -297,15 +297,15 @@ namespace {
297297#if defined(__linux__) || defined(__unix__)
298298// / Magic numbers from linux/magic.h:
299299// / <https://github.com/torvalds/linux/blob/ca91b9500108d4cf083a635c2e11c884d5dd20ea/include/uapi/linux/magic.h#L71>
300- constexpr uint64_t CGROUP_SUPER_MAGIC = 0x27e0eb ;
301- constexpr uint64_t CGROUP2_SUPER_MAGIC = 0x63677270 ;
300+ // constexpr uint64_t CGROUP_SUPER_MAGIC = 0x27e0eb;
301+ // constexpr uint64_t CGROUP2_SUPER_MAGIC = 0x63677270;
302302
303303// / Magic number from linux/proc_ns.h:
304304// / <https://github.com/torvalds/linux/blob/5859a2b1991101d6b978f3feb5325dad39421f29/include/linux/proc_ns.h#L41-L49>
305305// constexpr ino_t HOST_CGROUP_NAMESPACE_INODE = 0xeffffffb;
306306
307307// / Represents the cgroup version of the current process.
308- enum class Cgroup : char { v1, v2 };
308+ // enum class Cgroup : char { v1, v2 };
309309
310310Optional<ino_t > get_inode (std::string_view path) {
311311 struct stat buf;
@@ -316,21 +316,6 @@ Optional<ino_t> get_inode(std::string_view path) {
316316 return buf.st_ino ;
317317}
318318
319- Optional<Cgroup> get_cgroup_version () {
320- struct statfs buf;
321-
322- if (statfs (" /sys/fs/cgroup" , &buf) != 0 ) {
323- return nullopt ;
324- }
325-
326- if (buf.f_type == CGROUP_SUPER_MAGIC)
327- return Cgroup::v1;
328- else if (buf.f_type == CGROUP2_SUPER_MAGIC)
329- return Cgroup::v2;
330-
331- return nullopt ;
332- }
333-
334319Optional<std::string> find_container_id_from_cgroup (
335320 const std::shared_ptr<tracing::Logger>& logger) {
336321 auto cgroup_fd = std::ifstream (" /proc/self/cgroup" , std::ios::in);
@@ -377,32 +362,17 @@ Optional<std::string> find_container_id(std::istream& source,
377362Optional<ContainerID> get_id (const std::shared_ptr<tracing::Logger>& logger) {
378363#if defined(__linux__) || defined(__unix__)
379364 // Comment out the host namespace check, following the algorithm from other tracers.
365+ // Also don't get the cgroup version upfront, following the algorithm from other tracers.
380366 // This should allow us to detect containers running in Docker Desktop.
381367
382- auto maybe_cgroup = get_cgroup_version ();
383- if (!maybe_cgroup) {
384- logger->log_error (" Failed to get cgroup version." );
385- return nullopt ;
386- }
387-
388368 ContainerID id;
389- switch (*maybe_cgroup) {
390- case Cgroup::v1: {
391- if (auto maybe_id = find_container_id_from_cgroup (logger)) {
392- id.value = *maybe_id;
393- id.type = ContainerID::Type::container_id;
394- break ;
395- }
396- }
397- // NOTE(@dmehala): failed to find the container ID, try getting the cgroup
398- // inode.
399- [[fallthrough]];
400- case Cgroup::v2: {
401- if (auto maybe_inode = get_inode (" /sys/fs/cgroup" )) {
402- id.type = ContainerID::Type::cgroup_inode;
403- id.value = std::to_string (*maybe_inode);
404- }
405- }; break ;
369+ if (auto maybe_id = find_container_id_from_cgroup (logger)) {
370+ id.value = *maybe_id;
371+ id.type = ContainerID::Type::container_id;
372+ } else if (auto maybe_inode = get_inode (" /sys/fs/cgroup" )) {
373+ // NOTE(@dmehala): failed to find the container ID, try getting the cgroup inode.
374+ id.type = ContainerID::Type::cgroup_inode;
375+ id.value = std::to_string (*maybe_inode);
406376 }
407377
408378 return id;
0 commit comments