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:
- Open Visual Studio 2019
- Create new Azure Function app with HTTP trigger (follow the steps in the Azure Functions documentation) . It will look like this:
- 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)
- Now, you have your Azure Function app is deployed and configured with Application Insights for monitoring.
- Go back to your Visual Studio solution
- Install Application Insights Nuget package
- Open Function1.cs file
- Add the following namespaces at the top of the class
using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts;
- 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); } }
- 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; }); } }
- Add the following line at the namespace level
[assembly: WebJobsStartup(typeof(FunctionApp1.Function1.MyStartup))]
- Build your project, test and redeploy to Azure.
- After you deploy, you won’t see any request with 200 status code in Application Insights resource.
Summary:
- I used the above example only to show how you would configure Application Insights processor with Azure functions.
- 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.
- Carefully review everything before deciding what to disable.
0 comments