@@ -76,7 +76,7 @@ const sensor::MsgFromCollector* SensorClientFormatter::ToProtoMessage(sinsp_evt*
7676 Reset ();
7777
7878 if (!ValidateProcessDetails (event)) {
79- CLOG (INFO) << " Dropping process event: " << ProcessDetails (event);
79+ CLOG (INFO) << " Dropping process event: " << ToString (event);
8080 return nullptr ;
8181 }
8282
@@ -178,10 +178,9 @@ ProcessSignal* SensorClientFormatter::CreateProcessSignal(sinsp_evt* event) {
178178 }
179179
180180 // set process lineage
181- std::vector<LineageInfo> lineage;
182- this ->GetProcessLineage (event->get_thread_info (), lineage);
181+ auto lineage = GetProcessLineage (event->get_thread_info ());
183182 for (const auto & p : lineage) {
184- auto signal_lineage = signal->add_lineage_info ();
183+ auto * signal_lineage = signal->add_lineage_info ();
185184 signal_lineage->set_parent_exec_file_path (p.parent_exec_file_path ());
186185 signal_lineage->set_parent_uid (p.parent_uid ());
187186 }
@@ -241,11 +240,10 @@ ProcessSignal* SensorClientFormatter::CreateProcessSignal(sinsp_threadinfo* tinf
241240 signal->set_container_id (tinfo->m_container_id );
242241
243242 // set process lineage
244- std::vector<LineageInfo> lineage;
245- GetProcessLineage (tinfo, lineage);
243+ auto lineage = GetProcessLineage (tinfo);
246244
247245 for (const auto & p : lineage) {
248- auto signal_lineage = signal->add_lineage_info ();
246+ auto * signal_lineage = signal->add_lineage_info ();
249247 signal_lineage->set_parent_exec_file_path (p.parent_exec_file_path ());
250248 signal_lineage->set_parent_uid (p.parent_uid ());
251249 }
@@ -258,7 +256,7 @@ ProcessSignal* SensorClientFormatter::CreateProcessSignal(sinsp_threadinfo* tinf
258256 return signal;
259257}
260258
261- std::string SensorClientFormatter::ProcessDetails (sinsp_evt* event) {
259+ std::string SensorClientFormatter::ToString (sinsp_evt* event) {
262260 std::stringstream ss;
263261 const std::string* path = event_extractor_->get_exepath (event);
264262 const std::string* name = event_extractor_->get_comm (event);
@@ -293,37 +291,28 @@ bool SensorClientFormatter::ValidateProcessDetails(sinsp_evt* event) {
293291 return ValidateProcessDetails (tinfo);
294292}
295293
296- int SensorClientFormatter::GetTotalStringLength (const std::vector<LineageInfo>& lineage) {
297- int totalStringLength = 0 ;
298- for (LineageInfo l : lineage) {
299- totalStringLength += l.parent_exec_file_path ().size ();
300- }
301-
302- return totalStringLength;
303- }
294+ void SensorClientFormatter::UpdateLineageStats (const std::vector<LineageInfo>& lineage) {
295+ int string_total = std::accumulate (lineage.cbegin (), lineage.cend (), 0 , [](int acc, const auto & l) {
296+ return acc + l.parent_exec_file_path ().size ();
297+ });
304298
305- void SensorClientFormatter::CountLineage (const std::vector<LineageInfo>& lineage) {
306- int totalStringLength = GetTotalStringLength (lineage);
307299 COUNTER_INC (CollectorStats::process_lineage_counts);
308300 COUNTER_ADD (CollectorStats::process_lineage_total, lineage.size ());
309301 COUNTER_ADD (CollectorStats::process_lineage_sqr_total, lineage.size () * lineage.size ());
310- COUNTER_ADD (CollectorStats::process_lineage_string_total, totalStringLength );
302+ COUNTER_ADD (CollectorStats::process_lineage_string_total, string_total );
311303}
312304
313- void SensorClientFormatter::GetProcessLineage (sinsp_threadinfo* tinfo,
314- std::vector<LineageInfo>& lineage) {
305+ std::vector<LineageInfo> SensorClientFormatter::GetProcessLineage (sinsp_threadinfo* tinfo) {
306+ std::vector<LineageInfo> lineage;
315307 if (tinfo == nullptr ) {
316- return ;
308+ return lineage ;
317309 }
318- sinsp_threadinfo* mt = nullptr ;
319- if (tinfo->is_main_thread ()) {
320- mt = tinfo;
321- } else {
322- mt = tinfo->get_main_thread ();
323- if (mt == nullptr ) {
324- return ;
325- }
310+
311+ sinsp_threadinfo* mt = tinfo->get_main_thread ();
312+ if (mt == nullptr ) {
313+ return lineage;
326314 }
315+
327316 sinsp_threadinfo::visitor_func_t visitor = [&lineage](sinsp_threadinfo* pt) {
328317 if (pt == nullptr ) {
329318 return false ;
@@ -332,22 +321,9 @@ void SensorClientFormatter::GetProcessLineage(sinsp_threadinfo* tinfo,
332321 return false ;
333322 }
334323
335- //
336324 // Collection of process lineage information should stop at the container
337325 // boundary to avoid collecting host process information.
338- //
339- // In back-ported eBPF probes, `m_vpid` will not be set for containers
340- // running when collector comes online because /proc/{pid}/status does
341- // not contain namespace information, so `m_container_id` is checked
342- // instead. `m_container_id` is not enough on its own to identify
343- // containerized processes, because it is not guaranteed to be set on
344- // all platforms.
345- //
346- if (pt->m_vpid == 0 ) {
347- if (pt->m_container_id .empty ()) {
348- return false ;
349- }
350- } else if (pt->m_pid == pt->m_vpid ) {
326+ if (pt->m_pid == pt->m_vpid ) {
351327 return false ;
352328 }
353329
@@ -371,7 +347,9 @@ void SensorClientFormatter::GetProcessLineage(sinsp_threadinfo* tinfo,
371347 return true ;
372348 };
373349 mt->traverse_parent_state (visitor);
374- CountLineage (lineage);
350+ UpdateLineageStats (lineage);
351+
352+ return lineage;
375353}
376354
377355} // namespace collector
0 commit comments