Skip to content

Commit

Permalink
create datadog from socket
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseifedotov committed Aug 2, 2023
1 parent 0893c37 commit 86ee106
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,31 @@ impl Client {
})
}

/// Create a new client from existing UDP socket.
///
/// # Examples
///
/// ```
/// use std::net::UdpSocket;
/// use dogstatsd::{Client};
///
/// // Instead of constructing a new socket, you can use an existing one
/// // passed from another library or parent process.
///
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
/// socket.connect("127.0.0.1:8125").expect("couldn't connect to address");
///
/// let client = Client::from_socket(socket, String::from("mynamespace"), vec![]);
/// ```

pub fn from_socket(socket: UdpSocket, namespace: String, default_tags: Vec<u8>) -> Self {
Client {
socket,
namespace,
default_tags
}
}

/// Increment a StatsD counter
///
/// # Examples
Expand Down Expand Up @@ -790,6 +815,21 @@ mod tests {
)
.unwrap();
}

#[test]
fn test_from_socket() {
let socket = UdpSocket::bind("127.0.0.1:9001").expect("couldn't bind to address");
socket.connect("127.0.0.1:8125").expect("couldn't connect to address");

let client = Client::from_socket(socket, String::new(), vec!());
// Shouldn't panic or error
client
.send(
&GaugeMetric::new("gauge".into(), "1234".into()),
&["tag1", "tag2"],
)
.unwrap();
}
}

#[cfg(all(feature = "unstable", test))]
Expand Down

0 comments on commit 86ee106

Please sign in to comment.