-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#[cfg(feature = "statsd")] | ||
use cadence::{CountedExt, Gauged, StatsdClient}; | ||
#[cfg(feature = "statsd")] | ||
use std::sync::atomic::{AtomicU64, Ordering}; | ||
|
||
/// Queue with a maximum capacity of 8K events. | ||
/// This program is extremely unlikely to ever reach that upper bound. | ||
/// The bound is still here so that if it ever were to happen, we drop events | ||
/// instead of indefinitely filling the memory with unsent events. | ||
const QUEUE_SIZE: usize = 8 * 1024; | ||
Check failure on line 10 in src/statsd.rs GitHub Actions / clippyconstant `QUEUE_SIZE` is never used
|
||
|
||
const PREFIX: &str = "tcp2udp"; | ||
|
||
#[cfg(feature = "statsd")] | ||
pub struct StatsdMetrics(Option<StatsdMetricsImpl>); | ||
#[cfg(not(feature = "statsd"))] | ||
pub struct StatsdMetrics(Option<()>); | ||
|
||
impl StatsdMetrics { | ||
/// Creates a dummy statsd metrics instance. Does not actually connect to any statds | ||
/// server, nor emits any events. Used as an API compatible drop in when metrics | ||
/// should not be emitted. | ||
pub fn dummy() -> Self { | ||
Self(None) | ||
} | ||
|
||
/// Creates a statsd metric reporting instance connecting to the given host addr. | ||
#[cfg(feature = "statsd")] | ||
pub fn real(host: std::net::SocketAddr) -> Self { | ||
Self(Some(StatsdMetricsImpl::new(host))) | ||
} | ||
|
||
/// Emit a metric saying we failed to accept an incoming TCP connection (probably ran out of file descriptors) | ||
pub fn accept_error(&self) { | ||
#[cfg(feature = "statsd")] | ||
if let Some(statsd) = &self.0 { | ||
statsd.accept_error() | ||
} | ||
} | ||
|
||
/// Increment the connection counter inside this metrics instance and emit that new gauge value | ||
pub fn incr_connections(&self) { | ||
#[cfg(feature = "statsd")] | ||
if let Some(statsd) = &self.0 { | ||
statsd.incr_connections() | ||
} | ||
} | ||
|
||
/// Decrement the connection counter inside this metrics instance and emit that new gauge value | ||
pub fn decr_connections(&self) { | ||
#[cfg(feature = "statsd")] | ||
if let Some(statsd) = &self.0 { | ||
statsd.decr_connections() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "statsd")] | ||
struct StatsdMetricsImpl { | ||
client: StatsdClient, | ||
num_connections: AtomicU64, | ||
} | ||
|
||
#[cfg(feature = "statsd")] | ||
impl StatsdMetricsImpl { | ||
pub fn new(host: std::net::SocketAddr) -> Self { | ||
use cadence::{UdpMetricSink, QueuingMetricSink}; | ||
|
||
let socket = std::net::UdpSocket::bind("0.0.0.0:0").unwrap(); | ||
log::debug!("Statsd socket bound to {}", socket.local_addr().unwrap()); | ||
|
||
// Create a non-buffered blocking metrics sink. It is important that it's not buffered, | ||
// so events are emitted instantly when they happen (this program does not emit a lot of | ||
// events, nor does it attach timestamps to the events. | ||
// The fact that it's blocking does not matter, since the `QueuingMetricSink` will make sure | ||
// the `UdpMetricSink` runs in its own thread anyway. | ||
let udp_sink = UdpMetricSink::from(host, socket).unwrap(); | ||
let queuing_sink = QueuingMetricSink::with_capacity(udp_sink, QUEUE_SIZE); | ||
let statds_client = StatsdClient::from_sink(PREFIX, queuing_sink); | ||
Self { | ||
client: statds_client, | ||
num_connections: AtomicU64::new(0), | ||
} | ||
} | ||
|
||
pub fn accept_error(&self) { | ||
log::debug!("Sending statsd tcp_accept_errors"); | ||
if let Err(e) = self.client.incr("tcp_accept_errors") { | ||
log::error!("Failed to emit statsd tcp_accept_errors: {e}"); | ||
} | ||
} | ||
|
||
pub fn incr_connections(&self) { | ||
let num_connections = self.num_connections.fetch_add(1, Ordering::SeqCst) + 1; | ||
log::debug!("Sending statsd num_connections = {num_connections}"); | ||
if let Err(e) = self.client.gauge("num_connections", num_connections) { | ||
log::error!("Failed to emit statsd num_connections: {e}"); | ||
} | ||
} | ||
|
||
pub fn decr_connections(&self) { | ||
let num_connections = self.num_connections.fetch_sub(1, Ordering::SeqCst) - 1; | ||
log::debug!("Sending statsd num_connections = {num_connections}"); | ||
if let Err(e) = self.client.gauge("num_connections", num_connections) { | ||
log::error!("Failed to emit statsd num_connections: {e}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters