Skip to content

Commit 89426d5

Browse files
feat!: Set a default service name (DD_SERVICE) when no service is configured (#158)
* feat!: Implement consistency for DD_SERVICE - Set a default service name value when none is provided by DD_SERVICE or configuration Issues: APMAPI-709 * test: Remove the parametric app's default service and environment configuration so we can test default behaviors Issues: APMAPI-708 , APMAPI-709 * Apply suggestions from code review Co-authored-by: Damien Mehala <[email protected]> * Fix formatting after incorporating code review feedback --------- Co-authored-by: Damien Mehala <[email protected]>
1 parent 1bcd5b1 commit 89426d5

File tree

7 files changed

+36
-8
lines changed

7 files changed

+36
-8
lines changed

doc/maintainers.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ david@ein:~/src/dd-trace-cpp/src/datadog$ sed -n 's/^\s*\(\w\+\)\s*=\s*[0-9]\+,\
385385
1 SPAN_SAMPLING_RULES_MAX_PER_SECOND_WRONG_TYPE
386386
1 SPAN_SAMPLING_RULES_INVALID_JSON
387387
1 SPAN_SAMPLING_RULES_FILE_IO
388-
1 SERVICE_NAME_REQUIRED
389388
1 RULE_WRONG_TYPE
390389
1 RULE_TAG_WRONG_TYPE
391390
1 RULE_PROPERTY_WRONG_TYPE

include/datadog/error.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct Error {
2424
// versions.
2525
enum Code {
2626
OTHER = 1,
27-
SERVICE_NAME_REQUIRED = 2,
27+
// SERVICE_NAME_REQUIRED = 2,
2828
MESSAGEPACK_ENCODE_FAILURE = 3,
2929
CURL_REQUEST_FAILURE = 4,
3030
DATADOG_AGENT_NULL_HTTP_CLIENT = 5,

src/datadog/platform_util.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ int get_process_id() {
189189
#endif
190190
}
191191

192+
std::string get_process_name() {
193+
#if defined(__APPLE__) || defined(__FreeBSD__)
194+
char* process_name = getprogname();
195+
return (process_name != nullptr) ? process_name : "unknown-service";
196+
#elif defined(__linux__) || defined(__unix__)
197+
return program_invocation_short_name;
198+
#elif defined(_MSC_VER)
199+
TCHAR exe_name[MAX_PATH];
200+
if (GetModuleFileName(NULL, exe_name, MAX_PATH) <= 0) {
201+
return "unknown-service";
202+
}
203+
#ifdef UNICODE
204+
std::wstring wStr(exe_name);
205+
std::string path = std::string(wStr.begin(), wStr.end());
206+
#else
207+
std::string path = std::string(exe_name);
208+
#endif
209+
return path.substr(path.find_last_of("/\\") + 1);
210+
#else
211+
return "unknown-service";
212+
#endif
213+
}
214+
192215
int at_fork_in_child(void (*on_fork)()) {
193216
#if defined(_MSC_VER)
194217
// Windows does not have `fork`, and so this is not relevant there.

src/datadog/platform_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ std::string get_hostname();
2626

2727
int get_process_id();
2828

29+
std::string get_process_name();
30+
2931
int at_fork_in_child(void (*on_fork)());
3032

3133
} // namespace tracing

src/datadog/tracer_config.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "datadog_agent.h"
1616
#include "json.hpp"
1717
#include "parse_util.h"
18+
#include "platform_util.h"
1819
#include "string_util.h"
1920

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

265266
if (final_config.defaults.service.empty()) {
266-
return Error{Error::SERVICE_NAME_REQUIRED, "Service name is required."};
267+
final_config.defaults.service = get_process_name();
267268
}
268269

269270
final_config.metadata[ConfigName::SERVICE_NAME] = ConfigMetadata(

test/system-tests/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ int main(int argc, char* argv[]) {
7777
datadog::tracing::TracerConfig config;
7878
config.logger = logger;
7979
config.agent.event_scheduler = event_scheduler;
80-
config.service = "cpp-parametric-test";
81-
config.environment = "staging";
8280
config.name = "http.request";
8381

8482
auto finalized_config = datadog::tracing::finalize_config(config);

test/test_tracer_config.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,21 @@ class SomewhatSecureTemporaryFile : public std::fstream {
145145
TEST_CASE("TracerConfig::defaults") {
146146
TracerConfig config;
147147

148-
SECTION("service is required") {
148+
SECTION("service is not required") {
149149
SECTION("empty") {
150150
auto finalized = finalize_config(config);
151-
REQUIRE(!finalized);
152-
REQUIRE(finalized.error().code == Error::SERVICE_NAME_REQUIRED);
151+
REQUIRE(finalized);
152+
#ifdef _MSC_VER
153+
REQUIRE(finalized->defaults.service == "tests.exe");
154+
#else
155+
REQUIRE(finalized->defaults.service == "tests");
156+
#endif
153157
}
154158
SECTION("nonempty") {
155159
config.service = "testsvc";
156160
auto finalized = finalize_config(config);
157161
REQUIRE(finalized);
162+
REQUIRE(finalized->defaults.service == "testsvc");
158163
}
159164
}
160165

0 commit comments

Comments
 (0)