Report Python application data using OpenTelemetry
Instrument your Python application with OpenTelemetry and report trace data to Cloud Monitor 2.0. You can then view application topology, call traces, abnormal transactions, slow transactions, and SQL analysis. OpenTelemetry supports both automatic and manual instrumentation through its Python agent and SDK.
We recommend using the Alibaba Cloud proprietary Python probe.
Compared to open-source implementations, the Alibaba Cloud Python probe provides observability into common large language model (LLM) frameworks, such as llama-index, dify, langchain, openai, and Qwen. It also provides richer metrics, traces, and continuous profiling data. For more information, see Manually install the Python probe.
Prerequisites
Python 3.7 or later.
Background information
OpenTelemetry provides automatic instrumentation plugins that create spans for common frameworks. The following table lists the supported frameworks. For a complete list, see the official OpenTelemetry documentation.
Version ~= V.N means ≥ V.N and == V.*. For example, aiohttp ~= 3.0 requires the aiohttp version to be 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 Integration Protocol Type to OpenTelemetry.
-
In the Parameter Settings section, click Get to the right of LicenseKey. Then, select the Instrumentation Method, Connection Method, and Reporting Method, and enter the Application Name, Version, and Deployment Environment.
The corresponding integration code is generated at the bottom of the page based on your configuration, including the endpoint and LicenseKey.
The Set up dependencies section at the bottom of the page displays the prerequisites (Python 3.7 or later),
pipinstallation commands (pip install opentelemetry-distro opentelemetry-exporter-otlpandopentelemetry-bootstrap -a install), and theexportcommands for required environment variables. The values forOTEL_EXPORTER_OTLP_TRACES_ENDPOINT,OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, andOTEL_EXPORTER_OTLP_HEADERSare automatically generated based on your configuration. Copy these variables into your application's startup script.
Step 2: Set up dependencies
Automatic instrumentation (recommended)
-
Install the required packages.
pip install opentelemetry-distro opentelemetry-exporter-otlp opentelemetry-bootstrap -a installNoteThe
opentelemetry-bootstrap -a installcommand scans thesite-packagesfolder of the current Python environment. If a framework that supports OpenTelemetry automatic instrumentation is found, the corresponding plugin is automatically installed. For example, if Django is already installed, theopentelemetry-instrumentation-djangopackage is downloaded automatically. -
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 with the endpoint information obtained in Step 1. Replace
<your_run_command>with the startup command for your Python application. Examples:-
Standard application:
python app.py -
Flask application:
flask run -p 8000 -
Django application:
python manage.py runserver --noreload -
uWSGI application:
uwsgi --http :8000 --module app.wsgi -
Gunicorn 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
NoteDo not enable hot reloading (such as
--reloadorreload=True) with automatic instrumentation, as it causes OpenTelemetry instrumentation to fail. Django enables hot reloading by default in the development environment, so you must specify--noreloadexplicitly. Automatic instrumentation does not currently support multi-worker mode for Uvicorn applications. -
Manual instrumentation
-
Install the required packages.
pip install opentelemetry-api pip install opentelemetry-sdk pip install opentelemetry-exporter-otlp -
Create a Python demo application.
Create a file named
main.pyand add the following code: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. DEPLOYMENT_ENVIRONMENT: "<environment>", # Deployment environment. HOST_NAME: "${hostName}", # Replace ${hostName} with your hostname. "acs.cms.workspace": "<workspace>" # Replace 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 from 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 target Application Name to view its monitoring details. For more information, see Application Monitoring.