Note:
OpenTelemetry is a collection of tools, APIs, and SDKs for monitoring, generating, collecting, and exporting telemetry data (metrics, logs, and traces) to help users analyze the performance and behavior of the software. For more information about OpenTelemetry, see the OpenTelemetry official website. The OpenTelemetry community is active, with rapid technological changes, and widely compatible with mainstream programming languages, components, and frameworks, making its link-tracing capability highly popular for cloud-native microservices and container architectures.
This document will introduce how to connect Python applications using the OpenTelemetry-Python scheme provided by the community.
The OpenTelemetry-Python scheme provides automatic Event Tracking for commonly used dependency libraries and frameworks in the Python ecosystem, including Flask, Django, FastAPI, MySQL Connector, etc., enabling link information reporting without needing to modify the code. For other dependency libraries and frameworks that support automatic Event Tracking, see the complete list provided by the OpenTelemetry community. Prerequisites
This scheme supports Python 3.6 and above.
Demo
Required dependencies are as follows:
pip install flask
pip install mysql-connector-python
pip install redis
pip install requests
The demo code app.py provides 3 HTTP APIs through the Flask framework. Set up the corresponding MySQL and Redis services yourself or directly purchase Cloud Services.
from flask import Flask
import requests
import time
import mysql.connector
import redis
backend_addr = 'https://example.com/'
app = Flask(__name__)
@app.route('/')
def index():
start = time.time()
r = requests.get(backend_addr)
return r
@app.route('/mysql')
def func_rdb():
cnx = mysql.connector.connect(host='127.0.0.1', database="<DB-NAME>", user='<DB-USER>', password='<DB-PASSWORD>', auth_plugin='mysql_native_password')
cursor = cnx.cursor()
val = "null"
cursor.execute("select value from table_demo where id=1;")
val = cursor.fetchone()[0]
cursor.close()
cnx.close()
return "rdb res:" + val
@app.route("/redis")
def func_kvop():
client = redis.StrictRedis(host="localhost", port=6379)
val = "null"
val = client.get('foo').decode("utf8")
return "kv res:" + val
app.run(host='0.0.0.0', port=8080)
Preliminary steps: Get the connect point and Token.
1. Log in to the TCOP console. 2. In the left menu column, select Application Performance Management > Application monitoring, and click Application list > Access application.
3. In the Data access drawer frame that pops up on the right, click the Python language.
4. On the Access Python application page, select the region and Business System you want to connect.
5. Select Access protocol type as OpenTelemetry.
6. Reporting method Choose your desired reporting method, and obtain your Access Point and Token.
Note:
Private network reporting: Using this reporting method, your service needs to run in the Tencent Cloud VPC. Through VPC connecting directly, you can avoid the security risks of public network communication and save on reporting traffic overhead.
Public network reporting: If your service is deployed locally or in non-Tencent Cloud VPC, you can report data in this method. However, it involves security risks in public network communication and incurs reporting traffic fees.
Connecting Python Applications
Step 1: Install the required dependency packets.
pip install opentelemetry-instrumentation-redis
pip install opentelemetry-instrumentation-mysql
pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install
Step 2: Add runtime parameters.
Start the Python application with the following command:
opentelemetry-instrument \\
--traces_exporter otlp_proto_grpc \\
--metrics_exporter none \\
--service_name <serviceName> \\
--resource_attributes token=<token>,host.name=<hostName> \\
--exporter_otlp_endpoint <endpoint> \\
python3 app.py
The corresponding field descriptions are as follows:
<serviceName>
: Application name. Multiple application processes connecting with the same serviceName are displayed as multiple instances under the same application in APM. The application name can be up to 63 characters and can only contain lowercase letters, digits, and the separator (-), and it must start with a lowercase letter and end with a digit or lowercase letter.
<token>
: The business system Token obtained in the preliminary steps.
<hostName>
: The hostname of this instance, which is the unique identifier of the application instance. It can usually be set to the IP address of the application instance.
<endpoint>
: The connect point obtained in the preliminary steps.
The content below uses myService
as the application name, myToken
as the Business System Token, 192.168.0.10
as the hostname, and https://pl-demo.ap-guangzhou.apm.tencentcs.com:4317
as the connect point example, the complete startup command is:
opentelemetry-instrument \\
--traces_exporter otlp_proto_grpc \\
--metrics_exporter none \\
--service_name myService \\
--resource_attributes token=myToken,host.name=192.168.0.10 \\
--exporter_otlp_endpoint https://pl-demo.ap-guangzhou.apm.tencentcs.com:4317/ \\
python3 app.py
Connection Verification
After the Python application starts, access the corresponding API through port 8080, for example, https://localhost:8080/
. If there is normal traffic, the connected application is displayed in APM > Application monitoring > Application list, and the connected application instance will be displayed in APM > Application monitoring > Application details > Instance monitoring. Since there is a certain delay in the processing of observable data, if the application or instance is not found on the console after connection, wait for about 30 seconds. Django Application Precautions
If your application uses the Django framework, before you connect it through the OpenTelemetry-Python scheme, pay attention to the following items:
1. It is recommended to deploy the service using uWSGI. For deployment methods, see through uWSGI Hosting Django Application. Starting directly through the python command may cause reporting failures. 2. The introduction of OpenTelemetry-Python may result in Django applications no longer using the default configuration file. It is necessary to re-specify the configuration file through environment variables:
export DJANGO_SETTINGS_MODULE=mysite.settings
Custom Event Tracking (Optional)
When automatic Event Tracking does not meet your scenario, or you need to add business layer Event Tracking, you can see the content below and use the OpenTelemetry API to add custom Event Tracking. This document only shows the most basic custom Event Tracking method. The OpenTelemetry community offers more flexible custom Event Tracking. For specific methods of use, you can see the Python Custom Event Tracking Documentation provided by the OpenTelemetry community. from opentelemetry import trace
import requests
backend_addr = 'https://example.com/'
app = Flask(__name__)
@app.route('/')
def index():
r = requests.get(backend_addr)
slow()
return r
def slow():
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("child_span")
time.sleep(5)
return
Was this page helpful?