11# (c) Copyright IBM Corp. 2021
22# (c) Copyright Instana Inc. 2020
33
4- import re
54import os
5+ import platform
6+ import re
67import sys
8+ from typing import Dict , List , Tuple , Union
79
8- from . .log import logger
10+ from instana .log import logger
911
10- def get_py_source (filename ):
11- """
12- Retrieves and returns the source code for any Python
13- files requested by the UI via the host agent
1412
15- @param filename [String] The fully qualified path to a file
13+ def get_py_source (filename : str ) -> Dict [str , str ]:
14+ """
15+ Retrieves the source code for Python files requested by the UI via the host agent.
16+
17+ This function reads and returns the content of Python source files. It validates
18+ that the requested file has a .py extension and returns an appropriate error
19+ message if the file cannot be read or is not a Python file.
20+
21+ Args:
22+ filename (str): The fully qualified path to a Python source file
23+
24+ Returns:
25+ Dict[str, str]: A dictionary containing either:
26+ - {"data": source_code} if successful
27+ - {"error": error_message} if an error occurred
1628 """
1729 response = None
1830 try :
@@ -35,9 +47,24 @@ def get_py_source(filename):
3547regexp_py = re .compile (r"\.py$" )
3648
3749
38- def determine_service_name ():
39- """ This function makes a best effort to name this application process. """
40-
50+ def determine_service_name () -> str :
51+ """
52+ Determines the most appropriate service name for this application process.
53+
54+ The service name is determined using the following priority order:
55+ 1. INSTANA_SERVICE_NAME environment variable if set
56+ 2. For specific frameworks:
57+ - For gunicorn: process title or "gunicorn"
58+ - For Flask: FLASK_APP environment variable
59+ - For Django: first part of DJANGO_SETTINGS_MODULE
60+ - For uwsgi: "uWSGI master/worker [app_name]"
61+ 3. Command line arguments (first non-option argument)
62+ 4. Executable name
63+ 5. "python" as a fallback
64+
65+ Returns:
66+ str: The determined service name
67+ """
4168 # One environment variable to rule them all
4269 if "INSTANA_SERVICE_NAME" in os .environ :
4370 return os .environ ["INSTANA_SERVICE_NAME" ]
@@ -115,11 +142,22 @@ def determine_service_name():
115142
116143 return app_name
117144
118- def get_proc_cmdline (as_string = False ):
145+ def get_proc_cmdline (as_string : bool = False ) -> Union [ List [ str ], str ] :
119146 """
120- Parse the proc file system for the command line of this process. If not available, then return a default.
121- Return is dependent on the value of `as_string`. If True, return the full command line as a string,
122- otherwise a list.
147+ Parses the process command line from the proc file system.
148+
149+ This function attempts to read the command line of the current process from
150+ /proc/self/cmdline. If the proc filesystem is not available (e.g., on non-Unix
151+ systems), it returns a default value.
152+
153+ Args:
154+ as_string (bool, optional): If True, returns the command line as a single
155+ space-separated string. If False, returns a list
156+ of command line arguments. Defaults to False.
157+
158+ Returns:
159+ Union[List[str], str]: The command line as either a list of arguments or a
160+ space-separated string, depending on the as_string parameter.
123161 """
124162 name = "python"
125163 if os .path .isfile ("/proc/self/cmdline" ):
@@ -140,4 +178,24 @@ def get_proc_cmdline(as_string=False):
140178 if as_string is True :
141179 parts = " " .join (parts )
142180
143- return parts
181+ return parts
182+
183+
184+ def get_runtime_env_info () -> Tuple [str , str ]:
185+ """
186+ Returns information about the current runtime environment.
187+
188+ This function collects and returns details about the machine architecture
189+ and Python version being used by the application.
190+
191+ Returns:
192+ Tuple[str, str]: A tuple containing:
193+ - Machine type (e.g., 'arm64', 'ppc64le')
194+ - Python version string
195+ """
196+ machine = platform .machine ()
197+ python_version = platform .python_version ()
198+
199+ return machine , python_version
200+
201+ # Made with Bob
0 commit comments