88from abc import ABCMeta , abstractmethod
99from typing import List , Optional , TextIO , Tuple , Union
1010
11+ from gprofiler .metadata .enrichment import EnrichmentOptions
1112from granulate_utils .linux .ns import resolve_host_path , resolve_proc_root_links
1213from psutil import NoSuchProcess , Process
1314
@@ -91,6 +92,8 @@ def _append_file_to_proc_wd(process: Process, file_path: str) -> str:
9192
9293
9394class _ApplicationIdentifier (metaclass = ABCMeta ):
95+ enrichment_options : Optional [EnrichmentOptions ] = None
96+
9497 @abstractmethod
9598 def get_application_name (self , process : Process ) -> Optional [str ]:
9699 pass
@@ -262,11 +265,25 @@ def get_application_name(self, process: Process) -> Optional[str]:
262265 return None
263266
264267 try :
265- java_properties = run_process ([jattach_path (), str (process .pid ), "properties" ]).stdout .decode ()
268+ java_properties = run_process ([jattach_path (), str (process .pid ), "jcmd" , "VM.command_line" ]).stdout .decode ()
269+ java_command = None
270+ java_args = []
266271 for line in java_properties .splitlines ():
267- if line .startswith ("sun.java.command" ):
268- app_id = line [line .find ("=" ) + 1 :].split (" " , 1 )[0 ]
269- return f"java: { app_id } "
272+ if line .startswith ("jvm_args:" ):
273+ if (
274+ self .enrichment_options is not None
275+ and self .enrichment_options .application_identifier_args_filters
276+ ):
277+ for arg in line [line .find (":" ) + 1 :].strip ().split (" " ):
278+ if any (
279+ re .search (flag_filter , arg )
280+ for flag_filter in self .enrichment_options .application_identifier_args_filters
281+ ):
282+ java_args .append (arg )
283+ if line .startswith ("java_command:" ):
284+ java_command = line [line .find (":" ) + 1 :].strip ().split (" " , 1 )[0 ]
285+ if java_command :
286+ return f"java: { java_command } { ' (' + ' ' .join (java_args ) + ')' if java_args else '' } "
270287 except CalledProcessError as e :
271288 _logger .warning (f"Couldn't get Java properties for process { process .pid } : { e .stderr } " )
272289
@@ -286,6 +303,10 @@ def get_application_name(self, process: Process) -> Optional[str]:
286303]
287304
288305
306+ def set_enrichment_options (enrichment_options : EnrichmentOptions ) -> None :
307+ _ApplicationIdentifier .enrichment_options = enrichment_options
308+
309+
289310def get_application_name (process : Union [int , Process ]) -> Optional [str ]:
290311 """
291312 Tries to identify the application running in a given process, application identification is fully heuristic,
0 commit comments