以OpenTelemetry方式接入
更新時間 2024-11-12 10:34:38
最近更新時間: 2024-11-12 10:34:38
分享文章
在監控Python應用之前,您需要通過客戶端將應用數據上報至APM服務端。本文介紹如何通過OpenTelemetry Python SDK上報Python應用數據。
前提條件
完成vpce接入。
python版本不低于3.8。
接入步驟
1.下載所需包。
pip?install?flask
pip?install?requests
pip?install?opentelemetry-distro?opentelemetry-exporter-otlp
opentelemetry-bootstrap?-a?install
2.查看接入點信息。
應用列表的接入指引會根據您所在資源池提供“通過 HTTP 上報數據”和“通過 gRPC 上報數據”的ENDPOINT(天翼云vpc網絡接入點)、鑒權TOKEN信息。
3.示例代碼。
from?flask?import?Flask
import?requests
backend_addr?=?'//example.com/'
app?=?Flask(__name__)
@app.route('/hello')
def?index():
????r?=?requests.get(backend_addr)
????return?r.text
app.run(host='0.0.0.0',?port=8070)
4.添加運行參數。
- 請將命令中的token和endpoint替換成第2步中獲取的接入點信息。
使用http方式上報的啟動命令。
opentelemetry-instrument?--traces_exporter?otlp_proto_http,console?--metrics_exporter?none?--service_name?<service_name>?--resource_attributes?host.name=<host_name>?--exporter_otlp_endpoint?<endpoint>?--exporter_otlp_headers?x-ctg-authorization=<token>?python?app.py
使用grpc方式上報的啟動命令。
opentelemetry-instrument?--traces_exporter?otlp_proto_grpc,console?--metrics_exporter?none?--service_name?<service_name>?--resource_attributes?host.name=<host_name>?--exporter_otlp_endpoint?<endpoint>?--exporter_otlp_headers?x-ctg-authorization=<token>?python?app.py
5.接入驗證。
啟動Python應用后,通過8070端口訪問對應的接口,在APM控制臺應用列表將展示接入的應用。
//localhost:8070/hello