Search code examples
azureazure-application-insightsappinsights

Configuring Adaptive Sampling not reflected in Azure Portal


I am configuring Azure App Insight's adaptive sampling in my appsettings.json file based on this article. https://learn.microsoft.com/en-us/azure/azure-monitor/app/sampling-classic-api#configuring-adaptive-sampling-for-aspnet-applications

My appsettings.json:

  "ApplicationInsightsSamplingSettings": {
    "InitialSamplingPercentage": 60.0,
    "MinSamplingPercentage": 0.01,
    "MaxSamplingPercentage": 60.0,
    "MaxTelemetryItemsPerSecond": 5
  },

I know that sampling is on because I can query App Insight Logs with sum(itemCount) and it shows a higher value than using count().

dependencies
| summarize count() //sampled

dependencies
| summarize sum(itemCount) //Not sampled and will result in a higher number.

So why does the Azure Portal not reflect my sampling percentages? No matter what, the Data Sampling under App Insights >> Usage and estimated costs >> Data sampling, always return 100%.

enter image description here


Solution

  • When Adaptive Sampling is enabled, it automatically sets the Sampling percentage in Application Inisights=>Data Sampling.

    As per my observation, InitialSamplingPercentage doesn't reflects in the Portal=>Application Insights=>Data Sampling and the data sampling percentage automatically gets assigned according to the sampled data.

    To configure the InitialSamplingPercentage manually, disable the adaptive sampling by adding the setting:

    {
    "EnableAdaptiveSampling":false
    }
    

    I have tested the same with both ASP.NET and ASP.NET core in my environment to check if the manually configured sampling percentage reflects in the Portal, but the InitialSamplingPercentage doesn't affect the Data Sampling percentage in Portal.

    ASP.NET core:

    • Used below code to configure the settings manually in ASP.NET core. Refer SO solution by @Anarion
    var aiOptions = new ApplicationInsightsServiceOptions
    {
        EnableAdaptiveSampling = false,
        EnableQuickPulseMetricStream = false,
    };
    builder.Services.AddApplicationInsightsTelemetry(aiOptions);
    
    builder.Services.Configure<TelemetryConfiguration>(telemetryConfiguration =>
    {
        var telemetryProcessorChainBuilder = telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
        telemetryProcessorChainBuilder.UseAdaptiveSampling(maxTelemetryItemsPerSecond: 5);
        telemetryProcessorChainBuilder.UseAdaptiveSampling(new SamplingPercentageEstimatorSettings
    {
         MaxSamplingPercentage=50,
         MinSamplingPercentage=0.1,
         InitialSamplingPercentage=0.1
        }, null, excludedTypes: ""); 
        telemetryProcessorChainBuilder.Build();
    });
    

    ASP.NET:

    <TelemetryProcessors>
      <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryProcessor, Microsoft.AI.PerfCounterCollector" />
      <Add Type="Microsoft.ApplicationInsights.Extensibility.AutocollectedMetricsExtractor, Microsoft.ApplicationInsights" />
      <Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
        <MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
        <ExcludedTypes>Event</ExcludedTypes>
        <InitialSamplingPercentage>0.1</InitialSamplingPercentage>
      </Add>
      <Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
        <MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
        <IncludedTypes>Event</IncludedTypes>
        <InitialSamplingPercentage>0.1</InitialSamplingPercentage>
      </Add>
    </TelemetryProcessors>
    
    • Run the below Kusto query to check the % of requests being sampled:

    union requests,dependencies,pageViews,browserTimings,exceptions,traces
    | where timestamp > ago(1d)
    | summarize RetainedPercentage = 100/avg(itemCount) by bin(timestamp, 1h), itemType

    enter image description here

    As mentioned in the screenshot you have provided, if we are enabling the sampling manually using code or if the adaptive sampling enabled, it applies those settings regardless of the sampling percentage assigned in the portal.

    References:

    Azure Application Insight Sampling Configuration | by Chhavi Goel