Skip to content

Commit

Permalink
add more anyhow errors for easier debugging building dogstatsd client
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgajg1134 committed Sep 24, 2024
1 parent cafd312 commit 598852f
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions dogstatsd-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,17 @@ fn create_client(endpoint: &Endpoint) -> anyhow::Result<StatsdClient> {
match endpoint.url.scheme_str() {
#[cfg(unix)]
Some("unix") => {
let socket = UnixDatagram::unbound()?;
socket.set_nonblocking(true)?;
let socket = UnixDatagram::unbound()
.map_err(|e| anyhow!("failed to make unbound unix port: {}", e))?;
socket
.set_nonblocking(true)
.map_err(|e| anyhow!("failed to set socket to nonblocking: {}", e))?;
let sink = QueuingMetricSink::with_capacity(
UnixMetricSink::from(socket_path_from_uri(&endpoint.url)?, socket),
UnixMetricSink::from(
socket_path_from_uri(&endpoint.url)
.map_err(|e| anyhow!("failed to build socket path from uri: {}", e))?,
socket,
),
QUEUE_SIZE,
);

Expand All @@ -200,14 +207,16 @@ fn create_client(endpoint: &Endpoint) -> anyhow::Result<StatsdClient> {
.ok_or(anyhow!("invalid address"))?;

let socket = if server_address.is_ipv4() {
UdpSocket::bind("0.0.0.0:0")?
UdpSocket::bind("0.0.0.0:0")
.map_err(|e| anyhow!("failed to bind to 0.0.0.0:0: {}", e))?
} else {
UdpSocket::bind("[::]:0")?
UdpSocket::bind("[::]:0").map_err(|e| anyhow!("failed to bind to [::]:0: {}", e))?
};
socket.set_nonblocking(true)?;

let sink = QueuingMetricSink::with_capacity(
UdpMetricSink::from((host, port), socket)?,
UdpMetricSink::from((host, port), socket)
.map_err(|e| anyhow!("failed to build UdpMetricSink: {}", e))?,
QUEUE_SIZE,
);

Expand Down Expand Up @@ -290,7 +299,10 @@ mod test {
"unix://localhost:80".parse::<Uri>().unwrap(),
));
assert!(res.is_err());
assert_eq!("invalid url", res.unwrap_err().to_string().as_str());
assert_eq!(
"failed to build socket path from uri: invalid url",
res.unwrap_err().to_string().as_str()
);

let res = create_client(&Endpoint::from_url(
socket_path_to_uri("/path/to/a/socket.sock".as_ref()).unwrap(),
Expand Down

0 comments on commit 598852f

Please sign in to comment.