Implement App Insights Telemetry Processor in Azure Functions

Premier Developer

Developer

Dev Consultant Adel Ghabboun explores App Insights Telemetry Processor in Azure Functions.


In this post, will walk you through how to implement an Application Insights telemetry processor in Azure functions using Visual Studio.NET 2019.

Telemetry Processor is a plug-ins that can be ingested in your application to customize how telemetry can be processed before it’s sent to the Application Insights service such as filtering out telemetry, replace or discard a telemetry item, etc.

As an example, let’s assume that we need to discard any Request with 200 Status Code:

  1. Open Visual Studio 2019
  2. Create new Azure Function app with HTTP trigger (follow the steps in the Azure Functions documentation) . It will look like this:
  1. After you create, deploy, run and test. Configure Application Insights for your Azure Functions resource in Azure (Follow documentation to Enable Application Insights Integration only)
  2. Now, you have your Azure Function app is deployed and configured with Application Insights for monitoring.
  3. Go back to your Visual Studio solution
  4. Install Application Insights Nuget package
  5. Open Function1.cs file
  6. Add the following namespaces at the top of the class
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
  1. Under the main Run method, create your TelemetryProcessor class and add your implementation as the below:
internal class MyTelemetryProcessor : ITelemetryProcessor
{
   private readonly ITelemetryProcessor Next;

   public MyTelemetryProcessor(ITelemetryProcessor next)
   {
       this.Next = next;
   }
   public void Process(ITelemetry item)
   {
       if (item is RequestTelemetry request)
       {
           if (request.ResponseCode == "200")
           {
              return;
           }
       }
       this.Next.Process(item);
    }
}
  1. Now we need to add the processor to the chain. Add the following class under the MyTelemetryProcessor above
public class MyStartup : IWebJobsStartup
{
   public void Configure(IWebJobsBuilder builder)
   {
      var configDescriptor = builder.Services.SingleOrDefault(tc => tc.ServiceType == typeof(TelemetryConfiguration));
      if (configDescriptor?.ImplementationFactory == null)
         return;

      var implFactory = configDescriptor.ImplementationFactory;

      builder.Services.Remove(configDescriptor);
      builder.Services.AddSingleton(provider =>
      {
         if (!(implFactory.Invoke(provider) is TelemetryConfiguration config))
             return null;

         config.TelemetryProcessorChainBuilder.Use(next => new MyTelemetryProcessor(next));
          config.TelemetryProcessorChainBuilder.Build();

          return config;
        });
     }
}
  1. Add the following line at the namespace level
    [assembly: WebJobsStartup(typeof(FunctionApp1.Function1.MyStartup))]
  2. Build your project, test and redeploy to Azure.
  3. After you deploy, you won’t see any request with 200 status code in Application Insights resource.

Summary:

  1. I used the above example only to show how you would configure Application Insights processor with Azure functions.
  2. Disabling successful Request might break other feature in Application Insights like end-to-end transaction viewer. So even though Request is successful, but you might be interested in what happened for the whole transaction.
  3. Carefully review everything before deciding what to disable.

0 comments

Leave a comment