排查 Python 中的 OpenTelemetry 问题

本文讨论如何在 Python 中排查 OpenTelemetry 问题。

故障排除清单

启用诊断日志记录

Microsoft Azure Monitor 导出程序使用 Python 标准日志记录库执行其内部日志记录。 针对不规则活动,OpenTelemetry API 和 Azure Monitor 导出程序日志获配严重性级别 WARNINGERRORINFO 严重性级别用于常规活动或成功活动。

默认情况下,Python 日志记录库将严重性级别设置为 WARNING。 因此,必须更改严重性级别才能查看此严重性设置下的日志。 以下示例代码演示如何将所有严重性级别的日志输出到控制台和文件:

...
import logging

logging.basicConfig(format = "%(asctime)s:%(levelname)s:%(message)s", level = logging.DEBUG)

logger = logging.getLogger(__name__)
file = logging.FileHandler("example.log")
stream = logging.StreamHandler()
logger.addHandler(file)
logger.addHandler(stream)
...

测试应用程序主机与引入服务之间的连接性

Application Insights SDK 和代理发送遥测,在引入终结点将其作为 REST 调用引入。 请使用 PowerShell 中的 cURL 命令或原始 REST 请求,测试从 Web 服务器或应用程序主计算机到引入服务终结点的连接。 有关更多信息,请参阅排查 Azure Monitor Application Insights 中缺失应用程序遥测的问题

避免重复遥测

如果创建多个处理器或导出程序实例,则通常会造成重复遥测。 请确保每次只针对每个遥测要素(日志、指标和分布式跟踪)运行一个导出程序和处理器。

以下部分介绍可能导致重复遥测的情况。

Azure Functions 中的重复跟踪日志

如果在 Application Insights 中看到每个跟踪日志的一对条目,则可能已启用以下类型的日志记录检测:

  • Azure Functions 中的本机日志记录检测
  • 分发中的 azure-monitor-opentelemetry 日志记录检测

若要防止重复,可以禁用分发的日志记录,但在 Azure Functions 中保留本机日志记录检测。 为此,请将 OTEL_LOGS_EXPORTER 环境变量设置为 None

“始终可用的”Azure Functions 中的重复遥测数据

如果 Azure Functions 中的“始终可用”设置为“启用”,则 Azure Functions 会在每次运行完成后在后台保持运行一些进程。 例如,假设你有一个每次调用 configure_azure_monitor 的五分钟计时器函数。 20 分钟后,可能会有四个同时运行的指标导出程序。 这种情况可能是重复指标遥测数据的来源。

在这种情况下,请将“始终可用”设置为“关闭”,或尝试在每个 configure_azure_monitor 调用之间手动关闭提供程序。 若要关闭每个提供程序,请针对每个当前计量、跟踪器和记录器提供程序运行关闭调用,如以下代码所示:

get_meter_provider().shutdown()
get_tracer_provider().shutdown()
get_logger_provider().shutdown()

Azure 工作簿和 Jupyter Notebook

Azure 工作簿和 Jupyter Notebook 可能会使导出程序进程在后台运行。 若要防止重复遥测,请在调用更多 configure_azure_monitor 之前清除缓存。

缺少来自 FastAPI 或 Flask 应用的请求遥测数据

如果缺少请求表数据,但没有其他类别的数据,则可能无法正确检测 HTTP 框架。 如果不正确构建import声明,则使用适用于 PythonAzure Monitor OpenTelemetry 发行版客户端库的 FastAPI 和 Flask 应用中可能会出现此问题。 在调用 configure_azure_monitor 函数来检测 FastAPI 和 Flask 库之前,可能会分别导入 fastapi.FastAPIflask.Flask。 例如,以下代码未成功检测 FastAPI 和 Flask 应用:

# FastAPI

from azure.monitor.opentelemetry import configure_azure_monitor
from fastapi import FastAPI

configure_azure_monitor()

app = FastAPI()
# Flask

from azure.monitor.opentelemetry import configure_azure_monitor
from flask import Flask

configure_azure_monitor()

app = Flask(__name__)

相反,我们建议将 fastapiflask 模块作为一个整体导入,然后在访问 fastapi.FastAPIflask.Flask 之前调用 configure_azure_monitor 来将 OpenTelemetry 配置为使用 Azure Monitor:

# FastAPI

from azure.monitor.opentelemetry import configure_azure_monitor
import fastapi

configure_azure_monitor()

app = fastapi.FastAPI(__name__)
# Flask

from azure.monitor.opentelemetry import configure_azure_monitor
import flask

configure_azure_monitor()

app = flask.Flask(__name__)

或者,可以在导入 fastapi.FastAPIflask.Flask 之前调用 configure_azure_monitor

# FastAPI

from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

from fastapi import FastAPI

app = FastAPI(__name__)
# Flask

from azure.monitor.opentelemetry import configure_azure_monitor

configure_azure_monitor()

from flask import Flask

app = Flask(__name__)

联系我们寻求帮助

如果你有任何疑问或需要帮助,请创建支持请求联系 Azure 社区支持。 你还可以将产品反馈提交到 Azure 反馈社区