microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
565 stars 287 forks source link

EnableAdaptiveSampling with "excludedTypes" not taking effect #2570

Closed Jithesh-Menon closed 1 year ago

Jithesh-Menon commented 2 years ago

List of NuGet packages and version that you are using: Microsoft.ApplicationInsights 2.20.0 Microsoft.ApplicationInsights.WorkerService 2.20.0 and Microsoft.Extensions.DependencyInjection 6.0.0 Microsoft.Extensions.DependencyInjection.Abstractions 6.0.0 Microsoft.Identity.Client.Desktop 4.41.0 System.Runtime.CompilerServices.Unsafe 6.0.0

Runtime version (e.g. net461, net48, netcoreapp2.1, netcoreapp3.1, etc. You can find this information from the *.csproj file): NetFramework4.7.2

Hosting environment (e.g. Azure Web App, App Service on Linux, Windows, Ubuntu, etc.): Windows

Describe the bug

Attempting to use the default setting of EnableAdaptiveSampling - true and setting exclude types has not had the desired effect. i.e, trying to exclude the "Dependency" from the app insights logging [and also tried with "Request"] still logs details into app insights for the cloud role instance (my machine) that the app is running on.

Referred the below links :- Configuring adaptive sampling for ASP.NET applications https://github.com/toddkitta/azure-content/blob/master/articles/application-insights/app-insights-sampling.md

Approach:- Configure adaptive sampling in code EnableDependencyTrackingTelemetryModule and EnableAdaptiveSampling settings set to true

 var telemetryConfig = serviceProvider.GetRequiredService<TelemetryConfiguration>();

  var builder = telemetryConfig.DefaultTelemetrySink.TelemetryProcessorChainBuilder;

  // Alternately, the following configures adaptive sampling with 5 items per second, and also excludes
  // DependencyTelemetry from being subject to sampling.
  builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: "Dependency");

  builder.Build();

But the above approach didn't reflect and does provide results for KQL query.

union requests,dependencies,pageViews,browserTimings,exceptions,traces
| where cloud_RoleInstance contains "MyMachine"
and timestamp >= todatetime('2022-04-04T12:19:00.775Z') 
and timestamp <= todatetime('2022-04-04T12:22:13.538Z')

Please check and advise.

Thank you.

cijothomas commented 2 years ago

Please share a minimal repro application demonstrating the issue. its not clear how are you concluding "not taking effect". code sample, along with the actual queries used to validate if sampling is done would help us understand the question better.

cijothomas commented 2 years ago

https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling#knowing-whether-sampling-is-in-operation

Jithesh-Menon commented 2 years ago

Hi @cijothomas ,

In my query I have explained what I am trying to do with code sample and the query I used to see the results in app insights.

I was trying to reduce the traffic into app insights by excluding the "Dependency". I have also tried the same approach for "Request" as well. As I mentioned in my query, I am using a .Net Framework 4.7.2 application and trying to make the best out of app insights settings options.

Pasting the code approach I did from the query section for your convenience.

Approach:- Configure adaptive sampling in code EnableDependencyTrackingTelemetryModule and EnableAdaptiveSampling settings set to true

 var telemetryConfig = serviceProvider.GetRequiredService<TelemetryConfiguration>();

  var builder = telemetryConfig.DefaultTelemetrySink.TelemetryProcessorChainBuilder;

  // Alternately, the following configures adaptive sampling with 5 items per second, and also excludes
  // DependencyTelemetry from being subject to sampling.
  builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: "Dependency");

  builder.Build();

Irrespective of the sampling exclude types settings I provided Dependency/Request are still post details into app insights for the cloud role instance (my machine) that the app is running on.

Kindly advise.

Thank you

cijothomas commented 2 years ago

app insights by excluding the "Dependency"

ExcludedTypes in UseAdaptiveSampling - means just the opposite. i.e Dependency items are NOT considered for sampling, means they are always sent. You want the opposite, based on your description.

Jithesh-Menon commented 2 years ago

Thank you!

So, how could we ensure the Dependency is not considered for sampling while it is present in application insights logs.

Could you please advise?

cijothomas commented 2 years ago

// Alternately, the following configures adaptive sampling with 5 items per second, and also excludes // DependencyTelemetry from being subject to sampling. builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: "Dependency");

Jithesh-Menon commented 2 years ago

// Alternately, the following configures adaptive sampling with 5 items per second, and also excludes // DependencyTelemetry from being subject to sampling. builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: "Dependency");

This code will ensure Dependency is being excluded from Sampling. But how could we verify it got excluded..? Could you please provide a clarification on that..?

cijothomas commented 2 years ago

// Alternately, the following configures adaptive sampling with 5 items per second, and also excludes // DependencyTelemetry from being subject to sampling. builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: "Dependency");

This code will ensure Dependency is being excluded from Sampling. But how could we verify it got excluded..? Could you please provide a clarification on that..?

https://docs.microsoft.com/en-us/azure/azure-monitor/app/sampling#knowing-whether-sampling-is-in-operation

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 300 days with no activity. Remove stale label or this will be closed in 7 days. Commenting will instruct the bot to automatically remove the label.

tlsun0921 commented 1 year ago

i tried to exclude the following types in ApplicationInsights.config from sampling image

after more than 1 day i run the query to confirm, request type is still sampled. image

anyone know if there is anything wrong? thank you