Integrate a Python agent with an FC application

更新时间:
复制 MD 格式

This topic describes how to manually install a Python agent for a function that uses a custom runtime.

Limits

The Python version of the function must be the same as the Python version of the custom runtime.

Step 1: Build the agent layer

1. Prepare the Python agent package

1.1. Download the agent package

# Download a specific agent version 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 agent version 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 agent version over the internet
wget http://arms-apm-${regionID}.oss-${regionID}.aliyuncs.com/aliyun-python-agent/${version}/aliyun-python-agent.tar.gz
# Download the latest agent version over the internet
wget http://arms-apm-${regionID}.oss-${regionID}.aliyuncs.com/aliyun-python-agent/aliyun-python-agent.tar.gz
  1. Replace ${regionID} with your region ID. For more information, see Supported regions.

  2. Replace ${version} with the desired agent version. For more information, see Python agent release notes.

The following example shows how to download version 2.6.4 over the internet:

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

1.2. Unpack the agent package and install dependencies

## Unpack the package.
tar -zxvf aliyun-python-agent.tar.gz
## Install dependencies.
pip3 install target/*.whl -t aliyun-instrument
## If you use a custom runtime (Debian 10), modify the aliyun-instrument file in the ./aliyun-instrument/bin directory. Replace the first line #!/usr/bin/python3 with the following content.
#!/var/fc/lang/python3.10/bin/python3

For Python paths in other runtimes, see custom runtimes. Update the path to match your runtime's Python installation directory.

2. Create a layer

  1. Log on to the Function Compute console. In the left-side navigation pane, choose function management > layers.

  2. In the top navigation bar, select a region. On the layers page, click Create layer.

  3. On the Create layer page, select Upload layer by folder to upload the built layer.

    Set Name to aliyun-instrument and Description to ARMS Python Agent. For compatible runtimes, select Debian 10 and custom runtime Debian 10. After the upload is complete, click Create.

When you upload the aliyun-instrument directory, ensure it is the top-level directory and not nested inside another directory of the same name. Improper nesting may affect the environment variable configuration.

Step 2: Use the custom layer in a function

1. Add the custom layer in the advanced configuration of the function

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

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Configuration tab. Click Edit to the right of Advanced Configuration. In the Advanced Configuration panel, choose +Add Layer > Add Custom Layer. Select the Custom Layer and Layer Version. Then, click Deploy.

image

2. Add environment variables

Variable

Value

ARMS_APP_NAME

FC:{function_name}

ARMS_LICENSE_KEY

Obtain the LicenseKey

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.py

If 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:app

Change it to:

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

The 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=true

Verify the results

After the function runs successfully, wait for about a minute. The integration is successful if your Python application appears on the Application Monitoring > Application List page in the ARMS console and is reporting data.