After you instrument a Python application with OpenTelemetry and report its trace data to Cloud Monitor 2.0, Cloud Monitor 2.0 starts monitoring the application. You can then view monitoring data, such as application topology, call traces, abnormal transactions, slow transactions, and SQL analysis. This topic describes how to use the OpenTelemetry Python Agent or software development kit (SDK) for automatic or manual instrumentation and data reporting.
Use the Alibaba Cloud proprietary Python Agent for the best results.
Compared to open source implementations, the Alibaba Cloud Python agent provides observability for common large model frameworks, such as llama-index, dify, langchain, openai, and Qwen. It also offers richer metrics, traces, and continuous profiling data. For more information, see Manually install the Python agent.
Prerequisites
Python 3.7 or later.
Background information
OpenTelemetry provides several automatic instrumentation plugins that support automatic Span creation for common frameworks. The following table lists the supported frameworks. For a complete list, see the official OpenTelemetry documentation.
Version ~= V.N indicates ≥ V.N and == V.*. For example, aiohttp ~= 3.0 means that the required aiohttp version is aiohttp ≥ 3.0 and aiohttp == 3.*.
Step 1: Get endpoint information
Log on to the Cloud Monitor 2.0 console, and select a workspace. In the left navigation pane, click Integration Center.
In the Server Applications section, click the Python card, and set Protocol Type to OpenTelemetry.
In the Parameter Settings section, click Get on the right of LicenseKey. Then, set Instrumentation Method, Connection Method, and Reporting Method as needed. Enter Application Name, Version, and Environment.
The corresponding integration code is generated at the bottom of the page based on your configurations. The code contains endpoint information, such as the Endpoint and LicenseKey.

Step 2: Set dependency libraries
Automatic instrumentation (Recommended)
Download and install the required packages.
pip install opentelemetry-distro opentelemetry-exporter-otlp opentelemetry-bootstrap -a installNoteThe
opentelemetry-bootstrap -a installcommand scans the site-packages folder of the current Python environment. If a framework that OpenTelemetry supports for automatic instrumentation is found, the command automatically downloads the corresponding OpenTelemetry plugin for that framework. For example, if Django is already installed, the command automatically downloads the opentelemetry-instrumentation-django package.Configure the following environment variables and then start your application.
export OTEL_SERVICE_NAME=<service name> export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=<traces.endpoint> export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=<metrics.endpoint> export OTEL_EXPORTER_OTLP_HEADERS="x-arms-license-key=<license-key>,x-arms-project=<arms-project>,x-cms-workspace=<workspace>" export OTEL_RESOURCE_ATTRIBUTES=service.name=<service name>,acs.cms.workspace=<workspace>,service.version=<service version>,deployment.environment=<environment> opentelemetry-instrument <your_run_command>Replace the placeholders in the preceding code with the endpoint information that you obtained in Step 1. Replace
<your_run_command>with the startup command for your Python application. Examples of<your_run_command>include the following:Normal application:
python app.pyFlask application:
flask run -p 8000Django application:
python manage.py runserver --noreloaduWSGI application:
uwsgi --http :8000 --module app.wsgiGunicorn application:
gunicorn app:app --bind=127.0.0.1:8000orgunicorn app:app --workers 2 --worker-class uvicorn.workers.UvicornWorker --bind 127.0.0.1:8000(multi-worker mode)Uvicorn application:
uvicorn app:app --host localhost --port 8000
NoteWhen you use automatic instrumentation, do not enable hot reloading configurations, such as
--reloadorreload=True. Otherwise, OpenTelemetry automatic instrumentation will fail. Hot reloading is enabled by default in Django development environments. To use OpenTelemetry automatic instrumentation, you must explicitly specify the--noreloadparameter. OpenTelemetry automatic instrumentation does not currently support the multi-worker mode for Uvicorn applications.
Manual instrumentation
Download the required packages.
pip install opentelemetry-api pip install opentelemetry-sdk pip install opentelemetry-exporter-otlpCreate a Python demo application.
Create a main.py file and add the following content:
from opentelemetry import trace, baggage from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter as OTLPSpanHttpExporter from opentelemetry.sdk.resources import DEPLOYMENT_ENVIRONMENT, HOST_NAME, Resource, SERVICE_NAME, SERVICE_VERSION from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor def inner_method(): tracer = trace.get_tracer(__name__) with tracer.start_as_current_span("child_span") as child_span: print("hello world") def outer_method(): tracer = trace.get_tracer(__name__) with tracer.start_as_current_span("parent_span") as parent_span: inner_method() # Initialize OpenTelemetry def init_opentelemetry(): # Set the service name and hostname. resource = Resource(attributes={ SERVICE_NAME: "<service name>", # Application name. SERVICE_VERSION: "<service version>", # Version number. DEPLOYMENT_ENVIRONMENT: "<environment>", # Deployment environment. HOST_NAME: "${hostName}", # Replace ${hostName} with the hostname. "acs.cms.workspace": "<workspace>" # Replace <workspace> with your workspace name. }) headers = { "x-arms-license-key": "<license-key>", # Replace <license-key> with the endpoint information obtained in Step 1. "x-arms-project": "<arms-project>", # Replace <arms-project> with the endpoint information obtained in Step 1. "x-cms-workspace": "<workspace>" # Replace <workspace> with your workspace name. } # Report data over HTTP. span_processor = BatchSpanProcessor(OTLPSpanHttpExporter( endpoint="<endpoint>", # Replace <endpoint> with the endpoint information obtained in Step 1. headers=headers )) trace_provider = TracerProvider(resource=resource, active_span_processor=span_processor) trace.set_tracer_provider(trace_provider) if __name__ == '__main__': init_opentelemetry() outer_method()Start the application.
python main.py
View monitoring data
Log on to the Cloud Monitor 2.0 console, and select a workspace. In the left navigation pane, choose .
On the Application List page, click the name of the target application to view its monitoring details. For more information, see Application Monitoring.