This topic describes how to manually install the ARMS agent for a Python function that uses a custom runtime.
Background information
Function Compute seamlessly integrates with Application Real-Time Monitoring Service (ARMS). After you install the agent for a Python function, ARMS starts to monitor the Python application. You can use the ARMS platform to view monitoring data such as application topology, call chains, and SQL analysis. For more information, see What is Application Real-Time Monitoring Service (ARMS)?.
Limits
The Python version of the function must be the same as the Python version of the custom runtime.
Step 1: Create an agent layer
1. Obtain the Python agent package
1.1. Download the agent package
// Download a specific version of the agent over a VPC network
wget http://arms-apm-${regionID}.oss-${regionID}-internal.aliyuncs.com/aliyun-python-agent/${version}/aliyun-python-agent.tar.gz
// Download the latest version of the agent over a VPC network
wget http://arms-apm-${regionID}.oss-${regionID}-internal.aliyuncs.com/aliyun-python-agent/aliyun-python-agent.tar.gz
// Download a specific version of the agent over the internet
wget http://arms-apm-${regionID}.oss-${regionID}.aliyuncs.com/aliyun-python-agent/${version}/aliyun-python-agent.tar.gz
// Download the latest version of the agent over the internet
wget http://arms-apm-${regionID}.oss-${regionID}.aliyuncs.com/aliyun-python-agent/aliyun-python-agent.tar.gzReplace ${regionID} with the region ID. For more information, see Supported regions.
Replace ${version} with the version number. For more information, see Python Agent version guide.
The following example shows how to download a specific version of the agent:
wget http://arms-apm-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/aliyun-python-agent/1.5.0/aliyun-python-agent.tar.gz1.2. Decompress the agent package and run pip install
## Decompress the package.
tar -zxvf aliyun-python-agent.tar.gz
## Build the package.
pip3 install target/*.whl -t aliyun-instrument
## Modify the aliyun-instrument file in the ./aliyun-instrument/bin folder. Replace the first line #!/usr/bin/python3 with the following content.
#!/var/fc/lang/python3.10/bin/python32. Create a layer in the console
Log on to the Function Compute console. In the left-side navigation pane, choose .
In the top navigation bar, select a region. On the Layers page, click Create Layer.
On the Create Layer page, select Upload From Folder to upload the layer.

When you upload the aliyun-instrument file, make sure that aliyun-instrument is the top-level folder. Do not nest the aliyun-instrument folder within another folder of the same name during compression and decompression. Otherwise, the environment variable configuration is affected.
Step 2: Use the custom layer in a function
1. Add the custom layer in the advanced configuration of the function
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Configuration tab. Click Edit to the right of Advanced Configuration. In the Advanced Configuration panel, choose . Select the Custom Layer and Layer Version. Then, click Deploy.

2. Add environment variables
Variable | Value |
ARMS_APP_NAME | FC:{function_name} |
ARMS_LICENSE_KEY | |
ARMS_REGION_ID | {region} |
LD_LIBRARY_PATH | /code:/code/lib:/usr/local/lib:/opt/lib:/opt/php8.1/lib:/opt/php8.0/lib:/opt/php7.2/lib |
PATH | /code/python/bin:/var/fc/lang/python3.10/bin:/usr/local/bin/apache-maven/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/ruby/bin:/opt/bin:/code:/code/bin |
PYTHONPATH | /opt:/opt/python:/code |
PYTHONUSERBASE | /code/python |
3. Modify the startup command
On the function details page, click the Configuration tab. Click Edit to the right of Basic Configuration. Modify the value of Startup Command as follows.
aliyun-instrument python3 app.pyIf the application is started using uvicorn, replace the startup command with the following command to connect to the agent.
For example:
uvicorn -w 4 -b 0.0.0.0:8000 app:appChange it to:
aliyun-instrument gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 app:appThe aliyun-instrument command handles the initialization and automatic instrumentation of the ARMS Python agent. If your program uses gevent coroutines, you must set the GEVENT_ENABLE environment variable to true. For example, if your program contains the following code:
from gevent import monkey
monkey.patch_all()Set the environment variable as follows:
GEVENT_ENABLE=trueVerify the results
After you execute the function, wait for about one minute. If the Python application appears on the Application Monitoring > Application List page in the ARMS console and reports data, this indicates that the agent is connected.
