適用于 Python 的 Azure Core 追蹤 OpenTelemetry 用戶端程式庫 - 1.0.0b9 版
開始使用
安裝套件
使用 pip安裝適用于 Python 的 opentelemetry Python:
pip install azure-core-tracing-opentelemetry
現在,您可以像往常一樣使用適用于 Python 的 opentelemetry 與 azure 核心追蹤相容的任何 SDK。 這包括 (不完整的清單) 、azure-storage-blob、azure-keyvault-secrets、azure-eventhub 等。
重要概念
您不需要傳遞任何內容,SDK 會為您取得
這些行是唯一需要啟用追蹤的行
from azure.core.settings import settings from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan settings.tracing_implementation = OpenTelemetrySpan
範例
沒有要傳遞的明確內容,您只需建立一般的 opentelemetry 追蹤,並呼叫任何與 azure 核心追蹤相容的 SDK 程式碼。 這是使用 Azure 監視器匯出工具的範例,但您可以使用任何匯出工具 (Zipkin 等) 。
# Declare OpenTelemetry as enabled tracing plugin for Azure SDKs
from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
settings.tracing_implementation = OpenTelemetrySpan
# In the below example, we use a simple console exporter, uncomment these lines to use
# the Azure Monitor Exporter.
# Example of Azure Monitor exporter, but you can use anything OpenTelemetry supports
# from azure_monitor import AzureMonitorSpanExporter
# exporter = AzureMonitorSpanExporter(
# instrumentation_key="uuid of the instrumentation key (see your Azure Monitor account)"
# )
# Regular open telemetry usage from here, see https://github.com/open-telemetry/opentelemetry-python
# for details
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
# Simple console exporter
exporter = ConsoleSpanExporter()
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(exporter)
)
# Example with Storage SDKs
from azure.storage.blob import BlobServiceClient
with tracer.start_as_current_span(name="MyApplication"):
client = BlobServiceClient.from_connection_string('connectionstring')
client.create_container('mycontainer') # Call will be traced
您可以在套件中找到 Azure 匯出工具opentelemetry-azure-monitor-exporter
疑難排解
此用戶端會引發 Azure Core中定義的例外狀況。
下一步
如需 OpenTelemetry 設定的詳細資訊,請參閱 OpenTelemetry 網站
參與
此專案歡迎參與和提供建議。 大部分的參與都要求您同意「參與者授權合約 (CLA)」,宣告您有權且確實授與我們使用投稿的權利。 如需詳細資料,請前往 https://cla.microsoft.com 。
當您提交提取要求時,CLA Bot 會自動判斷您是否需要提供 CLA,並適當地裝飾 PR (例如標籤、註解)。 請遵循 bot 提供的指示。 您只需要使用我們的 CLA 在所有存放庫上執行此動作一次。
此專案採用 Microsoft Open Source Code of Conduct (Microsoft 開放原始碼管理辦法)。 如需詳細資訊,請參閱管理辦法常見問題集,如有任何其他問題或意見請連絡 opencode@microsoft.com。