Skip to content

Commit 565c4e0

Browse files
Put back the get_cgroup_version and add temporary logging to confirm the f_type returned by the statfs call
1 parent f0abaad commit 565c4e0

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/datadog/datadog_agent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ DatadogAgent::DatadogAgent(
171171
// Origin Detection headers are not necessary when Unix Domain Socket (UDS)
172172
// is used to communicate with the Datadog Agent.
173173
if (!contains(config.url.scheme, "unix")) {
174-
if (auto container_id = container::get_id()) {
174+
if (auto container_id = container::get_id(logger_)) {
175175
if (container_id->type == container::ContainerID::Type::container_id) {
176176
headers_.emplace("Datadog-Container-ID", container_id->value);
177177
headers_.emplace("Datadog-Entity-Id", "ci-" + container_id->value);

src/datadog/platform_util.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstdint>
55
#include <fstream>
66
#include <iostream>
7+
#include <memory>
78
#include <regex>
89

910
// clang-format off
@@ -45,6 +46,8 @@
4546
#endif
4647
// clang-format on
4748

49+
#include <datadog/logger.h>
50+
4851
namespace datadog {
4952
namespace tracing {
5053
namespace {
@@ -292,10 +295,18 @@ Expected<InMemoryFile> InMemoryFile::make(StringView) {
292295
namespace container {
293296
namespace {
294297
#if defined(__linux__) || defined(__unix__)
298+
/// Magic numbers from linux/magic.h:
299+
/// <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;
302+
295303
/// Magic number from linux/proc_ns.h:
296304
/// <https://github.com/torvalds/linux/blob/5859a2b1991101d6b978f3feb5325dad39421f29/include/linux/proc_ns.h#L41-L49>
297305
constexpr ino_t HOST_CGROUP_NAMESPACE_INODE = 0xeffffffb;
298306

307+
/// Represents the cgroup version of the current process.
308+
enum class Cgroup : char { v1, v2 };
309+
299310
Optional<ino_t> get_inode(std::string_view path) {
300311
struct stat buf;
301312
if (stat(path.data(), &buf) != 0) {
@@ -318,6 +329,25 @@ bool is_running_in_host_namespace() {
318329
return false;
319330
}
320331

332+
Optional<Cgroup> get_cgroup_version(const std::shared_ptr<tracing::Logger>& logger) {
333+
struct statfs buf;
334+
335+
if (statfs("/sys/fs/cgroup", &buf) != 0) {
336+
logger->log_error("Failed to statfs /sys/fs/cgroup");
337+
return nullopt;
338+
}
339+
340+
logger->log_error([&](auto& stream) {
341+
stream << "statfs /sys/fs/cgroup: f_type = " << buf.f_type;
342+
});
343+
if (buf.f_type == CGROUP_SUPER_MAGIC)
344+
return Cgroup::v1;
345+
else if (buf.f_type == CGROUP2_SUPER_MAGIC)
346+
return Cgroup::v2;
347+
348+
return nullopt;
349+
}
350+
321351
Optional<std::string> find_container_id_from_cgroup() {
322352
auto cgroup_fd = std::ifstream("/proc/self/cgroup", std::ios::in);
323353
if (!cgroup_fd.is_open()) {
@@ -355,8 +385,11 @@ Optional<std::string> find_container_id(std::istream& source) {
355385
return nullopt;
356386
}
357387

358-
Optional<ContainerID> get_id() {
388+
Optional<ContainerID> get_id(const std::shared_ptr<tracing::Logger>& logger) {
359389
#if defined(__linux__) || defined(__unix__)
390+
auto maybe_cgroup = get_cgroup_version(logger);
391+
if (!maybe_cgroup) return nullopt;
392+
360393
// Determine the container ID or inode
361394
ContainerID id;
362395
if (auto maybe_id = find_container_id_from_cgroup()) {

src/datadog/platform_util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// This component provides platform-dependent miscellanea.
44

55
#include <datadog/expected.h>
6+
#include <datadog/logger.h>
67
#include <datadog/string_view.h>
78

9+
#include <memory>
810
#include <string>
911

1012
namespace datadog {
@@ -96,7 +98,7 @@ Optional<std::string> find_container_id(std::istream& source);
9698
///
9799
/// @return A `ContainerID` object containing id of the container in
98100
/// which the current process is running.
99-
Optional<ContainerID> get_id();
101+
Optional<ContainerID> get_id(const std::shared_ptr<tracing::Logger>& logger);
100102

101103
} // namespace container
102104

0 commit comments

Comments
 (0)