Skip to content

Commit de48e39

Browse files
authored
Merge branch 'master' into dubloom/process-tags
2 parents 7c56e32 + ed3089e commit de48e39

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

profiling/src/config.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,42 @@ impl SystemSettings {
177177
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
178178
pub enum AgentEndpoint {
179179
Uri(Uri),
180-
Socket(PathBuf),
180+
Socket(Cow<'static, Path>),
181+
}
182+
183+
impl AgentEndpoint {
184+
/// This is the "default" path for the Unix domain socket for the agent.
185+
#[cfg(unix)]
186+
pub const DEFAULT_UNIX_SOCKET_PATH: &Path = {
187+
// The unsafe stuff is just because it's in a const context and
188+
// `Path::new` isn't const on stable yet.
189+
//
190+
// SAFETY: `Path` is `repr(transparent)` with `OsStr`, which is
191+
// transparent with a `Slice` type that is transparent with `[u8]` on
192+
// Unix platforms. We can cast from `*const [u8]` to `*const Path`
193+
// using unsized pointer-to-pointer casts and Rust guarantees that the
194+
// length of the metadata is preserved, and this cast is valid because
195+
// of the `repr(transparent)`s. The last piece is ensuring all the
196+
// bytes in the `[u8]` are valid for an `OsStr`, but on Unix the
197+
// implementation of `OsStrExt` for `OsStr` just transmutes the bytes
198+
// from `&[u8]` to `&OsStr`, so all the bytes must be valid.
199+
unsafe { &*("/var/run/datadog/apm.socket".as_bytes() as *const [u8] as *const Path) }
200+
};
201+
202+
/// This is the "default" URI for the agent.
203+
pub const DEFAULT_URI_STR: &str = "http://localhost:8126";
181204
}
182205

183206
impl Default for AgentEndpoint {
184207
/// Returns a socket configuration if the default socket exists, otherwise
185208
/// it returns http://localhost:8126/. This does not consult environment
186209
/// variables.
187210
fn default() -> Self {
188-
let path = Path::new("/var/run/datadog/apm.socket");
211+
let path = AgentEndpoint::DEFAULT_UNIX_SOCKET_PATH;
189212
if path.exists() {
190213
return AgentEndpoint::Socket(path.into());
191214
}
192-
AgentEndpoint::Uri(Uri::from_static("http://localhost:8126"))
215+
AgentEndpoint::Uri(Uri::from_static(AgentEndpoint::DEFAULT_URI_STR))
193216
}
194217
}
195218

@@ -243,7 +266,7 @@ fn detect_uri_from_config(
243266
if let Some(path) = trace_agent_url.strip_prefix("unix://") {
244267
let path = PathBuf::from(path);
245268
if path.exists() {
246-
return AgentEndpoint::Socket(path);
269+
return AgentEndpoint::Socket(Cow::Owned(path));
247270
} else {
248271
warn!(
249272
"Unix socket specified in DD_TRACE_AGENT_URL does not exist: {} ",

0 commit comments

Comments
 (0)