0

I have a DotNet 6 application ASP.NET Web API application that is running as an App Service in Azure.

I do not have fixed sampling enabled. I also do not have applicationinsights.json file in my application.

I have code to disable Adaptive Sampling in the ConfigureServices method. The code snippet is below:

  if (!string.IsNullOrEmpty(configuration["ApplicationInsights:InstrumentationKey"]))
            {
                services.AddApplicationInsightsTelemetry(options =>
                {
                    if (webHostEnvironment.IsDevelopment())
                    {
                        options.DeveloperMode = true;
                        options.EnableDebugLogger = false;
                    }
                    options.EnableAdaptiveSampling = false;
                    options.InstrumentationKey = configuration["ApplicationInsights:InstrumentationKey"];
                });
                services.AddApplicationInsightsTelemetryProcessor<FilterProcessor>();
                services.AddSingleton<ITelemetryInitializer, NopContextInitializer>();
            }

My Azure settings for application insights is as follows for ingestion samplingenter image description here

I continue to see sampling happening when I run the below query:

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

My application was a .NET Core 3.1 a few weeks back and could not get sampling to be turned off. Even after upgrading to DotNet 6, I am not able to turn off sampling.

Any pointers on why this may be happening ?

Charles Han
  • 1,920
  • 2
  • 10
  • 19

1 Answers1

0

I also do not have applicationinsights.json file in my application.

When we add Application Insights Telemetry from Visual Studio => Project Root Folder, by default ApplicationInsights.config used to be generated automatically.

enter image description here

I have Added Application Insights Telemetry from Project Root Folder, even I didn't find ApplicationInsights.config added in the application.

There might be some latest changes in Adding the ApplicationInsights Telemetry.

We can disable sampling from either ApplicationInsights.config or from code.

As we don't have ApplicationInsights.config file, will try to disable the sampling from Code.

As mentioned in the MSDoc,

Only ASP.NET server-side telemetry currently supports adaptive sampling.

In Program.cs, add the below lines of code

var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
aiOptions.EnableAdaptiveSampling = false; 
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
  • This code works only for the Production Environment(Deployed Application).

For .NET Core 6

My Program.cs

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
using Microsoft.ApplicationInsights.Extensibility;
var builder = WebApplication.CreateBuilder(args);

var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
aiOptions.EnableAdaptiveSampling = false;
builder.Services.AddApplicationInsightsTelemetry(aiOptions);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
   app.UseSwagger();
   app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

My appsettings.json file

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=********;IngestionEndpoint=https://****.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/",
    "EnableAdaptiveSampling": false,
    "EnablePerformanceCounterCollectionModule": false
  }
}
  • Re-deploy the App, Access the URL and check the traces/Logs.

References taken from MSDoc

Harshitha
  • 3,784
  • 2
  • 4
  • 9
  • Hi Harshitha. Thanks for your response. I have application insights adaptive sampling set to false using the code. Inspite of having it set to false, I do see sampling happening in the azure logs. I have already looked at the microsoft documentation before I posted in stack overflow. Is your response based on microsoft documentation or do you have a asp.net core web api application deployed with application insights sampling turned off ? – ashwanth sreedharan Jan 03 '23 at 16:31