-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(host_metrics source): Implement process collection for host metr…
…ics (#21791) * dev * bump * cleanup * process metrics * dd rust license * fix format * update docs * Update src/sources/host_metrics/process.rs Co-authored-by: Pavlos Rontidis <[email protected]> * Update changelog.d/process_host_metrics.feature.md Co-authored-by: Pavlos Rontidis <[email protected]> * update docs, process test * update docs --------- Co-authored-by: Pavlos Rontidis <[email protected]> Co-authored-by: LeeTeng2001 <users.noreply.github.com>
- Loading branch information
1 parent
6cf6df5
commit 20bf146
Showing
8 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,7 @@ crc-catalog,https://github.com/akhilles/crc-catalog,MIT OR Apache-2.0,Akhil Vela | |
crc32c,https://github.com/zowens/crc32c,Apache-2.0 OR MIT,Zack Owens | ||
crc32fast,https://github.com/srijs/rust-crc32fast,MIT OR Apache-2.0,"Sam Rijs <[email protected]>, Alex Crichton <[email protected]>" | ||
crossbeam-channel,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-channel Authors | ||
crossbeam-deque,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-deque Authors | ||
crossbeam-epoch,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-epoch Authors | ||
crossbeam-queue,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-queue Authors | ||
crossbeam-utils,https://github.com/crossbeam-rs/crossbeam,MIT OR Apache-2.0,The crossbeam-utils Authors | ||
|
@@ -484,6 +485,7 @@ rand_xorshift,https://github.com/rust-random/rngs,MIT OR Apache-2.0,"The Rand Pr | |
ratatui,https://github.com/ratatui/ratatui,MIT,"Florian Dehau <[email protected]>, The Ratatui Developers" | ||
raw-cpuid,https://github.com/gz/rust-cpuid,MIT,Gerd Zellweger <[email protected]> | ||
raw-window-handle,https://github.com/rust-windowing/raw-window-handle,MIT OR Apache-2.0 OR Zlib,Osspial <[email protected]> | ||
rayon,https://github.com/rayon-rs/rayon,MIT OR Apache-2.0,"Niko Matsakis <[email protected]>, Josh Stone <[email protected]>" | ||
rdkafka,https://github.com/fede1024/rust-rdkafka,MIT,Federico Giraud <[email protected]> | ||
redis,https://github.com/redis-rs/redis-rs,BSD-3-Clause,The redis Authors | ||
redox_syscall,https://gitlab.redox-os.org/redox-os/syscall,MIT,Jeremy Soller <[email protected]> | ||
|
@@ -592,6 +594,7 @@ syn,https://github.com/dtolnay/syn,MIT OR Apache-2.0,David Tolnay <dtolnay@gmail | |
syn_derive,https://github.com/Kyuuhachi/syn_derive,MIT OR Apache-2.0,Kyuuhachi <[email protected]> | ||
sync_wrapper,https://github.com/Actyx/sync_wrapper,Apache-2.0,Actyx AG <[email protected]> | ||
synstructure,https://github.com/mystor/synstructure,MIT,Nika Layzell <[email protected]> | ||
sysinfo,https://github.com/GuillaumeGomez/sysinfo,MIT,Guillaume Gomez <[email protected]> | ||
syslog,https://github.com/Geal/rust-syslog,MIT,[email protected] | ||
syslog_loose,https://github.com/FungusHumungus/syslog-loose,MIT,Stephen Wakely <[email protected]> | ||
system-configuration,https://github.com/mullvad/system-configuration-rs,MIT OR Apache-2.0,Mullvad VPN | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The `host_metrics` now supports process metrics collection, configurable via the `process` option. | ||
|
||
authors: leeteng2001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use super::{default_all_processes, example_processes, FilterList, HostMetrics}; | ||
use std::ffi::OsStr; | ||
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System, UpdateKind}; | ||
use vector_lib::configurable::configurable_component; | ||
#[cfg(target_os = "linux")] | ||
use vector_lib::metric_tags; | ||
|
||
/// Options for the process metrics collector. | ||
#[configurable_component] | ||
#[derive(Clone, Debug, Default)] | ||
pub struct ProcessConfig { | ||
/// Lists of process name patterns to include or exclude. | ||
#[serde(default = "default_all_processes")] | ||
#[configurable(metadata(docs::examples = "example_processes()"))] | ||
processes: FilterList, | ||
} | ||
|
||
const RUNTIME: &str = "process_runtime"; | ||
const CPU_USAGE: &str = "process_cpu_usage"; | ||
const MEMORY_USAGE: &str = "process_memory_usage"; | ||
|
||
impl HostMetrics { | ||
pub async fn process_metrics(&self, output: &mut super::MetricsBuffer) { | ||
let mut sys = System::new(); | ||
sys.refresh_processes_specifics( | ||
ProcessesToUpdate::All, | ||
true, | ||
ProcessRefreshKind::new() | ||
.with_memory() | ||
.with_cpu() | ||
.with_cmd(UpdateKind::OnlyIfNotSet), | ||
); | ||
output.name = "process"; | ||
let sep = OsStr::new(" "); | ||
for (pid, process) in sys.processes().iter().filter(|&(_, proc)| { | ||
self.config | ||
.process | ||
.processes | ||
.contains_str(proc.name().to_str()) | ||
}) { | ||
let tags = || { | ||
metric_tags!( | ||
"pid" => pid.as_u32().to_string(), | ||
"name" => process.name().to_str().unwrap_or("unknown"), | ||
"command" => process.cmd().join(sep).to_str().unwrap_or("")) | ||
}; | ||
output.gauge(CPU_USAGE, process.cpu_usage().into(), tags()); | ||
output.gauge(MEMORY_USAGE, process.memory() as f64, tags()); | ||
output.counter(RUNTIME, process.run_time() as f64, tags()); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sources::host_metrics::tests::count_tag; | ||
|
||
use super::super::{HostMetrics, HostMetricsConfig, MetricsBuffer}; | ||
|
||
#[tokio::test] | ||
async fn generates_process_metrics() { | ||
let mut buffer = MetricsBuffer::new(None); | ||
HostMetrics::new(HostMetricsConfig::default()) | ||
.process_metrics(&mut buffer) | ||
.await; | ||
let metrics = buffer.metrics; | ||
assert!(!metrics.is_empty()); | ||
|
||
// All metrics are named process_* | ||
assert!(!metrics | ||
.iter() | ||
.any(|metric| !metric.name().starts_with("process_"))); | ||
|
||
// They should all have the required tag | ||
assert_eq!(count_tag(&metrics, "pid"), metrics.len()); | ||
assert_eq!(count_tag(&metrics, "name"), metrics.len()); | ||
assert_eq!(count_tag(&metrics, "command"), metrics.len()); | ||
} | ||
} |
Oops, something went wrong.