Skip to content

Commit 37391dc

Browse files
authored
Fix unix datagram forwarder in metrics-exporter-dogstatsd (#569)
1 parent 2a960ec commit 37391dc

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

metrics-exporter-dogstatsd/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ mod tests {
468468
);
469469
}
470470

471-
#[cfg(target_os = "linux")]
471+
#[cfg(unix)]
472472
mod linux {
473473
use super::*;
474474

metrics-exporter-dogstatsd/src/forwarder/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(target_os = "linux")]
1+
#[cfg(unix)]
22
use std::path::PathBuf;
33
use std::{
44
fmt,
@@ -12,10 +12,10 @@ pub mod sync;
1212
pub(crate) enum RemoteAddr {
1313
Udp(Vec<SocketAddr>),
1414

15-
#[cfg(target_os = "linux")]
15+
#[cfg(unix)]
1616
Unixgram(PathBuf),
1717

18-
#[cfg(target_os = "linux")]
18+
#[cfg(unix)]
1919
Unix(PathBuf),
2020
}
2121

@@ -27,17 +27,17 @@ impl RemoteAddr {
2727
pub const fn transport_id(&self) -> &'static str {
2828
match self {
2929
RemoteAddr::Udp(_) => "udp",
30-
#[cfg(target_os = "linux")]
30+
#[cfg(unix)]
3131
RemoteAddr::Unix(_) => "uds-stream",
32-
#[cfg(target_os = "linux")]
32+
#[cfg(unix)]
3333
RemoteAddr::Unixgram(_) => "uds",
3434
}
3535
}
3636

3737
pub(crate) fn default_max_payload_len(&self) -> usize {
3838
match self {
3939
RemoteAddr::Udp(_) => 1432,
40-
#[cfg(target_os = "linux")]
40+
#[cfg(unix)]
4141
RemoteAddr::Unix(_) | RemoteAddr::Unixgram(_) => 8192,
4242
}
4343
}
@@ -50,9 +50,9 @@ impl<'a> TryFrom<&'a str> for RemoteAddr {
5050
// Try treating the address as a fully-qualified URL, where the scheme is the transport identifier.
5151
if let Some((scheme, path)) = addr.split_once("://") {
5252
return match scheme {
53-
#[cfg(target_os = "linux")]
53+
#[cfg(unix)]
5454
"unix" => Ok(RemoteAddr::Unix(PathBuf::from(path))),
55-
#[cfg(target_os = "linux")]
55+
#[cfg(unix)]
5656
"unixgram" => Ok(RemoteAddr::Unixgram(PathBuf::from(path))),
5757
"udp" => match path.to_socket_addrs() {
5858
Ok(addr) => Ok(RemoteAddr::Udp(addr.collect())),
@@ -89,7 +89,7 @@ impl fmt::Display for RemoteAddr {
8989
write!(f, "]")
9090
}
9191
}
92-
#[cfg(target_os = "linux")]
92+
#[cfg(unix)]
9393
RemoteAddr::Unix(path) | RemoteAddr::Unixgram(path) => {
9494
write!(f, "unixgram://{}", path.display())
9595
}
@@ -115,9 +115,9 @@ impl ForwarderConfiguration {
115115
pub fn is_length_prefixed(&self) -> bool {
116116
match self.remote_addr {
117117
RemoteAddr::Udp(_) => false,
118-
#[cfg(target_os = "linux")]
118+
#[cfg(unix)]
119119
RemoteAddr::Unix(_) => true,
120-
#[cfg(target_os = "linux")]
120+
#[cfg(unix)]
121121
RemoteAddr::Unixgram(_) => true,
122122
}
123123
}
@@ -149,7 +149,7 @@ mod tests {
149149
assert_eq!(addr, Err(unknown_scheme_error_str("spongebob")));
150150
}
151151

152-
#[cfg(target_os = "linux")]
152+
#[cfg(unix)]
153153
mod linux {
154154
#[test]
155155
fn remote_addr_scheme_unix() {

metrics-exporter-dogstatsd/src/forwarder/sync.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(target_os = "linux")]
1+
#[cfg(unix)]
22
use std::os::unix::net::{UnixDatagram, UnixStream};
33
use std::{
44
io::{self, Write as _},
@@ -7,7 +7,6 @@ use std::{
77
thread::sleep,
88
time::Instant,
99
};
10-
1110
use tracing::{debug, error, trace};
1211

1312
use super::{ForwarderConfiguration, RemoteAddr};
@@ -20,10 +19,10 @@ use crate::{
2019
enum Client {
2120
Udp(UdpSocket),
2221

23-
#[cfg(target_os = "linux")]
22+
#[cfg(unix)]
2423
Unixgram(UnixDatagram),
2524

26-
#[cfg(target_os = "linux")]
25+
#[cfg(unix)]
2726
Unix(UnixStream),
2827
}
2928

@@ -38,13 +37,14 @@ impl Client {
3837
})
3938
}
4039

41-
#[cfg(target_os = "linux")]
42-
RemoteAddr::Unixgram(path) => UnixDatagram::bind(path).and_then(|socket| {
40+
#[cfg(unix)]
41+
RemoteAddr::Unixgram(path) => UnixDatagram::unbound().and_then(|socket| {
42+
socket.connect(path)?;
4343
socket.set_write_timeout(Some(config.write_timeout))?;
4444
Ok(Client::Unixgram(socket))
4545
}),
4646

47-
#[cfg(target_os = "linux")]
47+
#[cfg(unix)]
4848
RemoteAddr::Unix(path) => UnixStream::connect(path).and_then(|socket| {
4949
socket.set_write_timeout(Some(config.write_timeout))?;
5050
Ok(Client::Unix(socket))
@@ -56,10 +56,10 @@ impl Client {
5656
match self {
5757
Client::Udp(socket) => socket.send(buf),
5858

59-
#[cfg(target_os = "linux")]
59+
#[cfg(unix)]
6060
Client::Unixgram(socket) => socket.send(buf),
6161

62-
#[cfg(target_os = "linux")]
62+
#[cfg(unix)]
6363
Client::Unix(socket) => match socket.write_all(buf) {
6464
Ok(()) => Ok(buf.len()),
6565
Err(e) => Err(e),

0 commit comments

Comments
 (0)