Question

Difficulty: HardConfigure Application Insights Instrumentation and Telemetry

You are developing an ASP.NET Core Web API hosted on Azure App Service. You must implement a custom telemetry processor named DependencyFilterProcessor to filter out successful SQL dependency telemetry before it is sent to Application Insights. You also need to register this telemetry processor in the dependency injection container.

How should you complete the code segments for the processor implementation and service registration?

Answer:csharp
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

// Register the custom telemetry processor
builder.Services.【AddApplicationInsightsTelemetryProcessor】<DependencyFilterProcessor>();

// DependencyFilterProcessor.cs
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

public class DependencyFilterProcessor : 【ITelemetryProcessor】
{
private readonly 【ITelemetryProcessor】 _next;

public DependencyFilterProcessor(【ITelemetryProcessor】 next)
{
_next = next;
}

public void Process(ITelemetry item)
{
// Filtering logic goes here
_next.Process(item);
}
}

Answer

To configure a custom telemetry processor, register it in the dependency injection container using the `AddApplicationInsightsTelemetryProcessor<DependencyFilterProcessor>()` extension method. The processor class and its constructor argument must implement the `ITelemetryProcessor` interface.
The correct implementation utilizes `AddApplicationInsightsTelemetryProcessor` to register the processor, which implements `ITelemetryProcessor` and receives the next `ITelemetryProcessor` in its constructor to continue the pipeline execution.

Step-by-Step Solution

1
Identify the dependency injection extension method for custom telemetry processors in ASP.NET Core.
The correct method is `AddApplicationInsightsTelemetryProcessor`.
This method registers the processor and ensures that the SDK properly chains it with other processors and supplies the next processor via constructor injection.
2
Determine the interface required for implementing a custom telemetry processor.
The interface is `ITelemetryProcessor`.
Custom telemetry processors must implement the `ITelemetryProcessor` interface and define the `Process` method.
3
Determine the parameter type needed in the processor's constructor to chain telemetry processors.
The type is `ITelemetryProcessor`.
The constructor of a custom telemetry processor must accept the next `ITelemetryProcessor` in the execution chain to pass telemetry along.

Key Concept

Custom Telemetry Processors in Application Insights
Rate this question