Microsoft Azure Monitor Opentelemetry Exporter Python客户端库
撤销此版本的原因
由于命名弃用此包。请使用azure-monitor-opentelemetry-exporter。
项目描述
Microsoft Azure Monitor Opentelemetry导出器
此导出器允许您使用OpenTelemetry SDK导出跟踪数据,并将遥测数据发送到用于Python应用程序的Azure Monitor。
源代码 | 包(PyPi) | API参考文档 | 产品文档 | 示例 | 变更日志
注意:这是
opentelemetry-azure-monitor-python
下一个主要版本的预览。
入门
安装包
使用
pip install microsoft-opentelemetry-exporter-azuremonitor --pre
先决条件
使用此包,您必须具备以下条件:
- Azure订阅 - 创建免费账户
- Azure Monitor - 如何使用应用程序洞察
- Opentelemetry SDK - Python的Opentelemetry SDK
- Python 3.5或更高版本 - 安装Python
客户端认证
与Azure Monitor导出器的交互从AzureMonitorSpanExporter
类的实例开始。您需要一个连接字符串
来实例化对象。请查看以下链接中的示例,了解如何使用连接字符串进行认证。
从连接字符串创建导出器
from microsoft.opentelemetry.exporter.azuremonitor import AzureMonitorSpanExporter
exporter = AzureMonitorSpanExporter(
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
)
关键概念
Azure Monitor导出器的关键概念包括:
-
Opentelemetry:Opentelemetry是一组库,用于收集和导出遥测数据(指标、日志和跟踪)以进行分析,以便了解您的软件的性能和行为。
-
Instrumentation:通过instrumentation,任何应用程序都可以直接调用opentelemetry API。使另一个库能够为OpenTelemetry可观察性提供支持的库称为Instrumentation Library。
-
Trace:Trace指的是分布式跟踪。它可以被认为是由Spans组成的定向无环图(DAG),其中Spans之间的边被定义为父/子关系。
-
Tracer Provider:为给定的instrumentation library提供
Tracer
。 -
Span Processor:一个span processor允许SDK的
Span
start和end方法调用的钩子。更多信息请见链接。 -
Sampling:Sampling是一种机制,通过减少收集并发送到后端的跟踪样本数量来控制OpenTelemetry引入的噪声和开销。
-
AzureMonitorSpanExporter:这是初始化以将跟踪相关的遥测数据发送到Azure Monitor的类。
-
Exporter Options:用于配置Azure导出器的选项。包括连接字符串、instrumentation_key、proxies、timeout等。
有关这些资源的更多信息,请参阅什么是Azure Monitor?。
示例
以下部分提供了几个代码片段,涵盖了包括以下在内的几个常见任务:
导出Hello World Trace
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
from microsoft.opentelemetry.exporter.azuremonitor import AzureMonitorSpanExporter
exporter = AzureMonitorSpanExporter(
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
)
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
with tracer.start_as_current_span("hello"):
print("Hello, World!")
修改Traces
- 您可以将回调函数传递给导出器,以在导出之前处理遥测数据。
- 如果不想导出此envelope,您的回调函数可以返回False。
- 您的回调函数必须接受envelope数据类型作为其参数。
- 您可以在envelopes中查看Azure Monitor数据类型的模式。
- AzureMonitorSpanExporter处理Data数据类型。
from microsoft.opentelemetry.exporter.azuremonitor import AzureMonitorSpanExporter
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
# Callback function to add os_type: linux to span properties
def callback_function(envelope):
envelope.data.baseData.properties['os_type'] = 'linux'
return True
exporter = AzureMonitorSpanExporter(
connection_string='InstrumentationKey=<your-ikey-here>'
)
# This line will modify telemetry
exporter.add_telemetry_processor(callback_function)
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
span_processor = BatchExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
with tracer.start_as_current_span('hello'):
print('Hello World!')
使用requests库进行仪表化
OpenTelemetry还支持多种仪表化,允许使用第三方库进行仪表化。
此示例展示了如何使用requests库进行仪表化。
- 使用pip install opentelemetry-instrumentation-requests安装requests集成包。
import os
import requests
from opentelemetry import trace
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
from microsoft.opentelemetry.exporter.azuremonitor import AzureMonitorSpanExporter
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
# This line causes your calls made with the requests library to be tracked.
RequestsInstrumentor().instrument()
span_processor = BatchExportSpanProcessor(
AzureMonitorSpanExporter(
connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING "]
)
)
trace.get_tracer_provider().add_span_processor(span_processor)
RequestsInstrumentor().instrument()
# This request will be traced
response = requests.get(url="https://azure.microsoft.com/")
故障排除
导出器抛出在Azure Core中定义的异常。
下一步
更多示例代码
请参阅samples目录中的更多示例,展示常见场景。
其他文档
有关Azure Monitor服务的更详细文档,请参阅docs.microsoft.com上的Azure Monitor文档。
有关Opentelemetry的详细概述,请访问他们的概述页面。
贡献
此项目欢迎贡献和建议。大多数贡献都需要您同意贡献者许可协议(CLA),声明您有权并实际上授予我们使用您贡献的权利。有关详细信息,请访问https://cla.microsoft.com。
当您提交拉取请求时,CLA机器人将自动确定您是否需要提供CLA,并适当装饰PR(例如,标签,注释)。只需遵循机器人提供的说明即可。您只需在整个使用我们的CLA的repo中进行一次此操作。
此项目采用了Microsoft Open Source Code of Conduct。有关更多信息,请参阅行为准则FAQ或通过opencode@microsoft.com联系以获取任何其他问题或评论。
项目详情
microsoft-opentelemetry-exporter-azuremonitor-1.0.0b1.zip的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 52a840e1c7edfce38d5141be13d163a97785927ea7aa60322474254923d4e353 |
|
MD5 | 0fd37fd6ae53460aa7e47eb3632d5baa |
|
BLAKE2b-256 | 1c5d647bb3c65dd9f6a9c18f92c5d855e9b24517202a0723cac2285f6a2c5e3a |
哈希值 for microsoft_opentelemetry_exporter_azuremonitor-1.0.0b1-py2.py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 07517f7961cd3a91d00777fa2f11c8398fc253d32c69ed33d378b7b01885a5a8 |
|
MD5 | 70a120e3d7c8a11bd37ed10def896ad5 |
|
BLAKE2b-256 | 5752ed59ff27a2f0831dc1d363c7c051b052050cefb4995999e763bd2bc0e0d0 |