Skip to content

Commit ded94f1

Browse files
authored
Merge pull request #217456 from kainawroth/patch-1
Adding .NET 6 version of enabling and configuring sampling
2 parents cceec12 + 36cd90d commit ded94f1

File tree

1 file changed

+113
-20
lines changed

1 file changed

+113
-20
lines changed

articles/azure-monitor/app/sampling.md

Lines changed: 113 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.reviewer: mmcc
99

1010
# Sampling in Application Insights
1111

12-
Sampling is a feature in [Azure Application Insights](./app-insights-overview.md). It's the recommended way to reduce telemetry traffic, data costs, and storage costs, while preserving a statistically correct analysis of application data. Sampling also helps you avoid Application Insights throttling your telemetry. The sampling filter selects items that are related, so that you can navigate between items when you're doing diagnostic investigations.
12+
Sampling is a feature in [Application Insights](./app-insights-overview.md). It's the recommended way to reduce telemetry traffic, data costs, and storage costs, while preserving a statistically correct analysis of application data. Sampling also helps you avoid Application Insights throttling your telemetry. The sampling filter selects items that are related, so that you can navigate between items when you're doing diagnostic investigations.
1313

1414
When metric counts are presented in the portal, they're renormalized to take into account sampling. Doing so minimizes any effect on the statistics.
1515

@@ -139,7 +139,7 @@ In [`ApplicationInsights.config`](./configuration-with-applicationinsights-confi
139139
Instead of setting the sampling parameter in the `.config` file, you can programmatically set these values.
140140

141141
1. Remove all the `AdaptiveSamplingTelemetryProcessor` node(s) from the `.config` file.
142-
2. Use the following snippet to configure adaptive sampling:
142+
1. Use the following snippet to configure adaptive sampling:
143143

144144
```csharp
145145
using Microsoft.ApplicationInsights;
@@ -179,21 +179,37 @@ Adaptive sampling is enabled by default for all ASP.NET Core applications. You c
179179

180180
#### Turning off adaptive sampling
181181

182-
The default sampling feature can be disabled while adding Application Insights service, in the method `ConfigureServices`, using `ApplicationInsightsServiceOptions` within the `Startup.cs` file:
182+
The default sampling feature can be disabled while adding the Application Insights service.
183+
184+
### [ASP.NET Core 6 and later](#tab/net-core-new)
185+
186+
Add `ApplicationInsightsServiceOptions` after the `WebApplication.CreateBuilder()` method in the `Program.cs` file:
187+
188+
```csharp
189+
var builder = WebApplication.CreateBuilder(args);
190+
191+
var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
192+
aiOptions.EnableAdaptiveSampling = false;
193+
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
194+
195+
var app = builder.Build();
196+
```
197+
198+
### [ASP.NET Core 5 and earlier](#tab/net-core-old)
199+
200+
Add `ApplicationInsightsServiceOptions` to the `ConfigureServices()` method in the `Startup.cs` file:
183201

184202
```csharp
185203
public void ConfigureServices(IServiceCollection services)
186204
{
187-
// ...
188-
189205
var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
190206
aiOptions.EnableAdaptiveSampling = false;
191207
services.AddApplicationInsightsTelemetry(aiOptions);
192-
193-
//...
194208
}
195209
```
196210

211+
---
212+
197213
The above code will disable adaptive sampling. Follow the steps below to add sampling with more customization options.
198214

199215
#### Configure sampling settings
@@ -203,6 +219,39 @@ Use extension methods of `TelemetryProcessorChainBuilder` as shown below to cust
203219
> [!IMPORTANT]
204220
> If you use this method to configure sampling, please make sure to set the `aiOptions.EnableAdaptiveSampling` property to `false` when calling `AddApplicationInsightsTelemetry()`. After making this change, you then need to follow the instructions in the code block below **exactly** in order to re-enable adaptive sampling with your customizations in place. Failure to do so can result in excess data ingestion. Always test post changing sampling settings, and set an appropriate [daily data cap](../logs/daily-cap.md) to help control your costs.
205221
222+
### [ASP.NET Core 6 and later](#tab/net-core-new)
223+
224+
```csharp
225+
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
226+
using Microsoft.ApplicationInsights.Extensibility;
227+
228+
var builder = WebApplication.CreateBuilder(args);
229+
230+
builder.Services.Configure<TelemetryConfiguration>(telemetryConfiguration =>
231+
{
232+
var builder = telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
233+
234+
// Using adaptive sampling
235+
builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond: 5);
236+
237+
// Alternately, the following configures adaptive sampling with 5 items per second, and also excludes DependencyTelemetry from being subject to sampling:
238+
// configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: "Dependency");
239+
240+
// If you have other telemetry processors:
241+
builder.Use(next => new AnotherProcessor(next));
242+
243+
});
244+
245+
builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
246+
{
247+
EnableAdaptiveSampling = false,
248+
});
249+
250+
var app = builder.Build();
251+
```
252+
253+
### [ASP.NET Core 5 and earlier](#tab/net-core-old)
254+
206255
```csharp
207256
using Microsoft.ApplicationInsights.Extensibility
208257

