After you manually install the ARMS agent for a Python application and deploy the application to SAE, you can use ARMS to monitor the application. With ARMS, you can view various monitoring data, such as application topology, call trace analysis, and SQL performance analysis. This topic describes how to manually install the ARMS agent for a Python application and deploy it to SAE.
Prerequisites
Make sure that your build environment can connect to the internet or an Alibaba Cloud internal network. The security group of the build environment must allow outbound TCP traffic on ports 80 and 443.
Check your Python and framework versions. For more information, see Python components and frameworks supported by ARMS Application Monitoring.
If you want to deploy a Python project to SAE from an image, activate Container Registry and then create an Enterprise instance or create a Personal instance.
Usage notes
If the application is started by using Uvicorn, replace the command to install the agent.
For example, if the following command is used to start your application:
uvicorn -w 4 -b 0.0.0.0:8000 app:appReplace the command with the following:
aliyun-instrument gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 app:appNoteThe
aliyun-instrumentcommand initializes the ARMS agent for Python and instruments data in a non-intrusive way.If you use gevent coroutines, set the environment variable
GEVENT_ENABLE=true.Example:
from gevent import monkey monkey.patch_all()Run the following command to set the environment variable:
GEVENT_ENABLE=true
Procedure
Prepare the project: This topic uses a demo project as an example.
Create a Docker image for the Python project or package the project into a ZIP file: If you deploy the application from an image, you must download and install the Python agent when you create the image. If you deploy the application from a code package, the system automatically downloads and installs the Python agent during deployment. You only need to start the application using the ARMS Python agent.
Deploy the Python application: Deploy the Python application in the SAE console and enable application monitoring. If you deploy the application from a code package, you must also set the startup command to start the application using the ARMS Python agent.
View application monitoring data: View the various monitoring data generated by the application.
1. Prepare the project
Upload the project to your environment.
Install the required environment to run and test the project. For more information, see Installing packages.
Test the project locally to make sure that it runs as expected.
2. Create a Docker image or package the Python project into a ZIP file
Create a Docker image and push it to an image repository
Create a Python image.
This topic describes only the commands to download and install the agent, and to start the application using the ARMS Python agent.
... # Download the agent installer from the PyPI repository. RUN pip3 install aliyun-bootstrap # Use aliyun-bootstrap to install the Python agent. RUN aliyun-bootstrap -a install ... # Start the application using the ARMS Python agent. CMD ["aliyun-instrument","python","main.py"]NoteIf you do not need to start the application using the Python agent, you must import the Python agent in the main entry file of the application, such as
main.py, and then start the application.from aliyun.opentelemetry.instrumentation.auto_instrumentation import sitecustomizePush the created Python image to an Alibaba Cloud image repository. For more information, see Push and pull images (Enterprise Edition) or Push and pull images (Personal Edition).
Package the Python project into a ZIP file
Package the Python project into a ZIP file in your local environment. For more information, see Instructions for packaging a Python project into a ZIP file.
3. Create a Python application
Create a Python application from an image
3.1 Create the application
This topic describes only the key steps. For more information, see Deploy an application from an image.
On the Configure Image panel, set Technology Stack to Python and select an image version.

On the Advanced Settings page, in the Application Monitoring section, enable Application Monitoring.

Click Create Application.
3.2 Test access
Test using the public endpoint
Add a public endpoint for the application. For more information, see Bind a CLB instance to an application and generate a public or internal endpoint.

Copy the public endpoint and paste it into a browser to test access.
Test in Webshell
Go to the Basic Information page of the application and click the Instance List tab.

In the Actions column of the instance, click Webshell.

On the Webshell page, enter
curl 127.0.0.1:ContainerPortto test.
Create a Python application from a code package
3.1 Create the application
This topic describes only the key steps. For more information, see Deploy a Python application.
On the Configure Code Package Deployment panel, set Technology Stack to Python. Then, in the Upload ZIP Package section, click the
icon to upload your ZIP package.
On the Advanced Settings page, configure the following settings.
In the Startup Command section, set the startup command.
aliyun-instrument python main.py # Start the application using the ARMS Python agent. main.py is the name of the main entry file in this example.
In the Application Monitoring section, enable Application Monitoring.

Click Create Application.
3.2 Test access
Test using the public endpoint
Add a public endpoint for the application. For more information, see Bind a CLB instance to an application and generate a public or internal endpoint.

Copy the public endpoint and paste it into a browser to test access.
Test in Webshell
Go to the Basic Information page of the application and click the Instance List tab.

In the Actions column of the instance, click Webshell.

On the Webshell page, enter
curl 127.0.0.1:ContainerPortto test.
4. View application monitoring data
View in the SAE console
Go to the Basic Information page for the application. In the navigation pane on the left, click Application Monitoring. On the Application Monitoring page, view the Monitoring Data.

View in the ARMS console
Log on to the ARMS console.
In the navigation pane on the left, choose . In the top navigation bar, select the destination region.

On the Applications page, search for your application and click its name.

On the monitoring details page, view the monitoring data.
