Azure/azure-functions-dotnet-worker

dotnet 8 - isolated model - httpTrigger does not generate Request object in Application Insights

Banchio opened this issue · 5 comments

Description

I'm using dotnet 8 isolated with Http trigger, when invoking an http triggered api I do not see a corresponding Request object in Application Insights.
I'm using IHostApplicationBuilder, the only line of code I was not able to port was this one:
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); in the ConfigureLogging of the previous model IHostBuilder.

Steps to reproduce

Please find below csproj, program.cs, SimpleFunction.cs, host.json and appsettings.json.
Traces, dependancies are working for every function. In this project I also have some durable orchestration and the v2 model is working effectively (I see request/dependencies).

csproj

   <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" />
    <PackageReference Include="Azure.Identity" Version="1.13.1" />
    <PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.AppConfiguration.Functions.Worker" Version="8.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.3.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Warmup" Version="4.0.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.OpenTelemetry" Version="1.0.0-preview1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
    <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.0" />
    <PackageReference Include="System.Net.Http" Version="4.3.4" />
    <PackageReference Include="System.Private.Uri" Version="4.3.2" />
    <PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
  </ItemGroup>

Program.cs

using DnsClient;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Data;

var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.Services
    .AddApplicationInsightsTelemetryWorkerService()
    .ConfigureFunctionsApplicationInsights();
//.AddHttpClient()
//.AddHealthChecks();

// full details for durable task host.json: https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-bindings?tabs=python-v2%2Cin-process%2C2x-durable-functions&pivots=programming-language-csharp#hostjson-settings

builder.Logging.Services.Configure<LoggerFilterOptions>(options =>
{
    // The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs.
    // Application Insights requires an explicit override.
    // Log levels can also be configured using appsettings.json.
    // For more information, see https://learn.microsoft.com/azure/azure-monitor/app/worker-service#ilogger-logs
    #pragma warning disable CS8600 // Copied from link https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=ihostapplicationbuilder%2Cwindows#start-up-and-configuration
    
    LoggerFilterRule defaultRule = options.Rules.FirstOrDefault(rule => rule.ProviderName
              == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
    
    #pragma warning restore CS8600
    if (defaultRule is not null)
    {
        options.Rules.Remove(defaultRule);
    }
    
});

// builder.Logging.Services.AddApplicationInsightsTelemetryProcessor<RenameCategoryNameToCategory>();

var host = builder.Build();

await host.RunAsync();

SimpleFunction.cs

public class SimpleFunction(ILogger<SimpleFunction> logger)
{
    [Function(nameof(SimpleFunction))]
    public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
    {
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine($"Trace: {logger.IsEnabled(LogLevel.Trace)}");
        stringBuilder.AppendLine($"Debug: {logger.IsEnabled(LogLevel.Debug)}");
        stringBuilder.AppendLine($"Info: {logger.IsEnabled(LogLevel.Information)}");
        stringBuilder.AppendLine($"Warning: {logger.IsEnabled(LogLevel.Warning)}");
        stringBuilder.AppendLine($"Error: {logger.IsEnabled(LogLevel.Error)}");
        stringBuilder.AppendLine($"Critical: {logger.IsEnabled(LogLevel.Critical)}");
        logger.LogError(stringBuilder.ToString());
        
        logger.LogInformation("MyMessage Info: SimpleFunction processed a request ok.");
        logger.LogDebug($"MyMessage Debug: SimpleFunction processed a request ok.");
        logger.LogTrace($"MyMessage Trace: SimpleFunction processed a request ok.");
        logger.LogWarning($"MyMessage Warning: SimpleFunction processed a request ok.");
        logger.LogError($"MyMessage Error: SimpleFunction processed a request ok.");
        logger.LogCritical($"MyMessage Critical: SimpleFunction processed a request ok.");
        
        return new OkObjectResult("Welcome to Azure Functions!");
    }
}

host.json

{
  "version": "2.0",
  "extensions": {
    "durableTask": {
      "tracing": {
        "distributedTracingEnabled": true,
        "Version": "V2",
        "traceInputsAndOutputs": true
      }
    }
  },
  "logging": {
    "LogLevel": {
      "default": "Information",
      "Function": "Information",
      "Host.Aggregator": "Trace",
      "Host.Results": "Information",
      "Function.HealthCheck": "Warning"
    },
    "applicationInsights": {
      "enableDependencyTracking": true,
      "dependencyTrackingOptions": {
        "enableSqlCommandTextInstrumentation": true
      },
      "samplingSettings": {
        "isEnabled": false,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    }
  }
}

appsettings.json

{
  "logging": {
    "LogLevel": {
      "default": "Information",
      "Function": "Information",
      "Host.Aggregator": "Trace",
      "Host.Results": "Information",
      "Function.HealthCheck": "Warning"
    },
    "applicationInsights": {
      "enableDependencyTracking": true,
      "dependencyTrackingOptions": {
        "enableSqlCommandTextInstrumentation": true
      },
      "samplingSettings": {
        "isEnabled": false,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    }
  }
}

Same behavior occurs when starting from a brand new template with the hello world http trigger. Steps are to create a new function project dotnet 8 isolated, uncomment two lines to enable app insights (and install the two nuget packages). When invoking http triggered function a request is not generated

Last comment (I hope :). By replacing .AddApplicationInsightsTelemetryWorkerService() with .AddApplicationInsightsTelemetry() (from package Microsoft.ApplicationInsights.AspNetCore) now http requests are tracked.
In this function app I also have some durable functions, curios thing is that now I have two items in the application map representing my azure function
Image
One (without any arrows) coming from durable function, with RoleInstance equal to pd1sdwk0007S6
The other one coming from http triggered functions, with roleinstance equal to fd44d9b4958f4c9906de94b125d0970e95d5665ce7ced9f7fca7a51da3c094ac.

Questions:

  • are there any known issues in using the application insights dotnet core package?
  • would request be generated from other function as well (service bus trigger and so on)?
  • maybe docs have to be updated?

thanks!

@Banchio can you try using Microsoft.ApplicationInsights.AspNetCore instead of the worker service one?

logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

this will already be done for you with the application builder, we now use all defaults from .net host

Thanks @jviau for looking into this. Using AddApplicationInsightsTelemetry() (from package Microsoft.ApplicationInsights.AspNetCore) http requests are tracked. Maybe an update to the docs is needed, if I can contribute just lmk. Closing this issue for now