-
Notifications
You must be signed in to change notification settings - Fork 5
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
Enable UDS settings #233
Enable UDS settings #233
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,22 @@ static const int k_size_api_key = 32; | |
|
||
static char *alloc_url_agent(const char *protocol, const char *host, | ||
const char *port) { | ||
size_t expected_size = snprintf(NULL, 0, "%s%s:%s", protocol, host, port); | ||
char *url = (char *)malloc(expected_size + 1); | ||
if (!url) // Early exit on alloc failure | ||
return NULL; | ||
|
||
snprintf(url, expected_size + 1, "%s%s:%s", protocol, host, port); | ||
return url; | ||
if (port) { | ||
size_t expected_size = snprintf(NULL, 0, "%s%s:%s", protocol, host, port); | ||
char *url = (char *)malloc(expected_size + 1); | ||
if (!url) // Early exit on alloc failure | ||
return NULL; | ||
|
||
snprintf(url, expected_size + 1, "%s%s:%s", protocol, host, port); | ||
return url; | ||
} else { | ||
size_t expected_size = snprintf(NULL, 0, "%s%s", protocol, host); | ||
char *url = (char *)malloc(expected_size + 1); | ||
if (!url) // Early exit on alloc failure | ||
return NULL; | ||
snprintf(url, expected_size + 1, "%s%s", protocol, host); | ||
return url; | ||
} | ||
} | ||
|
||
static DDRes create_pprof_file(ddog_Timespec start, | ||
|
@@ -73,6 +82,21 @@ static DDRes write_pprof_file(const ddog_EncodedProfile *encoded_profile, | |
return {}; | ||
} | ||
|
||
bool contains_port(const char *url) { | ||
const char *port_ptr = strrchr(url, ':'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making a completely irrelevant comment. Strictly speaking, I don't know whether |
||
if (port_ptr != NULL) { | ||
// Check if the characters after the ':' are digits | ||
for (const char *p = port_ptr + 1; *p != '\0'; p++) { | ||
if (!isdigit(*p)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
DDRes ddprof_exporter_init(const ExporterInput *exporter_input, | ||
DDProfExporter *exporter) { | ||
memset(exporter, 0, sizeof(DDProfExporter)); | ||
|
@@ -91,20 +115,43 @@ DDRes ddprof_exporter_init(const ExporterInput *exporter_input, | |
} | ||
|
||
if (exporter->_agent) { | ||
exporter->_url = | ||
alloc_url_agent("http://", exporter_input->host, exporter_input->port); | ||
const char *port_str = exporter_input->port; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be probably be extracted to its own function.
That way you can even stop distinguishing between agent and agentless. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good idea, though we would need to check if endpoint is reachable on startup, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure to understand, why would this be permissive ? |
||
|
||
if (exporter_input->url) { | ||
// uds -> no port | ||
if (!strncasecmp(exporter_input->url, "unix", 4)) { | ||
port_str = nullptr; | ||
} | ||
// already port -> no port | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, agreed |
||
else if (contains_port(exporter_input->url)) { | ||
port_str = nullptr; | ||
} | ||
// check if schema is already available | ||
if (strstr(exporter_input->url, "://") != NULL) { | ||
exporter->_url = alloc_url_agent("", exporter_input->url, port_str); | ||
} else { | ||
// not available, assume http | ||
exporter->_url = | ||
alloc_url_agent("http://", exporter_input->url, port_str); | ||
} | ||
} else { | ||
// no url, use default host and port settings | ||
exporter->_url = alloc_url_agent("http://", exporter_input->host, | ||
exporter_input->port); | ||
} | ||
} else { | ||
// site is the usual option for intake | ||
if (exporter->_input.site) { | ||
// agentless mode | ||
if (exporter->_input.url) { | ||
// warning : should not contain intake.profile. (prepended in | ||
// libdatadog_profiling) | ||
exporter->_url = strdup(exporter_input->site); | ||
exporter->_url = strdup(exporter_input->url); | ||
} else { | ||
LG_WRN( | ||
"[EXPORTER] Agentless - Attempting to use host (%s) instead of empty " | ||
"site", | ||
"url", | ||
exporter_input->host); | ||
exporter->_url = strdup(exporter_input->host); | ||
exporter->_url = alloc_url_agent("http://", exporter_input->host, | ||
exporter_input->port); | ||
} | ||
} | ||
if (!exporter->_url) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,7 @@ void fill_mock_exporter_input(ExporterInput &exporter_input, | |
exporter_input.agentless = "yes"; | ||
exporter_input.environment = "unit-test"; | ||
exporter_input.host = url.first.c_str(); | ||
exporter_input.site = "datadog_is_cool.com"; | ||
exporter_input.url = "datadog_is_cool.com"; | ||
exporter_input.port = url.second.c_str(); | ||
exporter_input.service = MYNAME; | ||
exporter_input.service_version = "42"; | ||
|
@@ -104,20 +104,28 @@ TEST(DDProfExporter, url) { | |
EXPECT_EQ(strcmp(exporter._url, "datadog_is_cool.com"), 0); | ||
ddprof_exporter_free(&exporter); | ||
|
||
// Default to host if site not found | ||
// To be discussed : should we fail here ? | ||
exporter_input.site = nullptr; | ||
exporter_input.url = nullptr; | ||
res = ddprof_exporter_init(&exporter_input, &exporter); | ||
EXPECT_TRUE(IsDDResOK(res)); | ||
EXPECT_EQ(strcmp(exporter._url, "25.04.1988.0"), 0); | ||
EXPECT_EQ(strcmp(exporter._url, "http://25.04.1988.0:1234"), 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering that the previous test uses a hostname without an explicit protocol, I think it's fine to convert this to a protocol-bearing statement. |
||
ddprof_exporter_free(&exporter); | ||
|
||
// If no API key --> expect host | ||
fill_mock_exporter_input(exporter_input, url, false); | ||
exporter_input.url = nullptr; | ||
res = ddprof_exporter_init(&exporter_input, &exporter); | ||
EXPECT_TRUE(IsDDResOK(res)); | ||
EXPECT_EQ(strcmp(exporter._url, "http://25.04.1988.0:1234"), 0); | ||
ddprof_exporter_free(&exporter); | ||
|
||
// UDS --> expect UDS | ||
fill_mock_exporter_input(exporter_input, url, false); | ||
exporter_input.url = "unix://some/uds/socket.sock"; | ||
res = ddprof_exporter_init(&exporter_input, &exporter); | ||
EXPECT_TRUE(IsDDResOK(res)); | ||
EXPECT_EQ(strcmp(exporter._url, "unix://some/uds/socket.sock"), 0); | ||
ddprof_exporter_free(&exporter); | ||
} | ||
|
||
TEST(DDProfExporter, simple) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, makes sense to move this 👍