Skip to content
Open
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
2 changes: 1 addition & 1 deletion include/datadog/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Tracer {
std::string config() const;

private:
void store_config();
void store_config(const std::unordered_map<std::string, std::string>&);
};

} // namespace tracing
Expand Down
8 changes: 8 additions & 0 deletions include/datadog/tracer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ struct TracerConfig {
/// Overridden by the `DD_APM_TRACING_ENABLED` environment variable. Defaults
/// to `true`.
Optional<bool> tracing_enabled;

/// A mapping of process-specific tags used to uniquely identify processes.
///
/// The `process_tags` map allows associating arbitrary string-based keys and
/// values with a process. These tags are consumed as fact for identifying
/// processes.
std::unordered_map<std::string, std::string> process_tags;
};

// `FinalizedTracerConfig` contains `Tracer` implementation details derived from
Expand Down Expand Up @@ -218,6 +225,7 @@ class FinalizedTracerConfig final {
std::shared_ptr<EventScheduler> event_scheduler;
std::shared_ptr<HTTPClient> http_client;
bool tracing_enabled;
std::unordered_map<std::string, std::string> process_tags;
};

// Return a `FinalizedTracerConfig` from the specified `config` and from any
Expand Down
5 changes: 4 additions & 1 deletion src/datadog/platform_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// This component provides platform-dependent miscellanea.

#include <datadog/expected.h>
#include <datadog/optional.h>
#include <datadog/string_view.h>

#include <filesystem>
#include <string>
#include <vector>

// clang-format off
#if defined(__x86_64__) || defined(_M_X64)
Expand Down Expand Up @@ -83,6 +84,8 @@ int get_process_id();

std::string get_process_name();

Optional<std::filesystem::path> get_process_path();

int at_fork_in_child(void (*on_fork)());

