@@ -15,8 +15,6 @@ def override(func): # type: ignore[reportMissingParameterType]
1515
1616
1717import utils
18- from internalapi .sensor .collector_pb2 import ProcessSignal
19- from internalapi .sensor .sfa_pb2 import FileActivity
2018
2119
2220def extract_container_id (cgroup : str ) -> str :
@@ -177,25 +175,26 @@ def container_id(self) -> str:
177175 def loginuid (self ) -> int :
178176 return self ._loginuid
179177
180- def diff (self , other : ProcessSignal ) -> dict | None :
178+ def diff (self , other : Process ) -> dict | None :
181179 """
182- Compare this Process with a ProcessSignal protobuf message.
180+ Compare this Process with another Process instance.
181+
182+ PID comparison is skipped if self.pid is None.
183183
184184 Args:
185- other: ProcessSignal protobuf message to compare against
185+ other: Process instance to compare against.
186186
187187 Returns:
188- None if identical, dict of differences if not matching
188+ None if identical, dict of differences if not matching.
189189 """
190190 diff = {}
191191
192- # Compare each field
193192 if self .pid is not None :
194193 Event ._diff_field (diff , 'pid' , self .pid , other .pid )
195194
196195 Event ._diff_field (diff , 'uid' , self .uid , other .uid )
197196 Event ._diff_field (diff , 'gid' , self .gid , other .gid )
198- Event ._diff_field (diff , 'exe_path' , self .exe_path , other .exec_file_path )
197+ Event ._diff_field (diff , 'exe_path' , self .exe_path , other .exe_path )
199198 Event ._diff_field (diff , 'args' , self .args , other .args )
200199 Event ._diff_field (diff , 'name' , self .name , other .name )
201200 Event ._diff_field (
@@ -204,7 +203,7 @@ def diff(self, other: ProcessSignal) -> dict | None:
204203 self .container_id ,
205204 other .container_id ,
206205 )
207- Event ._diff_field (diff , 'loginuid' , self .loginuid , other .login_uid )
206+ Event ._diff_field (diff , 'loginuid' , self .loginuid , other .loginuid )
208207
209208 return diff if diff else None
210209
@@ -302,106 +301,77 @@ def _diff_path(
302301 diff : dict ,
303302 name : str ,
304303 expected : str | Pattern [str ] | None ,
305- actual : str ,
304+ actual : str | Pattern [ str ] | None ,
306305 ):
307306 """
308307 Compare paths with regex pattern support.
308+
309+ When expected is a compiled regex pattern, actual must be a
310+ string that matches it. Otherwise a simple equality check is
311+ performed.
309312 """
310313 if isinstance (expected , Pattern ):
311- if not expected .match (actual ):
314+ if not isinstance ( actual , str ) or not expected .match (actual ):
312315 diff [name ] = {'expected' : f'{ expected } ' , 'actual' : actual }
313316 elif expected != actual :
314317 diff [name ] = {'expected' : expected , 'actual' : actual }
315318
316- def diff (self , other : FileActivity ) -> dict | None :
319+ def diff (self , other : Event ) -> dict | None :
317320 """
318- Compare this Event with a FileActivity protobuf message.
321+ Compare this Event with another Event instance.
322+
323+ Both gRPC and OTLP servers translate their native messages
324+ into Event objects, so this method provides a single
325+ protocol-agnostic comparison path.
319326
320327 Args:
321- other: FileActivity protobuf message to compare against
328+ other: Event instance to compare against.
322329
323330 Returns:
324- None if identical, dict of differences if not matching
331+ None if identical, dict of differences if not matching.
325332 """
326333 diff = {}
327334
328- # Check process differences first
329335 process_diff = self .process .diff (other .process )
330336 if process_diff is not None :
331337 diff ['process' ] = process_diff
332338
333- # Check event type
334- event_type_expected = self .event_type .name .lower ()
335- event_type_actual = other .WhichOneof ('file' )
336-
337339 Event ._diff_field (
338340 diff ,
339341 'event_type' ,
340- event_type_expected ,
341- event_type_actual ,
342+ self . event_type ,
343+ other . event_type ,
342344 )
343345 if diff :
344346 return diff
345347
346- # Get the appropriate event field based on type
347- event_field = getattr (other , event_type_expected )
348-
349348 # Rename handling is a bit different to the rest, since it has
350349 # new and old paths.
351- if self .event_type == EventType .RENAME :
352- Event ._diff_path (diff , 'new_file' , self .file , event_field .new .path )
350+ if self .event_type != EventType .RENAME :
351+ Event ._diff_path (diff , 'file' , self .file , other .file )
352+ Event ._diff_path (diff , 'host_path' , self .host_path , other .host_path )
353+ else :
354+ Event ._diff_path (diff , 'new_file' , self .file , other .file )
353355 Event ._diff_path (
354- diff ,
355- 'new_host_path' ,
356- self .host_path ,
357- event_field .new .host_path ,
356+ diff , 'new_host_path' , self .host_path , other .host_path
358357 )
358+ Event ._diff_path (diff , 'old_file' , self .old_file , other .old_file )
359359 Event ._diff_path (
360- diff ,
361- 'old_file' ,
362- self .old_file ,
363- event_field .old .path ,
360+ diff , 'old_host_path' , self .old_host_path , other .old_host_path
364361 )
365- Event ._diff_path (
366- diff ,
367- 'old_host_path' ,
368- self .old_host_path ,
369- event_field .old .host_path ,
370- )
371- return diff if diff else None
372-
373- # Compare file and host_path (common to all event types)
374- # All event types have .activity.path and .activity.host_path
375- # accessed differently
376- Event ._diff_path (diff , 'file' , self .file , event_field .activity .path )
377- Event ._diff_path (
378- diff ,
379- 'host_path' ,
380- self .host_path ,
381- event_field .activity .host_path ,
382- )
383362
384363 if self .event_type == EventType .PERMISSION :
385- Event ._diff_field (diff , 'mode' , self .mode , event_field .mode )
364+ Event ._diff_field (diff , 'mode' , self .mode , other .mode )
386365 elif self .event_type == EventType .OWNERSHIP :
387366 Event ._diff_field (
388- diff ,
389- 'owner_uid' ,
390- self .owner_uid ,
391- event_field .uid ,
367+ diff , 'owner_uid' , self .owner_uid , other .owner_uid
392368 )
393369 Event ._diff_field (
394- diff ,
395- 'owner_gid' ,
396- self .owner_gid ,
397- event_field .gid ,
370+ diff , 'owner_gid' , self .owner_gid , other .owner_gid
398371 )
399372 elif self .event_type in (EventType .XATTR_SET , EventType .XATTR_REMOVE ):
400373 Event ._diff_field (
401- diff ,
402- 'xattr_name' ,
403- self .xattr_name ,
404- event_field .xattr_name ,
374+ diff , 'xattr_name' , self .xattr_name , other .xattr_name
405375 )
406376
407377 return diff if diff else None
0 commit comments