Manually install the Python agent

Updated at:

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.

Note

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

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 sitecustomize

Approach 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:app
Note

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

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

To 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 here

Step 3: Install the Python agent

  1. 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
  2. Install the agent.

    aliyun-bootstrap -a install
    Note

    To 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

  1. Log on to the Cloud Monitor 2.0 console, and select a workspace. In the left navigation pane, choose All Features > Application Observability > Application Monitoring.

  2. On the Application List page, find your application and click its name to view detailed monitoring data.