Skip to content

Commit d6e9297

Browse files
This doesn't pass unit tests, but now I am writing some more helpful logs into our error logging so I can debug why this isn't working in system-tests
1 parent 43f214a commit d6e9297

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
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: 19 additions & 10 deletions
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 {
@@ -341,19 +344,21 @@ Optional<Cgroup> get_cgroup_version() {
341344
return nullopt;
342345
}
343346

344-
Optional<std::string> find_container_id_from_cgroup() {
347+
Optional<std::string> find_container_id_from_cgroup(
348+
const std::shared_ptr<tracing::Logger>& logger) {
345349
auto cgroup_fd = std::ifstream("/proc/self/cgroup", std::ios::in);
346350
if (!cgroup_fd.is_open()) {
347-
std::cerr << "failed to open /proc/self/cgroup" << std::endl;
351+
logger->log_error("failed to open /proc/self/cgroup");
348352
return nullopt;
349353
}
350354

351-
return find_container_id(cgroup_fd);
355+
return find_container_id(cgroup_fd, logger);
352356
}
353357
#endif
354358
} // namespace
355359

356-
Optional<std::string> find_container_id(std::istream& source) {
360+
Optional<std::string> find_container_id(std::istream& source,
361+
const std::shared_ptr<tracing::Logger>& logger) {
357362
static const std::string uuid_regex_str =
358363
"[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}"
359364
"|(?:[0-9a-f]{8}(?:-[0-9a-f]{4}){4}$)";
@@ -365,37 +370,41 @@ Optional<std::string> find_container_id(std::istream& source) {
365370

366371
std::string line;
367372
while (std::getline(source, line)) {
368-
std::cout << "Reading line: " << line << std::endl;
373+
logger->log_error("Reading line: " + line);
369374
// Example:
370375
// `0::/system.slice/docker-abcdef0123456789abcdef0123456789.scope`
371376
std::smatch match;
372377
if (std::regex_match(line, match, path_reg)) {
373378
assert(match.ready());
374379
assert(match.size() == 2);
375380

376-
std::cout << "Found container ID: " << match.str(1) << std::endl;
381+
logger->log_error("Found container ID: " + match.str(1));
377382
return match.str(1);
378383
}
379384
}
380385

381-
std::cout << "No container ID found" << std::endl;
386+
logger->log_error("No container ID found");
382387
return nullopt;
383388
}
384389

385-
Optional<ContainerID> get_id() {
390+
Optional<ContainerID> get_id(const std::shared_ptr<tracing::Logger>& logger) {
386391
#if defined(__linux__) || defined(__unix__)
387392
if (is_running_in_host_namespace()) {
388393
// Not in a container, no need to continue.
394+
logger->log_error("Not in a container, no need to continue.");
389395
return nullopt;
390396
}
391397

392398
auto maybe_cgroup = get_cgroup_version();
393-
if (!maybe_cgroup) return nullopt;
399+
if (!maybe_cgroup) {
400+
logger->log_error("Failed to get cgroup version.");
401+
return nullopt;
402+
}
394403

395404
ContainerID id;
396405
switch (*maybe_cgroup) {
397406
case Cgroup::v1: {
398-
if (auto maybe_id = find_container_id_from_cgroup()) {
407+
if (auto maybe_id = find_container_id_from_cgroup(logger)) {
399408
id.value = *maybe_id;
400409
id.type = ContainerID::Type::container_id;
401410
break;

src/datadog/platform_util.h

Lines changed: 5 additions & 2 deletions
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 {
@@ -90,13 +92,14 @@ struct ContainerID final {
9092
/// @param source The input from which to read the container ID.
9193
/// @return An Optional containing the container ID if found, otherwise
9294
/// nothing.
93-
Optional<std::string> find_container_id(std::istream& source);
95+
Optional<std::string> find_container_id(std::istream& source,
96+
const std::shared_ptr<tracing::Logger>& logger);
9497

9598
/// Function to retrieve the container metadata.
9699
///
97100
/// @return A `ContainerID` object containing id of the container in
98101
/// which the current process is running.
99-
Optional<ContainerID> get_id();
102+
Optional<ContainerID> get_id(const std::shared_ptr<tracing::Logger>& logger);
100103

101104
} // namespace container
102105

test/test_platform_util.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "platform_util.h"
44
#include "test.h"
5+
#include "null_logger.h"
56

67
using namespace datadog::tracing;
78

@@ -58,7 +59,7 @@ PLATFORM_UTIL_TEST("find docker container ID") {
5859

5960
std::istringstream is(test_case.input);
6061

61-
auto maybe_container_id = container::find_container_id(is);
62+
auto maybe_container_id = container::find_container_id(is, std::make_shared<NullLogger>());
6263
if (test_case.expected_container_id.has_value()) {
6364
REQUIRE(maybe_container_id.has_value());
6465
CHECK(*maybe_container_id == *test_case.expected_container_id);
@@ -186,7 +187,7 @@ PLATFORM_UTIL_TEST("find multiline container IDs") {
186187

187188
std::istringstream is(test_case.input);
188189

189-
auto maybe_container_id = container::find_container_id(is);
190+
auto maybe_container_id = container::find_container_id(is, std::make_shared<NullLogger>());
190191
if (test_case.expected_container_id.has_value()) {
191192
REQUIRE(maybe_container_id.has_value());
192193
CHECK(*maybe_container_id == *test_case.expected_container_id);

0 commit comments

Comments
 (0)