Question

Difficulty: MediumConfigure Application Insights Instrumentation and Telemetry

You are developing an ASP.NET Core Web API with the Application Insights SDK. To optimize resource consumption in a containerized environment, you must programmatically disable the collection of performance counters and disable adaptive sampling. Complete the code snippet by filling in the correct properties of the ApplicationInsightsServiceOptions class.

Answer:csharp
builder.Services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
options.【EnablePerformanceCounterCollectionModule】 = false; // Disables performance counter collection
options.【EnableAdaptiveSampling】 = false; // Disables adaptive sampling
});

Answer

To programmatically disable performance counter collection, set the EnablePerformanceCounterCollectionModule property to false. To disable adaptive sampling, set the EnableAdaptiveSampling property to false.
The correct properties are EnablePerformanceCounterCollectionModule to toggle the OS performance counter collection module and EnableAdaptiveSampling to control the sampling logic applied to outbound telemetry data.

Step-by-Step Solution

1
Locate the configuration options class used by the Application Insights SDK in ASP.NET Core.
ApplicationInsightsServiceOptions is passed to the AddApplicationInsightsTelemetry extension method configuration lambda.
This class holds the properties to configure the default telemetry modules and telemetry collection behaviors.
2
Find the boolean property responsible for loading the PerformanceCollectorModule.
EnablePerformanceCounterCollectionModule
Setting this property to false stops the SDK from spinning up the module that collects CPU, memory, and garbage collection metrics from the host operating system.
3
Find the boolean property that controls adaptive sampling.
EnableAdaptiveSampling
Setting this property to false disables adaptive sampling, which ensures 100% of telemetry data is transmitted rather than being dynamically sampled.

Key Concept

Configuring default telemetry modules and behavior using ApplicationInsightsServiceOptions in ASP.NET Core.
Rate this question