You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8独立模型Azure Function App集成OpenTelemetry异常求助

问题分析与解决方案

问题描述

基于.NET 8 Azure Function独立工作者模型开发的应用,集成OpenTelemetry后出现以下异常:

  • 未生成任何Request类型日志,仅存在trace、dependency、exception类型条目
  • 所有trace、dependency、exception条目均缺失operation_Name字段

原因分析与修复步骤

1. Request日志缺失的直接原因

问题根源:host.json的Application Insights采样配置直接排除了Request和Exception类型:

"samplingSettings": {
  "isEnabled": true,
  "maxTelemetryItemsPerSecond": 20,
  "excludedTypes": "Request;Exception" // 此处明确排除了Request类型
}

修复操作:移除excludedTypes中的Request;Exception,或根据实际需求调整采样规则:

"samplingSettings": {
  "isEnabled": true,
  "maxTelemetryItemsPerSecond": 20
}

2. operation_Name缺失的核心原因及修复

(1)重复配置OpenTelemetry导致上下文混乱

代码中两次调用services.AddOpenTelemetry(),会覆盖或干扰Function Worker默认的遥测上下文关联逻辑:

// 第一次初始化Function Worker默认遥测
services.AddOpenTelemetry().UseFunctionsWorkerDefaults().UseAzureMonitor();
// 第二次重复创建配置,破坏原有上下文关联
services.AddOpenTelemetry(appConfig.AppInsightsConnString, appName);

修复:合并为单一配置,保留UseFunctionsWorkerDefaults()(这是生成Function请求上下文和operation_Name的核心),在此基础上扩展自定义Instrumentation:

private static void ConfigureOpenTelemetry(IServiceCollection services, IConfiguration config, string appName)
{
    var appConfig = services.BindValidateReturn<AppCloudServiceOptions>(config);

    services.AddLogging(loggingBuilder =>
    {
        loggingBuilder.ClearProviders();
        loggingBuilder.AddOpenTelemetry(logging =>
        {
            logging.IncludeFormattedMessage = true;
            logging.IncludeScopes = true;
            logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(appName));
        });
    });

    // 合并配置,基于Function Worker默认遥测扩展
    services.AddOpenTelemetry()
        .UseFunctionsWorkerDefaults() // 必须保留,负责处理Function的遥测上下文
        .ConfigureResource(resource => resource.AddService(appName))
        .UseAzureMonitor(options =>
        {
            options.ConnectionString = appConfig.AppInsightsConnString;
        })
        .WithTracing(tracing => tracing
            .AddHttpClientInstrumentation()
            .AddEntityFrameworkCoreInstrumentation()
            .AddRedisInstrumentation())
        .WithMetrics(metrics => metrics
            .AddHttpClientInstrumentation()
            .AddRuntimeInstrumentation());
}

(2)错误添加AspNetCore Instrumentation

Azure Function独立工作者模型不依赖AspNetCore框架,AddAspNetCoreInstrumentation()无法捕获Function的请求上下文,反而会干扰遥测关联逻辑,导致operation_Name无法正确填充。
修复:移除所有AddAspNetCoreInstrumentation()调用,依赖UseFunctionsWorkerDefaults()提供的Function专用遥测支持。

(3)日志未关联Trace上下文

确保日志配置中设置了正确的Resource,让OpenTelemetry日志提供者能继承Function的Trace上下文,这样日志条目才能关联到对应的operation_Name。

3. 额外验证点

  • 确认部署环境中APPINSIGHTS_CONNECTION_STRING配置与代码中使用的连接字符串一致
  • 检查Microsoft.Azure.Functions.Worker等核心包是否为最新稳定版,避免版本兼容问题
  • 本地测试时,确保local.settings.json中配置了正确的App Insights连接字符串

内容的提问来源于stack exchange,提问作者santosh kumar patro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 09:07:11