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