从 Azure Python Function App 添加自定义属性和指标

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

我有下面的功能。日志已成功添加到应用程序见解中,但自定义维度/属性却未添加到应用程序见解中。有什么指点吗?此外,记录器没有记录指标,您没有与 c# 中相同的选项吗?

import datetime
import logging
import azure.functions as func

app = func.FunctionApp()

@app.timer_trigger(schedule="0 0 10 * * *", arg_name="myTimer", run_on_startup=True, use_monitor=False) 
def timer_trigger(myTimer: func.TimerRequest) -> None:
    
     logging.info('Python timer trigger function executed.')

     properties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
     logging.warning('Python timer trigger function executed.', extra=properties)

enter image description here

python azure azure-functions azure-application-insights
1个回答
0
投票

函数主机默认日志记录不支持自定义维度的日志记录。为了能够在 appinsights 中查看具有自定义维度的日志,您必须在代码中手动使用 Azure 监视器 OpenTelemetry 发行版进行检测:https://learn.microsoft.com/en-us/azure/azure-monitor/应用程序/opentelemetry-enable?tabs=python.

可以在此处找到如何使用发行版在 Python 应用程序中记录自定义维度的示例:https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/ azure-monitor-opentelemetry/samples/logging/custom_properties.py#L17

请记住,发行版和函数主机中的日志记录库可能会发生冲突并产生重复的遥测数据。请使用此解决方法:https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry#logging-issues帮助解决此问题。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.