Mike Christensen
Mike Christensen

Reputation: 91666

Adaptive Sampling not working in ApplicationInsights

I'm trying to get sampling working in AppInsights and not having much luck. Here's my repro:

First, I setup a TelemetryClient:

TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = "f7968b3b-b5ac-4e00-a6d9-f4915e5c5d32";
telemetryClientTest = new TelemetryClient(configuration);
var builder = configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond: 1);
builder.Build();

Next, I try logging a bunch of events:

for(var i = 0; i < 500; i++)
{
    telemetryClientTest.TrackEvent("SampledThingHappened");

     if(i % 100 == 0)
         await Task.Delay(1000);
}

Every hundred, I'll delay for a second. What I expect is a few chunks of SampledThingHappened events, each with a itemCount of probably around 100 or so. Instead, I see all 500 items each with an itemCount of 1.

customEvents
| where name == "SampledThingHappened"
| where itemCount > 1

returns no items.

Everything in the DefaultTelemetrySink looks correct, and I can see my AdaptiveSamplingTelemetryProcessor in there with all the correct settings:

enter image description here

Upvotes: 0

Views: 1635

Answers (1)

Harshita Singh
Harshita Singh

Reputation: 4870

Since, there is no ApplicationInsights.config for ASP.NET Core applications, so all configuration is done via code. Adaptive sampling is enabled by default for all ASP.NET Core applications. You can disable or customize the sampling behavior.

Customize Adaptive Sampling settings:

using Microsoft.ApplicationInsights.Extensibility

public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration configuration)
{
    var builder = configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
    // For older versions of the Application Insights SDK, use the following line instead:
    // var builder = configuration.TelemetryProcessorChainBuilder;

    // Using adaptive sampling
    builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5);

    // 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");

    // If you have other telemetry processors:
    builder.Use((next) => new AnotherProcessor(next));
    
    builder.Build();

    // ...
}

For ASP.NET application App Insights sampling setup, visit Configuring adaptive sampling for ASP.NET applications.

To verify the actual sampling rate no matter where it has been applied, use an Analytics query such as below:

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

If you see that RetainedPercentage for any type is less than 100, then that type of telemetry is being sampled.

Upvotes: 3

Related Questions