export DJANGO_SETTINGS_MODULE=mysite.settings
gevent.monkey.patch_all() must be executed as soon as possible: gevent modifies blocking I/O operations in Python standard library through monkey patching to make them non-blocking, adapting to the concurrency model of coroutines. OpenTelemetry's automatic event tracking probe may also depend on or modify standard library behavior. If the order of monkey patching is improper, or if the OpenTelemetry probe loads specific modules before gevent completes patching, it may cause startup error RecursionError.from gevent import monkeymonkey.patch_all() # Execute as early as possible# then import other modules and OpenTelemetry-related librariesfrom flask import Flaskfrom opentelemetry import tracefrom opentelemetry.sdk.trace import TracerProvider# ... other OpenTelemetry configurations and application code
--lazy-apps parameter. It enables each worker to independently load the application, as shown below:[uwsgi]module = your_app:applicationmaster = trueprocesses = 4lazy-apps = true # Important: delay application loadingenable-threads = true
pip install flaskpip install mysql-connector-pythonpip install redispip install requests
from flask import Flaskimport requestsimport timeimport mysql.connectorimport redisbackend_addr = 'https://example.com/'app = Flask(__name__)# Accessing External Site@app.route('/')def index():start = time.time()r = requests.get(backend_addr)return r.text# Accessing Database@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# Accessing Redis@app.route("/redis")def func_kvop():client = redis.StrictRedis(host="localhost", port=6379)val = "null"val = client.get('foo').decode("utf8")return "kv res:" + valapp.run(host='0.0.0.0', port=8080)
pip install opentelemetry-instrumentation-redispip install opentelemetry-instrumentation-mysqlpip install opentelemetry-distro opentelemetry-exporter-otlpopentelemetry-bootstrap -a install
opentelemetry-instrument \\--traces_exporter otlp_proto_grpc \\--metrics_exporter none \\--logs_exporter none \\--service_name <serviceName> \\--resource_attributes token=<token>,host.name=<hostName> \\--exporter_otlp_endpoint <endpoint> \\python3 app.py
<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.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 \\--logs_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
https://localhost:8080/. In normal traffic cases, the connected application will be displayed in APM > Application List. Click Application name/ID to enter the application details page, then select Instance Analysis to view the connected application instance. 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.from opentelemetry import traceimport requestsfrom flask import Flaskimport timebackend_addr = 'https://example.com/'app = Flask(__name__)@app.route('/')def index():r = requests.get(backend_addr) # For external requests initiated by requests.get(), OpenTelemetry-Python will perform automatic Event Tracking.slow() # Call a custom functionreturn r.textdef slow():tracer = trace.get_tracer(__name__)# Custom functions are not within the automatic Event Tracking range of OpenTelemetry-Python; therefore, add a custom Event Tracking. with tracer.start_as_current_span("child_span") time.sleep(5) return
--traces_exporter=console,otlp_proto_grpc to print link data to the console. If spans and traces are output in the console but no data appears in APM, check whether the endpoint and token are correct. If there is no output in the console, verify whether the framework or component used by the application is in the complete list supported by OpenTelemetry. If the specific issue cannot be identified, you may submit a ticket to us.Feedback