Lines
100 %
Functions
Branches
//! SSH session series and the optional `/metrics` listener.
use server::telemetry::recorder::{
PrometheusHandle, SSH_SESSIONS_ACTIVE, SSH_SESSIONS_TOTAL, serve_metrics,
};
use std::io;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tokio::task::JoinHandle;
/// Counts one open session; dropping it, on any close path, takes it back off the gauge.
pub struct SessionGauge;
impl SessionGauge {
pub fn open() -> Self {
metrics::counter!(SSH_SESSIONS_TOTAL).increment(1);
metrics::gauge!(SSH_SESSIONS_ACTIVE).increment(1.0);
Self
}
impl Drop for SessionGauge {
fn drop(&mut self) {
metrics::gauge!(SSH_SESSIONS_ACTIVE).decrement(1.0);
/// Obtains the recorder handle via `install` and serves `/metrics` on `bind`;
/// `None` installs and opens nothing.
///
/// # Errors
/// The recorder cannot be installed or `bind` cannot be bound.
pub async fn start(
bind: Option<SocketAddr>,
install: impl FnOnce() -> io::Result<PrometheusHandle>,
) -> io::Result<Option<(SocketAddr, JoinHandle<()>)>> {
let Some(bind) = bind else {
return Ok(None);
let handle = install()?;
let listener = TcpListener::bind(bind).await?;
let addr = listener.local_addr()?;
tracing::info!("nomisync-sshd metrics on {addr}");
Ok(Some((addr, tokio::spawn(serve_metrics(listener, handle)))))