Skip to content

Commit

Permalink
Merge pull request #16 from ancoron/feature/process-name
Browse files Browse the repository at this point in the history
feat: add actual process name to attributes - refs hubblo-org#367
  • Loading branch information
meslubi2021 authored Aug 25, 2024
2 parents 9267a6e + 7c6776d commit 04cc452
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,10 +926,11 @@ impl MetricGenerator {

for pid in self.topology.proc_tracker.get_alive_pids() {
let exe = self.topology.proc_tracker.get_process_name(pid);
let name = self.topology.proc_tracker.get_process_os_name(pid);
let cmdline = self.topology.proc_tracker.get_process_cmdline(pid);

let mut attributes = HashMap::new();
debug!("Working on {}: {}", pid, exe);
debug!("Working on {}: {}", pid, name);

#[cfg(feature = "containers")]
if self.watch_containers && (!self.containers.is_empty() || !self.pods.is_empty()) {
Expand All @@ -955,6 +956,8 @@ impl MetricGenerator {

attributes.insert("exe".to_string(), exe.clone());

attributes.insert("name".to_string(), name.clone());

if let Some(cmdline_str) = cmdline {
attributes.insert("cmdline".to_string(), utils::filter_cmdline(&cmdline_str));

Expand Down
18 changes: 18 additions & 0 deletions src/sensors/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct IStatus {
pub struct IProcess {
pub pid: Pid,
pub owner: u32,
pub name: String,
pub comm: String,
pub cmdline: Vec<String>,
//CPU (all of them) time usage, as a percentage
Expand Down Expand Up @@ -109,6 +110,7 @@ impl IProcess {
IProcess {
pid: process.pid(),
owner: 0,
name: process.name().to_string(),
comm: String::from(process.exe().to_str().unwrap()),
cmdline: process.cmd().to_vec(),
cpu_usage_percentage: process.cpu_usage(),
Expand All @@ -127,6 +129,7 @@ impl IProcess {
IProcess {
pid: process.pid(),
owner: 0,
name: process.name().to_string(),
comm: String::from(process.exe().to_str().unwrap()),
cmdline: process.cmd().to_vec(),
cpu_usage_percentage: process.cpu_usage(),
Expand Down Expand Up @@ -637,6 +640,21 @@ impl ProcessTracker {
process.first().unwrap().process.comm.clone()
}

/// Returns the OS process name associated to a PID
pub fn get_process_os_name(&self, pid: Pid) -> String {
let mut result = self
.procs
.iter()
.filter(|x| !x.is_empty() && x.first().unwrap().process.pid == pid);
let process = result.next().unwrap();
if result.next().is_some() {
panic!("Found two vectors of processes with the same id, maintainers should fix this.");
}

debug!("End of get process os name.");
process.first().unwrap().process.name.clone()
}

/// Returns the cmdline string associated to a PID
pub fn get_process_cmdline(&self, pid: Pid) -> Option<String> {
let mut result = self
Expand Down

0 comments on commit 04cc452

Please sign in to comment.