Skip to content

Commit

Permalink
Publish file descriptor stats to statsd (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanneary authored Nov 28, 2024
1 parent 91104e1 commit 6ef3dfd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
71 changes: 55 additions & 16 deletions data-plane/src/stats_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cadence::{BufferedUdpMetricSink, QueuingMetricSink};
use cadence_macros::{set_global_default, statsd_count, statsd_gauge};
use shared::stats::StatsError;
use shared::{publish_count, publish_count_dynamic_label, publish_gauge, ENCLAVE_STATSD_PORT};
use std::fs;
use std::net::UdpSocket;

use crate::EnclaveContext;
Expand Down Expand Up @@ -31,6 +32,26 @@ impl StatsClient {
}
}

fn get_file_descriptor_info() -> Result<(u64, u64, u64), StatsError> {
let content = fs::read_to_string("/proc/sys/fs/file-nr")?;
let parts: Vec<&str> = content.split_whitespace().collect();

if parts.len() == 3 {
let allocated: u64 = parts[0]
.parse()
.map_err(|_| StatsError::FDUsageParseError)?;
let free: u64 = parts[1]
.parse()
.map_err(|_| StatsError::FDUsageParseError)?;
let max: u64 = parts[2]
.parse()
.map_err(|_| StatsError::FDUsageParseError)?;
Ok((allocated, free, max))
} else {
Err(StatsError::FDUsageReadError)
}
}

pub fn record_encrypt() {
if let Ok(context) = EnclaveContext::get() {
publish_count!("evervault.enclaves.encrypt.count", 1, context);
Expand All @@ -52,24 +73,42 @@ impl StatsClient {
}

pub fn try_record_system_metrics() -> Result<(), StatsError> {
let mem_info = sys_info::mem_info()?;
let cpu = sys_info::loadavg()?;
let cpu_num = sys_info::cpu_num()?;
let mem_info =
sys_info::mem_info().map_err(|e| log::error!("Couldn't obtain mem info: {e}"));
let cpu = sys_info::loadavg().map_err(|e| log::error!("Couldn't obtain cpu info: {e}"));
let cpu_num = sys_info::cpu_num().map_err(|e| log::error!("Couldn't obtain cpu num: {e}"));
let fd_info = Self::get_file_descriptor_info()
.map_err(|e| log::error!("Couldn't obtain fd info: {e}"));

if let Ok(context) = EnclaveContext::get() {
publish_gauge!(
"evervault.enclaves.memory.total",
mem_info.total as f64,
context
);
publish_gauge!(
"evervault.enclaves.memory.avail",
mem_info.avail as f64,
context
);
publish_gauge!("evervault.enclaves.cpu.cores", cpu_num as f64, context);
publish_gauge!("evervault.enclaves.cpu.one", cpu.one, context);
};
if let Ok(mem_info) = mem_info {
publish_gauge!(
"evervault.enclaves.memory.total",
mem_info.total as f64,
context
);
publish_gauge!(
"evervault.enclaves.memory.avail",
mem_info.avail as f64,
context
);
}

if let Ok(cpu_num) = cpu_num {
publish_gauge!("evervault.enclaves.cpu.cores", cpu_num as f64, context);
}

if let Ok(cpu) = cpu {
publish_gauge!("evervault.enclaves.cpu.one", cpu.one, context);
}

if let Ok((allocated, free, max)) = fd_info {
publish_gauge!("evervault.enclaves.fd.allocated", allocated, context);
publish_gauge!("evervault.enclaves.fd.free", free, context);
publish_gauge!("evervault.enclaves.fd.max", max, context);
}
}

Ok(())
}
}
9 changes: 9 additions & 0 deletions e2e-tests/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ describe("Enclave is runnning", () => {
expect(keys).to.include(
"evervault.enclaves.cpu.cores;enclave_uuid=enclave_123;app_uuid=app_12345678"
);
expect(keys).to.include(
"evervault.enclaves.fd.allocated;enclave_uuid=enclave_123;app_uuid=app_12345678"
);
expect(keys).to.include(
"evervault.enclaves.fd.max;enclave_uuid=enclave_123;app_uuid=app_12345678"
);
expect(keys).to.include(
"evervault.enclaves.fd.free;enclave_uuid=enclave_123;app_uuid=app_12345678"
);
} finally {
sysClient.destroy();
done();
Expand Down
4 changes: 4 additions & 0 deletions shared/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub enum StatsError {
MetricError(#[from] MetricError),
#[error("IO error {0}")]
IOError(#[from] Error),
#[error("Couldn't parse file descriptor values info from /proc/sys/fs/file-nr")]
FDUsageParseError,
#[error("Couldn't read file descriptor info from /proc/sys/fs/file-nr")]
FDUsageReadError,
}

#[macro_export]
Expand Down

0 comments on commit 6ef3dfd

Please sign in to comment.