vector/sources/host_metrics/
memory.rs1#[cfg(target_os = "linux")]
2use heim::memory::os::linux::MemoryExt;
3#[cfg(target_os = "macos")]
4use heim::memory::os::macos::MemoryExt;
5#[cfg(not(windows))]
6use heim::memory::os::SwapExt;
7use heim::units::information::byte;
8use vector_lib::event::MetricTags;
9
10use crate::internal_events::HostMetricsScrapeDetailError;
11
12use super::HostMetrics;
13
14impl HostMetrics {
15 pub async fn memory_metrics(&self, output: &mut super::MetricsBuffer) {
16 output.name = "memory";
17 match heim::memory::memory().await {
18 Ok(memory) => {
19 output.gauge(
20 "memory_total_bytes",
21 memory.total().get::<byte>() as f64,
22 MetricTags::default(),
23 );
24 output.gauge(
25 "memory_free_bytes",
26 memory.free().get::<byte>() as f64,
27 MetricTags::default(),
28 );
29 output.gauge(
30 "memory_available_bytes",
31 memory.available().get::<byte>() as f64,
32 MetricTags::default(),
33 );
34 #[cfg(any(target_os = "linux", target_os = "macos"))]
35 output.gauge(
36 "memory_active_bytes",
37 memory.active().get::<byte>() as f64,
38 MetricTags::default(),
39 );
40 #[cfg(target_os = "linux")]
41 output.gauge(
42 "memory_buffers_bytes",
43 memory.buffers().get::<byte>() as f64,
44 MetricTags::default(),
45 );
46 #[cfg(target_os = "linux")]
47 output.gauge(
48 "memory_cached_bytes",
49 memory.cached().get::<byte>() as f64,
50 MetricTags::default(),
51 );
52 #[cfg(target_os = "linux")]
53 output.gauge(
54 "memory_shared_bytes",
55 memory.shared().get::<byte>() as f64,
56 MetricTags::default(),
57 );
58 #[cfg(target_os = "linux")]
59 output.gauge(
60 "memory_used_bytes",
61 memory.used().get::<byte>() as f64,
62 MetricTags::default(),
63 );
64 #[cfg(target_os = "macos")]
65 output.gauge(
66 "memory_inactive_bytes",
67 memory.inactive().get::<byte>() as f64,
68 MetricTags::default(),
69 );
70 #[cfg(target_os = "macos")]
71 output.gauge(
72 "memory_wired_bytes",
73 memory.wire().get::<byte>() as f64,
74 MetricTags::default(),
75 );
76 }
77 Err(error) => {
78 emit!(HostMetricsScrapeDetailError {
79 message: "Failed to load memory info.",
80 error,
81 });
82 }
83 }
84 }
85
86 pub async fn swap_metrics(&self, output: &mut super::MetricsBuffer) {
87 output.name = "memory";
88 match heim::memory::swap().await {
89 Ok(swap) => {
90 output.gauge(
91 "memory_swap_free_bytes",
92 swap.free().get::<byte>() as f64,
93 MetricTags::default(),
94 );
95 output.gauge(
96 "memory_swap_total_bytes",
97 swap.total().get::<byte>() as f64,
98 MetricTags::default(),
99 );
100 output.gauge(
101 "memory_swap_used_bytes",
102 swap.used().get::<byte>() as f64,
103 MetricTags::default(),
104 );
105 #[cfg(not(windows))]
106 output.counter(
107 "memory_swapped_in_bytes_total",
108 swap.sin().map(|swap| swap.get::<byte>()).unwrap_or(0) as f64,
109 MetricTags::default(),
110 );
111 #[cfg(not(windows))]
112 output.counter(
113 "memory_swapped_out_bytes_total",
114 swap.sout().map(|swap| swap.get::<byte>()).unwrap_or(0) as f64,
115 MetricTags::default(),
116 );
117 }
118 Err(error) => {
119 emit!(HostMetricsScrapeDetailError {
120 message: "Failed to load swap info.",
121 error,
122 });
123 }
124 }
125 }
126}