您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.
为 Python 应用程序设置 Azure MonitorSet up Azure Monitor for your Python application
09/24/2020
本文内容
通过与 OpenCensus 集成,Azure Monitor 支持对 Python 应用程序进行分布式跟踪、指标收集和日志记录。Azure Monitor supports distributed tracing, metric collection, and logging of Python applications through integration with OpenCensus. 本文将指导你完成为 Python 设置 OpenCensus 并将监视数据发送到 Azure Monitor 的过程。This article walks you through the process of setting up OpenCensus for Python and sending your monitoring data to Azure Monitor.
先决条件Prerequisites
Azure 订阅。An Azure subscription. 如果没有 Azure 订阅,请在开始之前创建一个免费帐户。If you don't have an Azure subscription, create a free account before you begin.
Python 安装。Python installation. 本文使用 Python 3.7.0,但其他版本可能会使用细微的更改。This article uses Python 3.7.0, although other versions will likely work with minor changes. SDK 仅支持 Python 版本2.7 和 v2.0 3.7。The SDK only supports Python v2.7 and v3.4-v3.7.
创建 Application Insights 资源。Create an Application Insights resource. 你将为你的资源分配自己的检测密钥 (ikey) 。You'll be assigned your own instrumentation key (ikey) for your resource.
检测适用于 Azure Monitor 的 OpenCensus Python SDKInstrument with OpenCensus Python SDK for Azure Monitor
安装 OpenCensus Azure Monitor 导出程序:Install the OpenCensus Azure Monitor exporters:
python -m pip install opencensus-ext-azure
备注
python -m pip install opencensus-ext-azure 命令假定你已为 Python 安装设置了 PATH 环境变量。The python -m pip install opencensus-ext-azure command assumes that you have a PATH environment variable set for your Python installation. 如果尚未配置此变量,则需要提供 Python 可执行文件所在位置的完整目录路径。If you haven't configured this variable, you need to give the full directory path to where your Python executable is located. 结果为如下所示的命令:C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe -m pip install opencensus-ext-azure。The result is a command like this: C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe -m pip install opencensus-ext-azure.
SDK 使用三个 Azure Monitor 导出程序将不同类型的遥测发送到 Azure Monitor。The SDK uses three Azure Monitor exporters to send different types of telemetry to Azure Monitor. 它们是跟踪、指标和日志。They're trace, metrics, and logs. 有关这些遥测类型的详细信息,请参阅数据平台概述。For more information on these telemetry types, see the data platform overview. 按照以下说明通过三个导出程序发送这些遥测类型。Use the following instructions to send these telemetry types via the three exporters.
遥测类型映射Telemetry type mappings
OpenCensus 提供的导出程序映射到 Azure Monitor 中看到的遥测类型。Here are the exporters that OpenCensus provides mapped to the types of telemetry that you see in Azure Monitor.
可观察性的支柱Pillar of observability
Azure Monitor 中的遥测类型Telemetry type in Azure Monitor
说明Explanation
日志Logs
跟踪,异常,customEventsTraces, exceptions, customEvents
日志遥测,异常遥测,事件遥测Log telemetry, exception telemetry, event telemetry
指标Metrics
customMetrics、performanceCounterscustomMetrics, performanceCounters
自定义指标性能计数器Custom metrics performance counters
跟踪Tracing
请求依赖项Requests dependencies
传入请求,传出请求Incoming requests, outgoing requests
日志Logs
首先,让我们生成一些本地日志数据。First, let's generate some local log data.
import logging
logger = logging.getLogger(__name__)
def valuePrompt():
line = input("Enter a value: ")
logger.warning(line)
def main():
while True:
valuePrompt()
if __name__ == "__main__":
main()
代码持续要求输入值。The code continuously asks for a value to be entered. 对于输入的每个值,将发出一个日志条目。A log entry is emitted for every entered value.
Enter a value: 24
24
Enter a value: 55
55
Enter a value: 123
123
Enter a value: 90
90
尽管输入值有助于演示,但最终我们希望向 Azure Monitor 发出日志数据。Although entering values is helpful for demonstration purposes, ultimately we want to emit the log data to Azure Monitor. 将连接字符串直接传递到导出程序。Pass your connection string directly into the exporter. 或者,可以在环境变量中指定它 APPLICATIONINSIGHTS_CONNECTION_STRING 。Or, you can specify it in an environment variable, APPLICATIONINSIGHTS_CONNECTION_STRING. 根据以下代码示例,修改上一步中的代码:Modify your code from the previous step based on the following code sample:
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
logger = logging.getLogger(__name__)
# TODO: replace the all-zero GUID with your instrumentation key.
logger.addHandler(AzureLogHandler(
connection_string='InstrumentationKey=00000000-0000-0000-0000-000000000000')
)
def valuePrompt():
line = input("Enter a value: ")
logger.warning(line)
def main():
while True:
valuePrompt()
if __name__ == "__main__":
main()
导出程序会将日志数据发送到 Azure Monitor。The exporter sends log data to Azure Monitor. 可在 traces 下找到数据。You can find the data under traces.
备注
在此上下文中, traces 与不同 tracing 。In this context, traces isn't the same as tracing. 此处, traces 是指在使用时将在 Azure Monitor 中看到的遥测类型 AzureLogHandler 。Here, traces refers to the type of