Version 3.0.0 of Microsoft.ApplicationInsights.WorkerService is not compatible with the Azure Functions Worker as reported here and here. There will not be a fix according to this comment. Instead, migrating to OpenTelemetry-based monitoring is advised.
I've written the following instructions for myself to perform a (basic) migration to OpenTelemetry-based monitoring. They are based on:
- Use OpenTelemetry with Azure Functions
- Tutorial: Monitor Azure Functions with OpenTelemetry distributed tracing
- Azure-Samples/functions-quickstart-dotnet-azd-otel
These docs also contain considerations and other useful information, so they are worth a read.
If you have any feedback or additions to these instructions, feel free to drop a comment.
After migrating, check that all necessary logging is still showing up in Application Insights.
I'm using Storage Account Table Output binding in one of my functions. Before the migration, I could see logging in Application Insights that data was submitted to Table Storage. This logging no longer shows up. I haven't found a solution yet, other then using the TableClient directly instead of using an output binding. I requested a sample that works with output bindings here.
Review the NuGet packages under open-telemetry/opentelemetry-dotnet-contrib for additional packages and configuration that might be relevant to your scenario. Each package has a README with details.
If you have any dashboards, workbooks, alerts, etc. that rely on kusto queries to query logging from your Azure Functions, you'll have to rewrite these because events that are logged have different (custom) properties. I've included a sample at the end of this gist.
Sampling is not enabled by default in Application Insights OpenTelemetry distros, as stated in Sampling in Azure Monitor Application Insights with OpenTelemetry. You must explicitly enable and configure sampling to manage your telemetry volume. See Configure Azure Monitor OpenTelemetry - Enable Sampling for instructions.
-
Set telemetry mode to
OpenTelemetryin thehost.jsonfile of your project:{ "version": "2.0", "telemetryMode": "OpenTelemetry", ... } -
Make sure the app setting
APPLICATIONINSIGHTS_CONNECTION_STRINGis set in your Function App. -
Remove Application Insights packages:
dotnet remove package Microsoft.ApplicationInsights.WorkerService dotnet remove package Microsoft.Azure.Functions.Worker.ApplicationInsights -
Add OpenTelemetry related packages:
dotnet add package Microsoft.Azure.Functions.Worker.OpenTelemetry dotnet add package OpenTelemetry.Extensions.Hosting dotnet add package Azure.Monitor.OpenTelemetry.Exporter dotnet add package OpenTelemetry.Instrumentation.HttpThe first 3 packages are necessary for OpenTelemetry to work. The OpenTelemetry.Instrumentation.Http package can be used to instrument
System.Net.Http.HttpClientandSystem.Net.HttpWebRequest. -
Add the following usings to your
Program.cs:using Azure.Monitor.OpenTelemetry.Exporter; using Microsoft.Azure.Functions.Worker.OpenTelemetry; using OpenTelemetry.Trace;
-
Replace the Application Insights configuration in your
Program.cswith the following (place it afterConfigureFunctionsWebApplication):builder.Logging.AddOpenTelemetry(logging => { logging.IncludeFormattedMessage = true; logging.IncludeScopes = true; }); builder.Services.AddOpenTelemetry() .WithTracing(tracing => tracing // Enables HttpClient instrumentation. .AddHttpClientInstrumentation()); builder.Services.AddOpenTelemetry().UseAzureMonitorExporter(options => { // Set the Azure Monitor credential to the DefaultAzureCredential. // This credential will use the Azure identity of the current user or // the service principal that the application is running as to authenticate // to Azure Monitor. options.Credential = new DefaultAzureCredential(); }); builder.Services.AddOpenTelemetry().UseFunctionsWorkerDefaults();
Notes:
-
The above example uses
DefaultAzureCredential, but it's a best practice to use a more specific credential if possible. See Authentication best practices with the Azure Identity library for .NET for more information. -
The above sample uses the system-assigned managed identity to authenticate with Application Insights. See Microsoft Entra authentication for Application Insights for more information.
If you don't need this, you can use the following instead:
builder.Services.AddOpenTelemetry().UseAzureMonitorExporter();
-
This sample shows how to configure the credentials when using a user-assigned managed identity to authenticate to App Insights.
-
Sample query to select Azure Functions based on 'old' Application Insights logging:
requests
| where customDimensions['Category'] == 'Host.Results'
| extend functionTrigger = customDimensions['TriggerReason']
| project
timestamp,
cloud_RoleName,
functionName = name,
duration,
success,
resultCode,
functionTrigger
| sort by timestamp descSample query to select Azure Functions based on OpenTelemetry logging:
requests
| extend functionName = tostring(customDimensions['faas.name'])
| where functionName != '' // Only include Azure Function related requests
| extend functionTrigger = tostring(customDimensions['faas.trigger'])
| project
timestamp,
cloud_RoleName,
functionName,
duration,
success,
resultCode,
functionTrigger
| sort by timestamp descSample query to select Azure Functions based on combined 'old' Application Insights and OpenTelemetry logging:
requests
| where customDimensions['faas.name'] != '' or customDimensions['Category'] == 'Host.Results'
| extend functionName = tostring(coalesce(customDimensions['faas.name'], name))
| extend functionTrigger = tostring(coalesce(customDimensions['faas.trigger'], customDimensions['TriggerReason']))
| project
timestamp,
cloud_RoleName,
functionName,
duration,
success,
resultCode,
functionTrigger
| sort by timestamp descIf you've build a solution that uses Application Insight's TelemetryClient directly, you might also want to update to version 3.x of Microsoft.ApplicationInsights after migrating to OpenTelemetry.
The migration guide provides detailed explanations of how to migrate from Application Insights 2.x to 3.x, and this document outlines the breaking changes.
I've created this azd template myself, which shows how to track availability in Application Insights using .NET. The PR Migrate to Application Insights .NET SDK version 3.0.0 in Function App shows the changes I had to make to update to Microsoft.ApplicationInsights version 3.x
Thanks @openPablo. I have updated the considerations section and added relevant links.