All Products
Search
Document Center

Application Real-Time Monitoring Service:Integrate a Python agent with a Function Compute application

Last Updated:Mar 11, 2026

When you run Python applications on Function Compute (FC), you need visibility into traces and performance metrics to diagnose latency, errors, and bottlenecks. Application Real-Time Monitoring Service (ARMS) provides a Python agent that collects this data automatically. The agent wraps your startup command to inject monitoring -- no code changes required.

How it works

The integration follows three steps:

  1. Build an agent layer -- Download the ARMS Python agent package, install its dependencies, and upload the result as an FC layer.

  2. Configure the function -- Attach the layer to your function, set environment variables for ARMS authentication, and modify the startup command.

  3. Verify -- Run the function and confirm that monitoring data appears in the ARMS console.

Prerequisites

Before you begin, make sure that you have:

  • An FC function that uses a custom runtime

  • A local Python version that matches the custom runtime version (for example, Python 3.10)

  • An ARMS LicenseKey -- call the DescribeTraceLicenseKey API to retrieve it

  • The region ID of the region where your function is deployed

Step 1: Build the agent layer

Download the agent package

Run wget to download the ARMS Python agent package. Choose the URL based on your network:

Over a VPC network (internal endpoint):

# Download a specific version
wget http://arms-apm-<region-id>.oss-<region-id>-internal.aliyuncs.com/aliyun-python-agent/<version>/aliyun-python-agent.tar.gz

# Download the latest version
wget http://arms-apm-<region-id>.oss-<region-id>-internal.aliyuncs.com/aliyun-python-agent/aliyun-python-agent.tar.gz

Over the internet (public endpoint):

# Download a specific version
wget http://arms-apm-<region-id>.oss-<region-id>.aliyuncs.com/aliyun-python-agent/<version>/aliyun-python-agent.tar.gz

# Download the latest version
wget http://arms-apm-<region-id>.oss-<region-id>.aliyuncs.com/aliyun-python-agent/aliyun-python-agent.tar.gz

Replace the following placeholders:

PlaceholderDescriptionExample
<region-id>The region where your function is deployed. See Supported regions.cn-hangzhou
<version>The agent version. See Python agent release notes.1.5.0

For example, to download version 1.5.0 from the cn-hangzhou region over the internet:

wget http://arms-apm-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/aliyun-python-agent/1.5.0/aliyun-python-agent.tar.gz

Install the agent dependencies

Extract the package and install the Python wheels into a folder named aliyun-instrument:

# Extract the package
tar -zxvf aliyun-python-agent.tar.gz

# Install dependencies into the aliyun-instrument folder
pip3 install target/*.whl -t aliyun-instrument

After installation, update the shebang line in ./aliyun-instrument/bin/aliyun-instrument. Replace the first line:

#!/usr/bin/python3

with the Python path used by your FC custom runtime:

#!/var/fc/lang/python3.10/bin/python3
Adjust the Python path if your custom runtime uses a different Python version.

Upload the layer to Function Compute

  1. Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Layers.

  2. In the top navigation bar, select the region where your function is deployed. On the Layer Management page, click Create Layer.

  3. On the Create Layer page, select Upload Layer By Folder and upload the aliyun-instrument folder.

Create a layer by uploading the aliyun-instrument folder
Important

The aliyun-instrument folder must be the top-level folder in the upload. Do not nest it inside another folder with the same name. An incorrect folder structure causes environment variable resolution to fail.

Step 2: Configure the function

Attach the layer

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select the region. On the Functions page, click the name of your function.

  3. On the function details page, click the Configuration tab. Click Edit next to Advanced Configuration.

  4. In the Advanced Configuration panel, click +Add Layer and then select Add Custom Layer. Select the layer you created in Step 1 and choose the Layer Version. Click Deploy.

Add the custom layer to the function

Set environment variables

On the Configuration tab, add the following environment variables:

VariableValueDescription
ARMS_APP_NAMEFC:<your-function-name>The application name displayed in ARMS. Replace <your-function-name> with the actual function name.
ARMS_LICENSE_KEY<your-license-key>The ARMS LicenseKey. See Prerequisites.
ARMS_REGION_ID<region-id>The region ID, such as cn-hangzhou.
LD_LIBRARY_PATH/code:/code/lib:/usr/local/lib:/opt/lib:/opt/php8.1/lib:/opt/php8.0/lib:/opt/php7.2/libLibrary search paths required by the runtime.
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/binExecutable search paths. Includes the Python binary and the aliyun-instrument command from the layer.
PYTHONPATH/opt:/opt/python:/codePython module search paths. /opt points to the layer contents.
PYTHONUSERBASE/code/pythonBase directory for Python user-level packages.

Modify the startup command

The aliyun-instrument command wraps your application startup to inject ARMS monitoring automatically, without changes to your application code.

On the function details page, click the Configuration tab and then click Edit next to Basic Configuration. Prepend aliyun-instrument to your existing Startup Command:

aliyun-instrument python3 app.py

uvicorn applications

If your application starts with uvicorn, switch to gunicorn with the UvicornWorker class and prepend aliyun-instrument. For example, change:

uvicorn -w 4 -b 0.0.0.0:8000 app:app

to:

aliyun-instrument gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 app:app

gevent applications

If your application uses gevent coroutines, for example:

from gevent import monkey
monkey.patch_all()

add the following environment variable:

VariableValue
GEVENT_ENABLEtrue

This tells the agent to apply gevent-compatible instrumentation hooks.

Verify the integration

  1. Invoke your function to generate traffic.

  2. Wait about one minute for the agent to report data.

  3. Log on to the ARMS console. In the left-side navigation pane, choose Application Monitoring > Application List.

  4. Confirm that your application appears in the list and is reporting data.

Verify that the application appears in the ARMS Application List

Troubleshooting

If the application does not appear after a few minutes, check the following:

IssueWhat to check
Authentication failureThe ARMS_LICENSE_KEY value is correct.
Wrong regionThe ARMS_REGION_ID matches the region where the function is deployed.
Layer structure errorThe aliyun-instrument folder is the top-level folder in the layer, not nested inside another folder.
Agent not loadedThe PYTHONPATH includes /opt so that the function can locate the agent dependencies in the layer.
Startup command incorrectThe startup command is prefixed with aliyun-instrument (for example, aliyun-instrument python3 app.py).

Related topics