Manually install the Python agent
After you install the Python agent, Cloud Monitor 2.0 monitors your Python application and provides data such as application topology, traces, and SQL analysis.
For applications deployed in Kubernetes, we recommend that you install the corresponding component instead of using manual installation. For more information, see Install the Python agent for Container Service for Kubernetes (ACK) and Alibaba Cloud Container Compute Service (ACS) by using the ack-onepilot component.
If you encounter issues with the Python agent, see FAQ for the Python agent. If the FAQ does not resolve your issue, contact us through the DingTalk group (ID: 159215000379).
To ensure you always have access to the latest capabilities of the Python agent, the latest agent version is installed by default every time you use the aliyun-bootstrap component. If you need to manually specify an agent version, refer to the "Install a specific region and version" section in Python agent FAQ.
Prerequisites
-
Ensure your build environment is connected to the public internet or the Alibaba Cloud internal network. The security group for the environment must allow outbound TCP traffic on ports 80 and 443.
-
Check your Python and framework versions. For requirements, see Supported Python components and frameworks for ARMS Application Monitoring.
Python framework compatibility
Review these framework-specific requirements before you proceed.
uvicorn
If your application starts with uvicorn, choose one of the following approaches:
Approach A: Import the agent in code. Add this import statement to the first line of your uvicorn entry file:
from aliyun.opentelemetry.instrumentation.auto_instrumentation import sitecustomizeApproach B: Switch to gunicorn with the aliyun-instrument prefix. Replace your uvicorn command:
# Before
uvicorn app:app --workers 4 --port=9090 --host 0.0.0.0
# After
aliyun-instrument gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 app:appThe aliyun-instrument prefix handles initialization and non-intrusive instrumentation of the ARMS Python agent.
uWSGI
If your application starts with uWSGI, see Install the Python agent for a Django or Flask project that starts with uWSGI.
gevent
If you use gevent coroutines, set the GEVENT_ENABLE environment variable to true.
For example, if your code contains:
from gevent import monkey
monkey.patch_all()Set the following environment variable in your deployment:
GEVENT_ENABLE=trueStep 1: Download the aliyun-bootstrap installer
Download the agent installer from the Python Package Index (PyPI).
pip3 install aliyun-bootstrap
Step 2: Configure environment variables
You must manually add the following environment variables for your Python application:
# Method 1: Add environment variables for all processes in the current shell.
export ARMS_APP_NAME=<your-app-name> # Specifies a custom name for your application.
export ARMS_WORKSPACE=<your-workspace-name> # Specifies the workspace to which data is reported.
export ARMS_REGION_ID=<your-region-id> # Specifies the Region ID of your workspace.
export ARMS_LICENSE_KEY=<your-license-key> # Specifies the license key for authentication.# Method 2: Add environment variables for a specific process.
ARMS_APP_NAME=<your-app-name> ARMS_WORKSPACE=<your-workspace-name> ARMS_REGION_ID=<your-region-id> ARMS_LICENSE_KEY=<your-license-key> aliyun-instrument <your-app-file>.pyTo obtain the license key, see the authToken field in the response of the GetApplicationObservability API operation.
(Optional) Dockerfile example
For Docker environments, modify your Dockerfile based on the following example.
# Add environment variables
ENV ARMS_APP_NAME={your-app-name}
ENV ARMS_REGION_ID={your-region-id}
ENV ARMS_LICENSE_KEY={your-license-key}
ENV ARMS_WORKSPACE={your-workspace-name}
## Add your original Dockerfile commands hereStep 3: Install the Python agent
-
To speed up the installation, we recommend first configuring the image repository by running the following command:
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && pip config set install.trusted-host mirrors.aliyun.com -
Install the agent.
aliyun-bootstrap -a installNoteTo install a specific version of the Python agent, run the following command:
# Replace ${version} with the target version number. aliyun-bootstrap -a install -v ${version}For a list of all released Python agent versions, see Release notes for the Python agent.
Step 4: Start the application
Start with the ARMS Python agent
aliyun-instrument python app.py
View monitoring details
Log on to the Cloud Monitor 2.0 console, and select a workspace. In the left navigation pane, choose .
On the Application List page, find your application and click its name to view detailed monitoring data.