namespace container {
Expand Down
12 changes: 12 additions & 0 deletions src/datadog/platform_util_darwin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <libproc.h>
#include <pthread.h>
#include <sys/sysctl.h>
#include <sys/types.h>
Expand All @@ -14,6 +15,8 @@
#define DD_SDK_OS "Darwin"
#define DD_SDK_KERNEL "Darwin"

namespace fs = std::filesystem;

namespace datadog {
namespace tracing {
namespace {
Expand Down Expand Up @@ -56,6 +59,15 @@ std::string get_hostname() { return get_host_info().hostname; }

int get_process_id() { return ::getpid(); }

Optional<std::filesystem::path> get_process_path() {
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
if (!proc_pidpath(::getpid(), pathbuf, sizeof(pathbuf))) {
return nullopt;
}

return fs::path(pathbuf);
}

std::string get_process_name() {
const char* process_name = getprogname();
return (process_name != nullptr) ? process_name : "unknown-service";
Expand Down
6 changes: 6 additions & 0 deletions src/datadog/platform_util_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define DD_SDK_OS "GNU/Linux"
#define DD_SDK_KERNEL "Linux"

namespace fs = std::filesystem;

namespace datadog {
namespace tracing {
namespace {
Expand Down Expand Up @@ -79,6 +81,10 @@ std::string get_hostname() { return get_host_info().hostname; }

int get_process_id() { return ::getpid(); }

Optional<std::filesystem::path> get_process_path() {
return fs::path(program_invocation_name);
}

std::string get_process_name() { return program_invocation_short_name; }

int at_fork_in_child(void (*on_fork)()) {
Expand Down
30 changes: 26 additions & 4 deletions src/datadog/platform_util_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "platform_util.h"

using namespace std::literals;

namespace datadog {
namespace tracing {
namespace {
Expand All @@ -19,7 +21,7 @@ std::tuple<std::string, std::string> get_windows_info() {
// application manifest, which is the lowest version supported by the
// application. Use `RtlGetVersion` to obtain the accurate OS version
// regardless of the manifest.
using RtlGetVersion = auto (*)(LPOSVERSIONINFOEXW)->NTSTATUS;
using RtlGetVersion = auto(*)(LPOSVERSIONINFOEXW)->NTSTATUS;

RtlGetVersion func =
(RtlGetVersion)GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");
Expand Down Expand Up @@ -98,6 +100,25 @@ std::string get_hostname() { return get_host_info().hostname; }

int get_process_id() { return GetCurrentProcessId(); }

Optional<std::filesystem::path> get_process_path() {
const char* cmdline = GetCommandLineA();
if (cmdline == NULL) return nullopt;

StringView cmdline_sv{cmdline};
auto end = cmdline_sv.find_first_of(" \t"sv);
if (end == cmdline_sv::npos) {
return cmdline;
}

if (cmdline_sv[end - 1] == '"') {
--end;
}

size_t beg = cmdline_sv[0] == '"' ? 1 : 0;

return cmdline_sv.substr(beg, end - beg);
}

std::string get_process_name() {
TCHAR exe_name[MAX_PATH];
if (GetModuleFileName(NULL, exe_name, MAX_PATH) <= 0) {
Expand Down Expand Up @@ -161,11 +182,12 @@ Optional<std::string> find_container_id(std::istream& source) {
source.clear();
source.seekg(0);

// Perform a second pass using a regular expression for matching container IDs
// in a Fargate environment. This two-step approach is used because STL
// Perform a second pass using a regular expression for matching container
// IDs in a Fargate environment. This two-step approach is used because STL
// `regex` is relatively slow, so we avoid using it unless necessary.
static const std::string uuid_regex_str =
"[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}"
"[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{"
"12}"
"|(?:[0-9a-f]{8}(?:-[0-9a-f]{4}){4}$)";
static const std::string container_regex_str = "[0-9a-f]{64}";
static const std::string task_regex_str = "[0-9a-f]{32}-\\d+";
Expand Down
20 changes: 17 additions & 3 deletions src/datadog/tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <algorithm>
#include <cassert>
#include <filesystem>

#include "config_manager.h"
#include "datadog_agent.h"
Expand All @@ -30,6 +31,8 @@
#include "trace_sampler.h"
#include "w3c_propagation.h"

namespace fs = std::filesystem;

namespace datadog {
namespace tracing {

Expand Down Expand Up @@ -99,7 +102,17 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
});
}

store_config();
std::unordered_map<std::string, std::string> process_tags(
config.process_tags);
process_tags.emplace("entrypoint.name", get_process_name());
process_tags.emplace("entrypoint.type", "executable");
process_tags.emplace("entrypoint.workdir", fs::current_path().filename());
if (auto maybe_process_path = get_process_path();
maybe_process_path.has_value()) {
process_tags.emplace("entrypoint.basedir",
maybe_process_path->parent_path().filename());
}
store_config(process_tags);
}

std::string Tracer::config() const {
Expand Down Expand Up @@ -129,7 +142,8 @@ std::string Tracer::config() const {
return config.dump();
}

void Tracer::store_config() {
void Tracer::store_config(
const std::unordered_map<std::string, std::string>& process_tags) {
auto maybe_file =
InMemoryFile::make(std::string("datadog-tracer-info-") + short_uuid());
if (auto error = maybe_file.if_error()) {
Expand Down Expand Up @@ -162,7 +176,7 @@ void Tracer::store_config() {
"service_name", [&](auto& buffer) { return msgpack::pack_string(buffer, defaults->service); },
"service_env", [&](auto& buffer) { return msgpack::pack_string(buffer, defaults->environment); },
"service_version", [&](auto& buffer) { return msgpack::pack_string(buffer, defaults->version); },
"process_tags", [&](auto& buffer) { return msgpack::pack_string(buffer, ""); },
"process_tags", [&](auto& buffer) { return msgpack::pack_string(buffer, join_tags(process_tags)); },
"container_id", [&](auto& buffer) { return msgpack::pack_string(buffer, container_id); }
);
// clang-format on
Expand Down
2 changes: 2 additions & 0 deletions src/datadog/tracer_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig &user_config,
final_config.runtime_id = user_config.runtime_id;
}

final_config.process_tags = user_config.process_tags;

auto agent_finalized =
finalize_config(user_config.agent, final_config.logger, clock);
if (auto *error = agent_finalized.if_error()) {
Expand Down
Loading