@@ -222,11 +271,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, Telemetr
222271
builder.Use((next) => new AnotherProcessor(next));
223272

224273
builder.Build();
225-
226-
// ...
227274
}
228275
```
229276

277+
---
278+
230279
### Configuring adaptive sampling for Azure Functions
231280

232281
Follow instructions from [this page](../../azure-functions/configure-monitoring.md#configure-sampling) to configure adaptive sampling for apps running in Azure Functions.
@@ -252,7 +301,7 @@ In Metrics Explorer, rates such as request and exception counts are multiplied b
252301
-->
253302
```
254303

255-
2. **Enable the fixed-rate sampling module.** Add this snippet to [`ApplicationInsights.config`](./configuration-with-applicationinsights-config.md):
304+
1. **Enable the fixed-rate sampling module.** Add this snippet to [`ApplicationInsights.config`](./configuration-with-applicationinsights-config.md):
256305

257306
```xml
258307
<TelemetryProcessors>
@@ -264,8 +313,8 @@ In Metrics Explorer, rates such as request and exception counts are multiplied b
264313
</TelemetryProcessors>
265314
```
266315

267-
Alternatively, instead of setting the sampling parameter in the `ApplicationInsights.config` file, you can programmatically set these values:
268-
316+
Alternatively, instead of setting the sampling parameter in the `ApplicationInsights.config` file, you can programmatically set these values:
317+
269318
```csharp
270319
using Microsoft.ApplicationInsights.Extensibility;
271320
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
@@ -288,22 +337,66 @@ In Metrics Explorer, rates such as request and exception counts are multiplied b
288337

289338
### Configuring fixed-rate sampling for ASP.NET Core applications
290339

291-
1. **Disable adaptive sampling**: Changes can be made in the `ConfigureServices` method, using `ApplicationInsightsServiceOptions`:
340+
1. **Disable adaptive sampling**
341+
342+
### [ASP.NET Core 6 and later](#tab/net-core-new)
343+
344+
Changes can be made after the `WebApplication.CreateBuilder()` method, using `ApplicationInsightsServiceOptions`:
345+
346+
```csharp
347+
var builder = WebApplication.CreateBuilder(args);
348+
349+
var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
350+
aiOptions.EnableAdaptiveSampling = false;
351+
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
352+
353+
var app = builder.Build();
354+
```
355+
356+
### [ASP.NET Core 5 and earlier](#tab/net-core-old)
357+
358+
Changes can be made in the `ConfigureServices()` method, using `ApplicationInsightsServiceOptions`:
292359

293360
```csharp
294361
public void ConfigureServices(IServiceCollection services)
295362
{
296-
// ...
297-
298363
var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
299364
aiOptions.EnableAdaptiveSampling = false;
300365
services.AddApplicationInsightsTelemetry(aiOptions);
301-
302-
//...
303366
}
304367
```
368+
369+
---
370+
371+
1. **Enable the fixed-rate sampling module**
372+
373+
### [ASP.NET Core 6 and later](#tab/net-core-new)
374+
375+
Changes can be made after the `WebApplication.CreateBuilder()` method:
376+
377+
```csharp
378+
var builder = WebApplication.CreateBuilder(args);
379+
380+
builder.Services.Configure<TelemetryConfiguration>(telemetryConfiguration =>
381+
{
382+
var builder = telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
383+
384+
// Using fixed rate sampling
385+
double fixedSamplingPercentage = 10;
386+
builder.UseSampling(fixedSamplingPercentage);
387+
});
388+
389+
builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
390+
{
391+
EnableAdaptiveSampling = false,
392+
});
393+
394+
var app = builder.Build();
395+
```
305396

306-
2. **Enable the fixed-rate sampling module.** Changes can be made in the `Configure` method as shown in the below snippet:
397+
### [ASP.NET Core 5 and earlier](#tab/net-core-old)
398+
399+
Changes can be made in the `Configure()` method:
307400

308401
```csharp
309402
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@@ -319,11 +412,11 @@ In Metrics Explorer, rates such as request and exception counts are multiplied b
319412
builder.UseSampling(fixedSamplingPercentage);
320413

321414
builder.Build();
322-
323-
// ...
324415
}
325416
```
326417

418+
---
419+
327420
### Configuring sampling overrides and fixed-rate sampling for Java applications
328421

329422
By default no sampling is enabled in the Java auto-instrumentation and SDK. Currently the Java auto-instrumentation, [sampling overrides](./java-standalone-sampling-overrides.md) and fixed rate sampling are supported. Adaptive sampling isn't supported in Java.

0 commit comments

Comments
 (0)