Skip to content

Commit c665b4c

Browse files
committed
Corrections to Filtering and Enrichment doc
1 parent b66a352 commit c665b4c

File tree

1 file changed

+54
-55
lines changed

1 file changed

+54
-55
lines changed

articles/azure-monitor/app/api-filtering-sampling.md

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ms.author: mbullwin
2121
You can write and configure plug-ins for the Application Insights SDK to customize how telemetry is captured and processed before it is sent to the Application Insights service.
2222

2323
* [Sampling](../../azure-monitor/app/sampling.md) reduces the volume of telemetry without affecting your statistics. It keeps together related data points so that you can navigate between them when diagnosing a problem. In the portal, the total counts are multiplied to compensate for the sampling.
24-
* Filtering with Telemetry Processors [for ASP.NET](#filtering) or [Java](../../azure-monitor/app/java-filter-telemetry.md) lets you select or modify telemetry in the SDK before it is sent to the server. For example, you could reduce the volume of telemetry by excluding requests from robots. But filtering is a more basic approach to reducing traffic than sampling. It allows you more control over what is transmitted, but you have to be aware that it affects your statistics - for example, if you filter out all successful requests.
24+
* Filtering with Telemetry Processors [for ASP.NET or ASP.NET Core](#filtering) or [Java](../../azure-monitor/app/java-filter-telemetry.md) lets you select or modify telemetry in the SDK before it is sent to the server. For example, you could reduce the volume of telemetry by excluding requests from robots. But filtering is a more basic approach to reducing traffic than sampling. It allows you more control over what is transmitted, but you have to be aware that it affects your statistics - for example, if you filter out all successful requests.
2525
* [Telemetry Initializers add properties](#add-properties) to any telemetry sent from your app, including telemetry from the standard modules. For example, you could add calculated values; or version numbers by which to filter the data in the portal.
2626
* [The SDK API](../../azure-monitor/app/api-custom-events-metrics.md) is used to send custom events and metrics.
2727

@@ -32,9 +32,10 @@ Before you start:
3232
<a name="filtering"></a>
3333

3434
## Filtering: ITelemetryProcessor
35-
This technique gives you more direct control over what is included or excluded from the telemetry stream. You can use it in conjunction with Sampling, or separately.
3635

37-
To filter telemetry, you write a telemetry processor and register it with the SDK. All telemetry goes through your processor, and you can choose to drop it from the stream, or add properties. This includes telemetry from the standard modules such as the HTTP request collector and the dependency collector, as well as telemetry you have written yourself. You can, for example, filter out telemetry about requests from robots, or successful dependency calls.
36+
This technique gives you more direct control over what is included or excluded from the telemetry stream. Filtering can be used to drop telemetry items from being sent to Application Insights. You can use it in conjunction with Sampling, or separately.
37+
38+
To filter telemetry, you write a telemetry processor and register it with the `TelemetryConfiguration`. All telemetry goes through your processor, and you can choose to drop it from the stream, or add properties. This includes telemetry from the standard modules such as the HTTP request collector and the dependency collector, as well as telemetry you have written yourself. You can, for example, filter out telemetry about requests from robots, or successful dependency calls.
3839

3940
> [!WARNING]
4041
> Filtering the telemetry sent from the SDK using processors can skew the statistics that you see in the portal, and make it difficult to follow related items.
@@ -44,56 +45,45 @@ To filter telemetry, you write a telemetry processor and register it with the SD
4445
>
4546
4647
### Create a telemetry processor (C#)
47-
1. Verify that the Application Insights SDK in your project is version 2.0.0 or later. Right-click your project in Visual Studio Solution Explorer and choose Manage NuGet Packages. In NuGet package manager, check Microsoft.ApplicationInsights.Web.
48-
2. To create a filter, implement ITelemetryProcessor. This is another extensibility point like telemetry module, telemetry initializer, and telemetry channel.
4948

50-
Notice that Telemetry Processors construct a chain of processing. When you instantiate a telemetry processor, you pass a link to the next processor in the chain. When a telemetry data point is passed to the Process method, it does its work and then calls the next Telemetry Processor in the chain.
49+
1. To create a filter, implement ITelemetryProcessor. This is another extensibility point like telemetry module, telemetry initializer, and telemetry channel.
50+
51+
Notice that Telemetry Processors construct a chain of processing. When you instantiate a telemetry processor, you are given a reference to the next processor in the chain. When a telemetry data point is passed to the Process method, it does its work and then calls (or not calls) the next Telemetry Processor in the chain.
5152

5253
```csharp
5354
using Microsoft.ApplicationInsights.Channel;
5455
using Microsoft.ApplicationInsights.Extensibility;
5556

5657
public class SuccessfulDependencyFilter : ITelemetryProcessor
5758
{
59+
private ITelemetryProcessor Next { get; set; }
5860

59-
private ITelemetryProcessor Next { get; set; }
60-
61-
// You can pass values from .config
62-
public string MyParamFromConfigFile { get; set; }
63-
64-
// Link processors to each other in a chain.
65-
public SuccessfulDependencyFilter(ITelemetryProcessor next)
66-
{
67-
this.Next = next;
68-
}
69-
public void Process(ITelemetry item)
70-
{
71-
// To filter out an item, just return
72-
if (!OKtoSend(item)) { return; }
73-
// Modify the item if required
74-
ModifyItem(item);
61+
// next will point to the next TelemetryProcessor in the chain.
62+
public SuccessfulDependencyFilter(ITelemetryProcessor next)
63+
{
64+
this.Next = next;
65+
}
7566

76-
this.Next.Process(item);
77-
}
67+
public void Process(ITelemetry item)
68+
{
69+
// To filter out an item, return without calling the next processor.
70+
if (!OKtoSend(item)) { return; }
7871

79-
// Example: replace with your own criteria.
80-
private bool OKtoSend (ITelemetry item)
81-
{
82-
var dependency = item as DependencyTelemetry;
83-
if (dependency == null) return true;
72+
this.Next.Process(item);
73+
}
8474

85-
return dependency.Success != true;
86-
}
75+
// Example: replace with your own criteria.
76+
private bool OKtoSend (ITelemetry item)
77+
{
78+
var dependency = item as DependencyTelemetry;
79+
if (dependency == null) return true;
8780

88-
// Example: replace with your own modifiers.
89-
private void ModifyItem (ITelemetry item)
90-
{
91-
item.Context.Properties.Add("app-version", "1." + MyParamFromConfigFile);
92-
}
81+
return dependency.Success != true;
82+
}
9383
}
9484
```
9585

96-
3. Add your processor
86+
2. Add your processor
9787

9888
**ASP.NET apps**
9989
Insert this in ApplicationInsights.config:
@@ -119,7 +109,7 @@ You can pass string values from the .config file by providing public named prope
119109
**Alternatively,** you can initialize the filter in code. In a suitable initialization class - for example AppStart in Global.asax.cs - insert your processor into the chain:
120110

121111
```csharp
122-
var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
112+
var builder = TelemetryConfiguration.Active.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
123113
builder.Use((next) => new SuccessfulDependencyFilter(next));
124114

125115
// If you have more processors:
@@ -130,13 +120,12 @@ builder.Build();
130120

131121
TelemetryClients created after this point will use your processors.
132122

133-
**ASP.NET Core apps**
123+
**ASP.NET Core/ Worker Service apps**
134124

135125
> [!NOTE]
136-
> Adding initializer using `ApplicationInsights.config` or using `TelemetryConfiguration.Active` is not valid for ASP.NET Core applications.
137-
126+
> Adding processor using `ApplicationInsights.config` or using `TelemetryConfiguration.Active` is not valid for ASP.NET Core applications or if you are using Microsoft.ApplicationInsights.WorkerService SDK.
138127
139-
For [ASP.NET Core](asp-net-core.md#adding-telemetry-processors) applications, adding a new `TelemetryInitializer` is done by adding it to the Dependency Injection container, as shown below. This is done in `ConfigureServices` method of your `Startup.cs` class.
128+
For apps written using [ASP.NET Core](asp-net-core.md#adding-telemetry-processors) pr [WorkerService](worker-service.md#adding-telemetry-processors), adding a new `TelemetryProcessor` is done by using `AddApplicationInsightsTelemetryProcessor` extension method on `IServiceCollection`, as shown below. This is done in `ConfigureServices` method of your `Startup.cs` class.
140129

141130
```csharp
142131
public void ConfigureServices(IServiceCollection services)
@@ -151,8 +140,10 @@ For [ASP.NET Core](asp-net-core.md#adding-telemetry-processors) applications, ad
151140
```
152141

153142
### Example filters
143+
154144
#### Synthetic requests
155-
Filter out bots and web tests. Although Metrics Explorer gives you the option to filter out synthetic sources, this option reduces traffic by filtering them at the SDK.
145+
146+
Filter out bots and web tests. Although Metrics Explorer gives you the option to filter out synthetic sources, this option reduces traffic and ingestion size by filtering them at the SDK itself.
156147

157148
```csharp
158149
public void Process(ITelemetry item)
@@ -165,6 +156,7 @@ public void Process(ITelemetry item)
165156
```
166157

167158
#### Failed authentication
159+
168160
Filter out requests with a "401" response.
169161

170162
```csharp
@@ -175,19 +167,21 @@ public void Process(ITelemetry item)
175167
if (request != null &&
176168
request.ResponseCode.Equals("401", StringComparison.OrdinalIgnoreCase))
177169
{
178-
// To filter out an item, just terminate the chain:
170+
// To filter out an item, return without calling the next processor.
179171
return;
180172
}
181-
// Send everything else:
173+
174+
// Send everything else
182175
this.Next.Process(item);
183176
}
184177
```
185178

186179
#### Filter out fast remote dependency calls
180+
187181
If you only want to diagnose calls that are slow, filter out the fast ones.
188182

189183
> [!NOTE]
190-
> This will skew the statistics you see on the portal. The dependency chart will look as if the dependency calls are all failures.
184+
> This will skew the statistics you see on the portal.
191185
>
192186
>
193187
@@ -205,17 +199,18 @@ public void Process(ITelemetry item)
205199
```
206200

207201
#### Diagnose dependency issues
208-
[This blog](https://azure.microsoft.com/blog/implement-an-application-insights-telemetry-processor/) describes a project to diagnose dependency issues by automatically sending regular pings to dependencies.
209202

203+
[This blog](https://azure.microsoft.com/blog/implement-an-application-insights-telemetry-processor/) describes a project to diagnose dependency issues by automatically sending regular pings to dependencies.
210204

211205
<a name="add-properties"></a>
212206

213207
## Add properties: ITelemetryInitializer
214-
Use telemetry initializers to define global properties that are sent with all telemetry; and to override selected behavior of the standard telemetry modules.
208+
209+
Use telemetry initializers to enrich telemetry with additional information and/or to override telemetry properties set by the standard telemetry modules.
215210

216211
For example, the Application Insights for Web package collects telemetry about HTTP requests. By default, it flags as failed any request with a response code >= 400. But if you want to treat 400 as a success, you can provide a telemetry initializer that sets the Success property.
217212

218-
If you provide a telemetry initializer, it is called whenever any of the Track*() methods are called. This includes methods called by the standard telemetry modules. By convention, these modules do not set any property that has already been set by an initializer.
213+
If you provide a telemetry initializer, it is called whenever any of the Track*() methods are called. This includes methods called by the standard telemetry modules. By convention, these modules do not set any property that has already been set by an initializer. Telemetry initializers are called before calling telemetry processors. So any enrichment done by initializers are visible to processors.
219214

220215
**Define your initializer**
221216

@@ -249,9 +244,9 @@ namespace MvcWebRole.Telemetry
249244
// If we set the Success property, the SDK won't change it:
250245
requestTelemetry.Success = true;
251246
// Allow us to filter these requests in the portal:
252-
requestTelemetry.Context.Properties["Overridden400s"] = "true";
247+
requestTelemetry.Properties["Overridden400s"] = "true";
253248
}
254-
// else leave the SDK to set the Success property
249+
// else leave the SDK to set the Success property
255250
}
256251
}
257252
}
@@ -288,7 +283,7 @@ protected void Application_Start()
288283
> [!NOTE]
289284
> Adding initializer using `ApplicationInsights.config` or using `TelemetryConfiguration.Active` is not valid for ASP.NET Core applications.
290285
291-
For [ASP.NET Core](asp-net-core.md#adding-telemetryinitializers) applications, adding a new `TelemetryInitializer` is done by adding it to the Dependency Injection container, as shown below. This is done in `ConfigureServices` method of your `Startup.cs` class.
286+
For apps written using [ASP.NET Core](asp-net-core.md#adding-telemetryinitializers) or [WorkerService](worker-service.md#adding-telemetryinitializers), adding a new `TelemetryInitializer` is done by adding it to the Dependency Injection container, as shown below. This is done in `Startup.ConfigureServices` method.
292287

293288
```csharp
294289
using Microsoft.ApplicationInsights.Extensibility;
@@ -364,25 +359,29 @@ Insert a telemetry initializer immediately after the initialization code that yo
364359

365360
For a summary of the non-custom properties available on the telemetryItem, see [Application Insights Export Data Model](../../azure-monitor/app/export-data-model.md).
366361

367-
You can add as many initializers as you like.
362+
You can add as many initializers as you like, and they are called in the order they are added.
368363

369364
## ITelemetryProcessor and ITelemetryInitializer
365+
370366
What's the difference between telemetry processors and telemetry initializers?
371367

372-
* There are some overlaps in what you can do with them: both can be used to add properties to telemetry.
368+
* There are some overlaps in what you can do with them: both can be used to add properties to telemetry, though it is recommended to use initializers for that purpose.
373369
* TelemetryInitializers always run before TelemetryProcessors.
370+
* TelemetryInitializers may be called more than once. By convention, they do not set any property that has already been set.
374371
* TelemetryProcessors allow you to completely replace or discard a telemetry item.
375-
* TelemetryProcessors don't process performance counter telemetry.
376372

377373
## Troubleshooting ApplicationInsights.config
374+
378375
* Confirm that the fully qualified type name and assembly name are correct.
379376
* Confirm that the applicationinsights.config file is in your output directory and contains any recent changes.
380377

381378
## Reference docs
379+
382380
* [API Overview](../../azure-monitor/app/api-custom-events-metrics.md)
383381
* [ASP.NET reference](https://msdn.microsoft.com/library/dn817570.aspx)
384382

385383
## SDK Code
384+
386385
* [ASP.NET Core SDK](https://github.com/Microsoft/ApplicationInsights-aspnetcore)
387386
* [ASP.NET SDK](https://github.com/Microsoft/ApplicationInsights-dotnet)
388387
* [JavaScript SDK](https://github.com/Microsoft/ApplicationInsights-JS)

0 commit comments

Comments
 (0)