你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 Python 的 Azure Core 跟踪 OpenTelemetry 客户端库 - 版本 1.0.0b9

入门

安装包

使用 pip 安装适用于 Python 的 opentelemetry python:

pip install azure-core-tracing-opentelemetry

现在,可以像往常一样将 opentelemetry for Python 与任何与 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 Monitor 导出器的示例,但你可以使用任何导出程序 (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 机器人将自动确定你是否需要提供 CLA,并相应地修饰 PR(例如标签、注释)。 直接按机器人提供的说明操作。 只需使用 CLA 对所有存储库执行一次这样的操作。

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,或如果有任何其他问题或意见,请与 联系。