Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ronaldbosma/ac21e32e2d7f8ad1512760bca8b0609b to your computer and use it in GitHub Desktop.

Select an option

Save ronaldbosma/ac21e32e2d7f8ad1512760bca8b0609b to your computer and use it in GitHub Desktop.

Migrate Azure Functions project from Application Insights to OpenTelemetry

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:

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.

Considerations

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.

Instructions

  1. Set telemetry mode to OpenTelemetry in the host.json file of your project:

    {
    	"version": "2.0",
    	"telemetryMode": "OpenTelemetry",
    	...
    }
  2. Make sure the app setting APPLICATIONINSIGHTS_CONNECTION_STRING is set in your Function App.

  3. Remove Application Insights packages:

    dotnet remove package Microsoft.ApplicationInsights.WorkerService
    dotnet remove package Microsoft.Azure.Functions.Worker.ApplicationInsights
    
  4. 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.Http
    

    The first 3 packages are necessary for OpenTelemetry to work. The OpenTelemetry.Instrumentation.Http package can be used to instrument System.Net.Http.HttpClient and System.Net.HttpWebRequest.

  5. Add the following usings to your Program.cs:

    using Azure.Monitor.OpenTelemetry.Exporter;
    using Microsoft.Azure.Functions.Worker.OpenTelemetry;
    using OpenTelemetry.Trace;
  6. Replace the Application Insights configuration in your Program.cs with the following (place it after ConfigureFunctionsWebApplication):

    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:

Kusto Queries

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 desc

Sample 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 desc

Sample 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 desc

Other

If 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

@openPablo

Copy link
Copy Markdown

Make sure to mention something about trace sampling to avoid crazy costs :)

@ronaldbosma

Copy link
Copy Markdown
Author

Make sure to mention something about trace sampling to avoid crazy costs :)

Thanks @openPablo. I have updated the considerations section and added relevant links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment