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

Add IPv6 support to DD_AGENT_HOST #1707

Merged
merged 4 commits into from
Feb 16, 2023
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
9 changes: 6 additions & 3 deletions ext/coms.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ static ddtrace_coms_stack_t *_dd_coms_attempt_acquire_stack(void) {
}

#define TRACE_PATH_STR "/v0.4/traces"
#define HOST_FORMAT_STR "http://%s:%u"
#define HOST_V6_FORMAT_STR "http://[%s]:%u"
#define HOST_V4_FORMAT_STR "http://%s:%u"
#define DEFAULT_UDS_PATH "/var/run/datadog/apm.socket"

static struct curl_slist *dd_agent_curl_headers = NULL;
Expand Down Expand Up @@ -729,12 +730,14 @@ char *ddtrace_agent_url(void) {
}

if (ZSTR_LEN(hostname) > 0) {
bool isIPv6 = memchr(ZSTR_VAL(hostname), ':', ZSTR_LEN(hostname));

int64_t port = get_global_DD_TRACE_AGENT_PORT();
if (port <= 0 || port > 65535) {
port = 8126;
}
char *formatted_url;
asprintf(&formatted_url, HOST_FORMAT_STR, ZSTR_VAL(hostname), (uint32_t)port);
asprintf(&formatted_url, isIPv6 ? HOST_V6_FORMAT_STR : HOST_V4_FORMAT_STR, ZSTR_VAL(hostname), (uint32_t)port);
return formatted_url;
}

Expand All @@ -747,7 +750,7 @@ char *ddtrace_agent_url(void) {
port = 8126;
}
char *formatted_url;
asprintf(&formatted_url, HOST_FORMAT_STR, "localhost", (uint32_t)port);
asprintf(&formatted_url, HOST_V4_FORMAT_STR, "localhost", (uint32_t)port);
return formatted_url;
}

Expand Down
69 changes: 68 additions & 1 deletion profiling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,13 @@ fn detect_uri_from_config(
if port.is_some() || host.is_some() {
let host = host.unwrap_or(Cow::Borrowed("localhost"));
let port = port.unwrap_or(8126u16);
let url = if host.contains(':') {
format!("http://[{host}]:{port}")
} else {
format!("http://{host}:{port}")
};

match Uri::from_str(format!("http://{host}:{port}").as_str()) {
match Uri::from_str(url.as_str()) {
Ok(uri) => return AgentEndpoint::Uri(uri),
Err(err) => {
warn!("The combination of DD_AGENT_HOST({host}) and DD_TRACE_AGENT_PORT({port}) was not a valid URL: {err}")
Expand Down Expand Up @@ -1181,3 +1186,65 @@ fn is_zend_mm() -> bool {
unsafe { zend::is_zend_mm() }
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn detect_uri_from_config_works() {
// expected
let endpoint = detect_uri_from_config(
None,
None,
None
);
let expected = AgentEndpoint::default();
assert_eq!(endpoint, expected);

// ipv4 host
let endpoint = detect_uri_from_config(
None,
Some(Cow::Owned("127.0.0.1".to_owned())),
None
);
let expected = AgentEndpoint::Uri(Uri::from_static("http://127.0.0.1:8126"));
assert_eq!(endpoint, expected);

// ipv6 host
let endpoint = detect_uri_from_config(
None,
Some(Cow::Owned("::1".to_owned())),
None
);
let expected = AgentEndpoint::Uri(Uri::from_static("http://[::1]:8126"));
assert_eq!(endpoint, expected);

// ipv6 host, custom port
let endpoint = detect_uri_from_config(
None,
Some(Cow::Owned("::1".to_owned())),
Some(9000),
);
let expected = AgentEndpoint::Uri(Uri::from_static("http://[::1]:9000"));
assert_eq!(endpoint, expected);

// agent_url
let endpoint = detect_uri_from_config(
Some(Cow::Owned("http://[::1]:8126".to_owned())),
None,
None,
);
let expected = AgentEndpoint::Uri(Uri::from_static("http://[::1]:8126"));
assert_eq!(endpoint, expected);

// fallback on non existing UDS
let endpoint = detect_uri_from_config(
Some(Cow::Owned("unix://foo/bar/baz/I/do/not/exist".to_owned())),
None,
None,
);
let expected = AgentEndpoint::default();
assert_eq!(endpoint, expected);
}
}
18 changes: 18 additions & 0 deletions tests/ext/background-sender/background_sender_ipv6_support.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
DD_AGENT_HOST with IPv6 works
--SKIPIF--
<?php include __DIR__ . '/../startup_logging_skipif.inc'; ?>
--ENV--
DD_AGENT_HOST=::1
--FILE--
<?php
include_once __DIR__ . '/../startup_logging.inc';

$logs = dd_get_startup_logs([], ['DD_TRACE_DEBUG=1']);

dd_dump_startup_logs($logs, [
'agent_url',
]);
?>
--EXPECT--
agent_url: "http://[::1]:8126"