Reporting Python Application Data Using OpenTelemetry
Huawei Cloud APM is compatible with OpenTelemetry and can directly receive the trace data reported using the OpenTelemetry SDK or Agent. This section describes how to use OpenTelemetry to connect a Python application to APM and report trace data.
Constraint
Python 3.9 or later must be used.
Environment Preparation
- Install Python:
sudo apt update sudo apt install python3 python3-pip python3-venv
- Verify the environment:
python3 --version pip3 --version
Demo
Implement a simple dice roller application using the Flask framework.
- Create a running environment:
mkdir demo cd demo python3 -m venv demo source ./demo/bin/activate
- Download dependencies:
pip install flask
- Compile the service code. Create an app.py file:
from random import randint from flask import Flask, request import logging app = Flask(__name__) logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @app.route("/rolldice") def roll_dice(): player = request.args.get('player', default=None, type=str) result = str(roll()) if player: logger.warning("%s is rolling the dice: %s", player, result) else: logger.warning("Anonymous player is rolling the dice: %s", result) return result def roll(): return randint(1, 6) - Run the startup command:
flask run -p 8080 & curl http://localhost:8080/rolldice
Auto Tracking
- Download dependencies:
pip install opentelemetry-distro pip install opentelemetry-exporter-otlp opentelemetry-bootstrap -a install
- Start the Python application:
opentelemetry-instrument \ --traces_exporter otlp \ --metrics_exporter none \ --service_name Application name.Component name.Environment name \ --resource_attributes host.name=hostName \ --exporter_otlp_endpoint Reporting address \ --exporter_otlp_headers Authentication=Authentication information \ flask run -p 8080 & curl http://localhost:8080/rolldice
- Trigger a request:
curl http://localhost:8080/rolldice

- Log in to the APM console.
- Click
on the left and choose Management & Governance > Application Performance Management. - In the navigation pane, choose Link Trace > Metrics.
- In the tree on the left, locate an environment and then go to the URL tab page to view the URL calling data.
- In the navigation pane, choose Link Trace > Tracing.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot