全部產品
Search
文件中心

Application Real-Time Monitoring Service:通過SkyWalking上報Python應用資料

更新時間:Aug 08, 2024

通過SkyWalking為應用埋點並上報鏈路資料至Managed Service for OpenTelemetry後,Managed Service for OpenTelemetry即可開始監控應用,您可以查看應用拓撲、調用鏈路、異常事務、慢事務和SQL分析等一系列監控資料。本文介紹如何使用SkyWalking Python Agent進行自動埋點並上報應用資料。

前提條件

  • 下載Apache-skywalking的Python Agent(建議下載最新版本)。

  • 在Python專案中引入Skywalking-python Agent。

  • 擷取存取點資訊

    1. 登入ARMS控制台,在左側導覽列單擊接入中心

    2. 服務端應用地區單擊SkyWalking卡片。

    3. 在彈出的SkyWalking面板中選擇資料需要上報的地區。

      說明

      初次接入的地區將會自動進行資源初始化。

    4. 選擇串連方式,然後複製存取點資訊。

      若您的服務部署在阿里雲上,且所屬地區與選擇的接入地區一致,推薦使用阿里雲內網方式,否則選擇公網方式。

      image.png

背景資訊

SkyWalking是一款廣受歡迎的國產APM(Application Performance Monitoring,應用效能監控)產品,主要針對微服務、Cloud Native和容器化(Docker、Kubernetes、Mesos)架構的應用。SkyWalking的核心是一個分布式追蹤系統。

Skywalking-python是SkyWalking的Python Agent官方庫,可以通過接入Skywalking-python實現對Python應用的監控,Skywalking-python支援Kafka、HTTP、AIOHTTP、Redis、WebSockets等多種第三方庫的自動埋點。

展開查看支援監控的第三方庫

Python版本 - 庫版本

外掛程式名

aiohttp

Python≥3.7 - 3.7.*

sw_aiohttp

aioredis

Python≥3.7 - 2.0.*

sw_aioredis

aiormq

Python≥3.7 - 6.3、6.4

sw_aiormq

amqp

Python≥3.7 - 2.6.1

sw_amqp

asyncpg

Python≥3.7 - 0.25.0

sw_asyncpg

bottle

Python≥3.7 - 0.12.23

sw_bottle

celery

Python≥3.7 - 5.1

sw_celery

confluent_kafka

Python≥3.7 - 1.5.0、1.7.0、1.8.2

sw_confluent_kafka

django

Python≥3.7 - 3.2

sw_django

elasticsearch

Python ≥3.7 - 7.13、7.14、7.15

sw_elasticsearch

hug

Python ≥3.11 - 暫不支援

Python ≥3.10 - 2.5、2.6

Python ≥3.7 - 2.4.1、2.5、2.6

sw_falcon

fastapi

Python ≥3.7 - 0.89.*、0.88.*

sw_fastapi

flask

Python ≥3.7 - 2.0

sw_flask

happybase

Python ≥3.7 - 1.2.0

sw_happybase

http_server

Python ≥3.7 - *

sw_http_server

werkzeug

Python ≥3.7 - 1.0.1、2.0

sw_http_server

httpx

Python ≥3.7 - 0.23.*、0.22.*

sw_httpx

kafka-python

Python ≥3.7 - 2.0

sw_kafka

loguru

Python ≥3.7 - 0.6.0、0.7.0

sw_loguru

mysqlclient

Python ≥3.7 - 2.1.*

sw_mysqlclient

neo4j

Python ≥3.7 - 5.*

sw_neo4j

psycopg[binary]

Python ≥3.11 - 3.1.*

Python ≥3.7 - 3.0.18、3.1.*

sw_psycopg

psycopg2-binary

Python ≥3.10 - 暫不支援

Python ≥3.7 - 2.9

sw_psycopg2

pymongo

Python ≥3.7 - 3.11.*

sw_pymongo

pymysql

Python ≥3.7 - 1.0

sw_pymysql

pyramid

Python ≥3.7 - 1.10、2.0

sw_pyramid

pika

Python ≥3.7 - 1.2

sw_rabbitmq

redis

Python ≥3.7 - 3.5.*、4.5.1

sw_redis

requests

Python ≥3.7 - 2.26、2.25

sw_requests

sanic

Python ≥3.10 - 暫不支援

Python ≥3.7 - 20.12

sw_sanic

tornado

Python ≥3.7 - 6.0、6.1

sw_tornado

urllib3

Python ≥3.7 - 1.26、1.25

sw_urllib3

urllib_request

Python ≥3.7 - *

sw_urllib_request

websockets

Python ≥3.7 - 10.3、10.4

sw_websockets

樣本Demo

樣本Demo倉庫地址:SkyWalking Demo

該Demo是一個簡單的基於Flask架構的請求路由轉寄和操作MySQL資料庫的樣本,SkyWalking Agent對其監控並上報資料。

用SkyWalking為Python應用自動埋點

您可以在Python專案檔代碼裡直接配置SkyWalking Python Agent參數或在環境變數中配置SkyWalking Python Agent參數。

在代碼中配置參數

在Python專案檔中修改config.init參數,樣本Demo中對應為proxy/TestProxy.pycontroller/TestController.py檔案。

from skywalking import config
config.init(ConfigurationName = ConfigurationValue)

將參數配置到環境變數

在環境變數檔案中添加以下內容,並重新整理使其生效。

export SW_AGENT_ConfigurationName=ConfigurationValue
說明

如果是Docker容器環境,可以在docker-compose.yaml檔案的environment選項下面配置環境變數。

  1. 配置存取點和令牌。

    1. 在Python檔案中引入Skywalking。

      from skywalking import agent, config
    2. 配置存取點和鑒權令牌。

      請將<endpoint><auth-token>分別替換成前提條件中擷取的存取點資訊和鑒權令牌。

      config.init(agent_collector_backend_services='<endpoint>',
                  agent_authentication='<auth-token>')
  2. 配置Service Name作為應用標識。

    config.init(agent_name='<service name>')
  3. 選擇資料上報協議,SkyWalking支援gRPC上報。

    ### 可選擇的上報協議有'grpc'。
    config.init(agent_protocol='<protocol>')
  4. 根據需求配置其他選擇性參數。參數詳情請參見Apache SkyWalking官方文檔

  5. 重新啟動應用。

常見問題

  • gRPC上報資料出現Method not found: skywalking.v3.LogReportService/collect錯誤。image.png

    config.init中設定agent_log_reporter_activeFalse

    config.init(agent_log_reporter_active=False)
  • gRPC上報資料出現Method not found: skywalking.v3.MeterReportService/collect錯誤。image.png控制台暫不支援metric資料上報,您可以在config.init中設定agent_meter_reporter_activeFalse

    config.init(agent_meter_reporter_active=False)

相關文檔

SkyWalking官網