OpenTelemetry 未从 Python 代码将带有异常详细信息的消息记录到 Application Insights

问题描述 投票:0回答:1

我正在将 python 3.10 项目从 OpenCensus 迁移到 OpenTelemetry,因为前者今年已被日落

对于标准

info
warning
error
等日志记录,日志可以正常工作,但我在日志记录
exceptions
方面遇到问题。

采用以下示例代码:

from azure.monitor.opentelemetry import configure_azure_monitor
import logging

configure_azure_monitor(
     connection_string="MY_APPINSIGHTS_CONNECTION_STRING",
)

try:
    logging.info("My INFO message")
    logging.warning("My WARNING message")
    logging.error("My ERROR message")
    raise Exception("My EXCEPTION message")
except Exception:
    logging.exception('My CUSTOM message')
    logging.error('My CUSTOM message', stack_info=True, exc_info=True)

引发异常之前的日志按预期工作。对于 except 块中的日志,应用程序洞察日志将使用记录中某处的“

我的自定义消息
”或至少“
我的自定义消息
”的 outerMessagedetails..message 创建。当我检查时,我看到的是以下内容:

exceptions
| order by timestamp desc 

我的自定义消息”无处可寻。是否可以设置配置选项以便获取自定义错误消息?请注意,我得到以下两行完全相同的日志:

logging.exception('My CUSTOM message')
logging.error('My CUSTOM message', stack_info=True, exc_info=True)

此行为与旧的 OpenCensus 行为不同,旧的 OpenCensus 行为确实将自定义消息与异常数据一起记录在记录中。

python azure logging azure-application-insights open-telemetry
1个回答
0
投票

使用 opentelemetry 的预期行为,您将得到如下引发的异常:

import logging
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter

from opentelemetry._logs import (
    get_logger_provider,
    set_logger_provider,
)
from opentelemetry.sdk._logs import (
    LoggerProvider,
    LoggingHandler,
)
set_logger_provider(LoggerProvider())
exporter = AzureMonitorLogExporter(
    connection_string="InstrumentationKey=d1079;IngestionEndpoint=https://centralindia-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralindia.livediagnostics.monitor.azure.com/"
)
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter))

rith_handler = LoggingHandler()
rith_logger = logging.getLogger(__name__)
rith_logger.addHandler(rith_handler)
rith_logger.setLevel(logging.INFO)

try:
    rith_logger.info("Rithwik INFO message")
    rith_logger.warning("Rithwik WARNING message")
    rith_logger.error("Rithwik ERROR message")
    raise Exception("Rithwik Exception")
except Exception as e:
    rith_logger.error(f'Rithwik custom error: {e}')
    

enter image description here

您在加注后还会收到一条错误消息,如下所示:

enter image description here

使用日志记录异常的自定义异常给出如下

exception
消息,而不是自定义消息:

import logging
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter

from opentelemetry._logs import (
    get_logger_provider,
    set_logger_provider,
)
from opentelemetry.sdk._logs import (
    LoggerProvider,
    LoggingHandler,
)
set_logger_provider(LoggerProvider())
exporter = AzureMonitorLogExporter(
    connection_string="InstrumentationKey=d19b79;IngestionEndpoint=https://centralindia-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralindia.livediagnostics.monitor.azure.com/"
)
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter))

rith_handler = LoggingHandler()
rith_logger = logging.getLogger(__name__)
rith_logger.addHandler(rith_handler)
rith_logger.setLevel(logging.INFO)

rith_logger.info("Rithwik INFO message")
rith_logger.warning("Rithwik WARNING message")
rith_logger.error("Rithwik ERROR message")
rith_logger.exception(f'Rithwik custom error: ')

enter image description here

要获取自定义消息,您必须使用 raise 。

© www.soinside.com 2019 - 2024. All rights reserved.