Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Set a default service name (DD_SERVICE) when no service is configured #158

Merged
merged 4 commits into from
Oct 8, 2024
Merged
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
1 change: 0 additions & 1 deletion doc/maintainers.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ david@ein:~/src/dd-trace-cpp/src/datadog$ sed -n 's/^\s*\(\w\+\)\s*=\s*[0-9]\+,\
1 SPAN_SAMPLING_RULES_MAX_PER_SECOND_WRONG_TYPE
1 SPAN_SAMPLING_RULES_INVALID_JSON
1 SPAN_SAMPLING_RULES_FILE_IO
1 SERVICE_NAME_REQUIRED
1 RULE_WRONG_TYPE
1 RULE_TAG_WRONG_TYPE
1 RULE_PROPERTY_WRONG_TYPE
Expand Down
2 changes: 1 addition & 1 deletion include/datadog/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Error {
// versions.
enum Code {
OTHER = 1,
SERVICE_NAME_REQUIRED = 2,
// SERVICE_NAME_REQUIRED = 2,
MESSAGEPACK_ENCODE_FAILURE = 3,
CURL_REQUEST_FAILURE = 4,
DATADOG_AGENT_NULL_HTTP_CLIENT = 5,
Expand Down
23 changes: 23 additions & 0 deletions src/datadog/platform_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ int get_process_id() {
#endif
}

std::string get_process_name() {
#if defined(__APPLE__) || defined(__FreeBSD__)
char* process_name = getprogname();
return (process_name != nullptr) ? process_name : "unknown-service";
#elif defined(__linux__) || defined(__unix__)
return program_invocation_short_name;
#elif defined(_MSC_VER)
TCHAR exe_name[MAX_PATH];
if (GetModuleFileName(NULL, exe_name, MAX_PATH) <= 0) {
return "unknown-service";
}
#ifdef UNICODE
std::wstring wStr(exe_name);
std::string path = std::string(wStr.begin(), wStr.end());
#else
std::string path = std::string(exe_name);
#endif
return path.substr(path.find_last_of("/\\") + 1);
#else
return "unknown-service";
#endif
}

int at_fork_in_child(void (*on_fork)()) {
#if defined(_MSC_VER)
// Windows does not have `fork`, and so this is not relevant there.
Expand Down
2 changes: 2 additions & 0 deletions src/datadog/platform_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ std::string get_hostname();

int get_process_id();

std::string get_process_name();

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

} // namespace tracing
Expand Down
3 changes: 2 additions & 1 deletion src/datadog/tracer_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "datadog_agent.h"
#include "json.hpp"
#include "parse_util.h"
#include "platform_util.h"
#include "string_util.h"

namespace datadog {
Expand Down Expand Up @@ -263,7 +264,7 @@ Expected<FinalizedTracerConfig> finalize_config(const TracerConfig &user_config,
pick(env_config->service, user_config.service, "");

if (final_config.defaults.service.empty()) {
return Error{Error::SERVICE_NAME_REQUIRED, "Service name is required."};
final_config.defaults.service = get_process_name();
}

final_config.metadata[ConfigName::SERVICE_NAME] = ConfigMetadata(
Expand Down
2 changes: 0 additions & 2 deletions test/system-tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ int main(int argc, char* argv[]) {
datadog::tracing::TracerConfig config;
config.logger = logger;
config.agent.event_scheduler = event_scheduler;
config.service = "cpp-parametric-test";
config.environment = "staging";
config.name = "http.request";

auto finalized_config = datadog::tracing::finalize_config(config);
Expand Down
11 changes: 8 additions & 3 deletions test/test_tracer_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,21 @@ class SomewhatSecureTemporaryFile : public std::fstream {
TEST_CASE("TracerConfig::defaults") {
TracerConfig config;

SECTION("service is required") {
SECTION("service is not required") {
SECTION("empty") {
auto finalized = finalize_config(config);
REQUIRE(!finalized);
REQUIRE(finalized.error().code == Error::SERVICE_NAME_REQUIRED);
REQUIRE(finalized);
#ifdef _MSC_VER
REQUIRE(finalized->defaults.service == "tests.exe");
#else
REQUIRE(finalized->defaults.service == "tests");
#endif
}
SECTION("nonempty") {
config.service = "testsvc";
auto finalized = finalize_config(config);
REQUIRE(finalized);
REQUIRE(finalized->defaults.service == "testsvc");
}
}

Expand Down
Loading