feldera_observability/system.rs
1//! System introspection utilities.
2
3/// True when this process runs inside a Kubernetes pod.
4///
5/// Kubernetes injects `KUBERNETES_SERVICE_HOST` into every pod, and the
6/// pipeline manager rejects user-supplied `KUBERNETES_*` variables in the
7/// pipeline's `env` config.
8pub fn running_in_kubernetes() -> bool {
9 std::env::var_os("KUBERNETES_SERVICE_HOST").is_some()
10}
11
12/// Returns the memory, in megabytes (MB), of the current system, or `None`
13/// when the operating system does not report it.
14///
15/// This is the min of the host's available memory and the cgroup budget
16/// (what a container or Kubernetes pod memory limit sets).
17pub fn total_memory_megabyte() -> Option<u64> {
18 let mut system = sysinfo::System::new();
19 system.refresh_memory();
20 let mut available = system.total_memory();
21 if let Some(limits) = system.cgroup_limits() {
22 available = available.min(limits.total_memory);
23 }
24 let mb = available / 1_000_000;
25 // `sysinfo` reports 0 on platforms it does not support.
26 if mb > 0 { Some(mb) } else { None }
27}
28
29#[cfg(test)]
30mod tests {
31 /// Guards the `sysinfo` call sequence (`cgroup_limits` panics when memory
32 /// was not refreshed first).
33 #[cfg(target_os = "linux")]
34 #[test]
35 fn reports_nonzero_memory() {
36 let mb = super::total_memory_megabyte().expect("Linux reports available memory");
37 assert!(mb > 0, "available memory must be positive, got {mb} MB");
38 }
39}