本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
遷移至 OpenTelemetry Python
本指南可協助您將 Python 應用程式從 X-Ray SDK 遷移至 OpenTelemetry 檢測。它涵蓋自動和手動檢測方法,並提供常見案例的程式碼範例。
零程式碼自動檢測解決方案
使用 X-Ray SDK,您必須修改應用程式程式碼以追蹤請求。OpenTelemetry 提供零程式碼的自動檢測解決方案來追蹤請求。使用 OpenTelemetry,您可以選擇使用零碼自動檢測解決方案來追蹤請求。
使用 OpenTelemetry 型自動檢測的零代碼
-
使用適用於 Python 的 AWS Distro for OpenTelemetry (ADOT) 自動檢測 – 如需適用於 Python 應用程式的自動檢測,請參閱使用適用於 OpenTelemetry Python 自動檢測的 AWS Distro 追蹤和指標
。 (選用) 您也可以 AWS 在使用 ADOT Python 自動檢測功能自動檢測應用程式時啟用 CloudWatch Application Signals,以監控目前的應用程式運作狀態,並根據業務目標追蹤長期應用程式效能。Application Signals 為您提供應用程式、服務和相依性的統一、以應用程式為中心的檢視,並協助您監控和分類應用程式運作狀態。
-
使用 OpenTelemetry Python 零碼自動檢測 – 如需使用 OpenTelemetry Python 自動檢測,請參閱 Python 零碼檢測
。
手動檢測您的應用程式
您可以使用 pip
命令手動檢測您的應用程式。
追蹤設定初始化
使用適用於 Python 的 ADOT 自動檢測
您可以使用適用於 Python 的 ADOT 自動檢測,為您的 Python 應用程式自動設定 OpenTelemetry。透過使用 ADOT 自動檢測,您不需要手動變更程式碼來追蹤傳入的請求,或追蹤 AWS SDK 或 HTTP 用戶端等程式庫。如需詳細資訊,請參閱使用 AWS Distro for OpenTelemetry Python Auto-Instrumentation 追蹤 和 指標
適用於 Python 的 ADOT 自動檢測支援:
-
透過環境變數進行 X-Ray 遠端取樣
export OTEL_TRACES_SAMPLER=xray
-
X-Ray 追蹤內容傳播 (預設為啟用)
-
資源偵測 (Amazon EC2、Amazon ECS 和 Amazon EKS 環境的資源偵測預設為啟用)
-
預設會啟用所有支援的 OpenTelemetry 檢測的自動程式庫檢測。您可以透過
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS
環境變數選擇性地停用 (預設為啟用) -
手動建立跨度
從 X-Ray 服務外掛程式到 OpenTelemetry AWS 資源提供者
X-Ray 開發套件提供外掛程式,您可以新增至 xray_recorder
,以從 Amazon EC2、Amazon ECS 和 Elastic Beanstalk 等託管服務擷取平台特定資訊。它類似於 OpenTelemetry 中將資訊擷取為資源屬性的資源提供者。有多個資源提供者可用於不同的 AWS 平台。
-
首先安裝 AWS 延伸套件,
pip install opentelemetry-sdk-extension-aws
-
設定所需的資源偵測器。下列範例示範如何在 OpenTelemetry SDK 中設定 Amazon EC2 資源提供者
from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.extension.aws.resource.ec2 import ( AwsEc2ResourceDetector, ) from opentelemetry.sdk.resources import get_aggregated_resources provider = TracerProvider( active_span_processor=span_processor, resource=get_aggregated_resources([ AwsEc2ResourceDetector(), ])) trace.set_tracer_provider(provider)
追蹤傳入的請求
AWS SDK 檢測
透過請求檢測傳出的 HTTP 呼叫
其他程式庫的檢測支援
您可以在支援的程式庫、架構、應用程式伺服器和 JVM 下找到 OpenTelemetry Python 支援的程式庫檢測的完整清單。 JVMs
或者,您可以搜尋 OpenTelemetry 登錄檔,了解 OpenTelemetry 是否支援檢測。請參閱 登錄
手動建立追蹤資料
您可以在 xray_recorder
Python 應用程式中使用 建立客群和子客群。如需詳細資訊,請參閱手動檢測 Python 程式碼。您也可以手動將註釋和中繼資料新增至追蹤資料。
使用 OpenTelemetry SDK 建立範圍
使用 start_as_current_span
API 啟動跨度並將其設定為建立跨度。如需建立跨度的範例,請參閱建立跨度SERVER
種類範圍會轉換為 X-Ray 區段,其他則轉換為 X-Ray 子區段。
from opentelemetry import trace from opentelemetry.trace import SpanKind import time tracer = trace.get_tracer("my.tracer.name") # Create a new span to track some work with tracer.start_as_current_span("parent", kind=SpanKind.SERVER) as parent_span: time.sleep(1) # Create a nested span to track nested work with tracer.start_as_current_span("child", kind=SpanKind.CLIENT) as child_span: time.sleep(2) # the nested span is closed when it's out of scope # Now the parent span is the current span again time.sleep(1) # This span is also closed when it goes out of scope
使用 OpenTelemetry SDK 將註釋和中繼資料新增至追蹤
X-Ray Python SDK 提供不同的 APIs,put_annotation
以及put_metadata
將註釋和中繼資料新增至追蹤。在 OpenTelemetry SDK 中,註釋和中繼資料只是跨度上的屬性,透過 set_attribute
API 新增。
您希望它們在追蹤上成為註釋的跨度屬性會新增到保留索引鍵下方aws.xray.annotations
,其值是註釋的索引鍵/值對清單。所有其他跨度屬性會成為轉換區段或子區段上的中繼資料。
此外,如果您使用 ADOT 收集器,您可以透過在收集器組態indexed_attributes
中指定 來設定哪些跨度屬性應該轉換為 X-Ray 註釋。
以下範例示範如何使用 OpenTelemetry SDK 將註釋和中繼資料新增至追蹤。
with tracer.start_as_current_span("parent", kind=SpanKind.SERVER) as parent_span: parent_span.set_attribute("TransactionId", "qwerty12345") parent_span.set_attribute("AccountId", "1234567890") # This will convert the TransactionId and AccountId to be searchable X-Ray annotations parent_span.set_attribute("aws.xray.annotations", ["TransactionId", "AccountId"]) with tracer.start_as_current_span("child", kind=SpanKind.CLIENT) as child_span: # The MicroTransactionId will be converted to X-Ray metadata for the child subsegment child_span.set_attribute("MicroTransactionId", "micro12345")
Lambda 檢測
若要在 X-Ray 上監控 lambda 函數,您可以啟用 X-Ray,並將適當的許可新增至函數叫用角色。此外,如果您要從函數追蹤下游請求,則會使用 X-Ray Python SDK 檢測程式碼。
使用適用於 X-Ray 的 OpenTelemetry,建議在 Application Signals 關閉的情況下使用 CloudWatch Application Signals lambda 層。這會自動檢測您的函數,並產生函數調用的範圍和函數的任何下游請求。除了追蹤之外,如果您有興趣使用 Application Signals 來監控函數的運作狀態,請參閱在 Lambda 上啟用應用程式。
-
從適用於 AWS OpenTelemetry ARN 的 Lambda Layer 尋找函數所需的 Lambda layer ARNs,並將其新增。
-
為您的函數設定下列環境變數。
-
AWS_LAMBDA_EXEC_WRAPPER=/opt/otel-instrument
– 這會載入函數的自動檢測 -
OTEL_AWS_APPLICATION_SIGNALS_ENABLED=false
– 這會停用 Application Signals 監控
-
使用 Lambda 檢測手動建立跨度
此外,您可以在函數內產生自訂跨度,以追蹤工作。您可以只使用 opentelemetry-api
套件搭配 Application Signals lambda layer 自動檢測來完成。
-
在函數中包含
opentelemetry-api
做為相依性 -
下列程式碼片段是產生自訂範圍的範例
from opentelemetry import trace # Get the tracer (auto‑configured by the Application Signals layer) tracer = trace.get_tracer(__name__) def handler(event, context): # This span is a child of the layer's root span with tracer.start_as_current_span("my-custom-span") as span: span.set_attribute("key1", "value1") span.add_event("custom-event", {"detail": "something happened"}) # Any logic you want to trace result = some_internal_logic() return { "statusCode": 200, "body": result }