从Azure Python函数应用添加自定义属性与指标的问题求助
问题排查与解决方案
一、customDimensions/Properties未显示的排查思路
- 替换标准logging为func.logger
Azure Functions Python运行时提供的func.logger已与Application Insights深度集成,能正确识别并传递自定义维度。将代码中的logging调用替换为func.logger,调整自定义维度的传递方式:
import datetime 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: func.logger.info('Python timer trigger function executed.') # 直接传递自定义维度字典 func.logger.warning('Python timer trigger function executed.', extra={'key_1': 'value_1', 'key_2': 'value_2'})
也可保留custom_dimensions键的格式:
func.logger.warning('Python timer trigger function executed.', extra={'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}})
- 检查host.json日志配置
确保host.json中开启了Application Insights日志收集,并设置了正确的日志级别:
{ "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" }, "enableLiveMetrics": true, "enableDependencyTracking": true }, "logLevel": { "default": "Warning", "Function.timer_trigger": "Warning" } } }
若采样率过低可能导致日志被过滤,可临时设置samplingSettings.isEnabled为false测试。
验证环境变量配置
确认local.settings.json(本地运行)或函数应用的环境变量中,APPINSIGHTS_INSTRUMENTATION_KEY已正确设置,且函数运行时能读取到该值。直接查询Application Insights日志
在Application Insights的日志查询界面执行以下语句,查看自定义维度是否实际存在(默认视图可能不显示所有维度):
traces | where message contains "Python timer trigger function executed." | project message, customDimensions
二、指标日志的配置选项(对标C#)
Python中实现类似C#的指标日志功能,可通过以下方式:
- 使用Application Insights SDK手动发送指标
安装applicationinsights包后,通过TelemetryClient直接发送自定义指标,对应C#中TelemetryClient.TrackMetric的用法:
import os from applicationinsights import TelemetryClient 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: tc = TelemetryClient(os.environ['APPINSIGHTS_INSTRUMENTATION_KEY']) # 发送数值型指标 tc.track_metric('TimerExecutionCount', 1) # 发送带维度的指标 tc.track_metric('ProcessingTime', 250, properties={'Environment': 'Production'}) tc.flush()
- OpenTelemetry集成(对标C#自动指标收集)
通过配置OpenTelemetry实现自动或自定义指标收集,对应C#中通过配置启用指标的方式:
- 安装依赖包:
azure-monitor-opentelemetry-exporter、opentelemetry-instrumentation-azure-functions - 在host.json中配置OpenTelemetry:
{ "logging": { "applicationInsights": { "opentelemetry": { "isEnabled": true } } } }
- 代码中手动添加自定义指标:
from opentelemetry import metrics from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader from azure.monitor.opentelemetry.exporter import AzureMonitorMetricExporter import os # 初始化指标提供者 exporter = AzureMonitorMetricExporter(connection_string=os.environ['APPLICATIONINSIGHTS_CONNECTION_STRING']) reader = PeriodicExportingMetricReader(exporter) provider = MeterProvider(metric_readers=[reader]) metrics.set_meter_provider(provider) meter = metrics.get_meter("timer-function-meter") # 创建计数器指标 execution_counter = meter.create_counter("timer_executions", description="Number of timer trigger executions") @app.timer_trigger(...) def timer_trigger(myTimer: func.TimerRequest) -> None: execution_counter.add(1, {"environment": "production"})
内容的提问来源于stack exchange,提问作者Thomas Segato
相关产品推荐
相关产品推荐

