You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Filtering and preprocessing telemetry in the Application Insights SDK
19
19
20
+
You can write and configure plug-ins for the Application Insights SDK to customize how telemetry can be enriched and processed before it's sent to the Application Insights service.
20
21
21
-
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.
22
-
23
-
*[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.
25
-
*[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.
22
+
*[Sampling](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.
23
+
* Filtering with Telemetry Processors lets you filter out 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. 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
+
*[Telemetry Initializers add or modify 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.
26
25
*[The SDK API](../../azure-monitor/app/api-custom-events-metrics.md) is used to send custom events and metrics.
27
26
28
27
Before you start:
29
28
30
-
* Install the Application Insights [SDK for ASP.NET](../../azure-monitor/app/asp-net.md) or [SDK for Java](../../azure-monitor/app/java-get-started.md) in your app.
29
+
* Install the appropriate SDK for you application. [ASP.NET](asp-net.md) or [ASP.NET Core](asp-net-core.md) or [Non HTTP/Worker for .NET/.NET Core](worker-service.md) or [Java](../../azure-monitor/app/java-get-started.md) in your app.
31
30
32
31
<aname="filtering"></a>
33
32
34
33
## 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.
36
34
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.
35
+
This technique gives you 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.
36
+
37
+
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 give it to the next processor in the chain. This includes telemetry from the standard modules such as the HTTP request collector and the dependency collector, and telemetry you have tracked yourself. You can, for example, filter out telemetry about requests from robots, or successful dependency calls.
38
38
39
39
> [!WARNING]
40
40
> 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,59 +44,48 @@ To filter telemetry, you write a telemetry processor and register it with the SD
44
44
>
45
45
46
46
### 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.
49
-
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.
1. To create a filter, implement `ITelemetryProcessor`.
78
49
79
-
// Example: replace with your own criteria.
80
-
privateboolOKtoSend (ITelemetryitem)
81
-
{
82
-
vardependency=itemasDependencyTelemetry;
83
-
if (dependency==null) returntrue;
50
+
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.
**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:
105
+
**Alternatively,**youcaninitializethefilterincode. Inasuitableinitialization class - for example AppStart in `Global.asax.cs` - insert your processor into the chain:
TelemetryClients created after this point will use your processors.
132
118
133
-
**ASP.NET Core apps**
119
+
**ASP.NET Core/ Worker Service apps**
134
120
135
121
> [!NOTE]
136
-
> Adding initializer using `ApplicationInsights.config` or using `TelemetryConfiguration.Active` is not valid for ASP.NET Core applications.
137
-
122
+
> 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.
138
123
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.
124
+
For apps written using [ASP.NET Core](asp-net-core.md#adding-telemetry-processors)or [WorkerService](worker-service.md#adding-telemetry-processors), adding a new `TelemetryProcessor` is done by using `AddApplicationInsightsTelemetryProcessor` extension method on `IServiceCollection`, as shown below. This method is called in `ConfigureServices` method of your `Startup.cs` class.
@@ -151,8 +136,10 @@ For [ASP.NET Core](asp-net-core.md#adding-telemetry-processors) applications, ad
151
136
```
152
137
153
138
### Example filters
139
+
154
140
#### 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.
141
+
142
+
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.
156
143
157
144
```csharp
158
145
publicvoidProcess(ITelemetryitem)
@@ -165,6 +152,7 @@ public void Process(ITelemetry item)
165
152
```
166
153
167
154
#### Failed authentication
155
+
168
156
Filter out requests with a "401" response.
169
157
170
158
```csharp
@@ -175,19 +163,21 @@ public void Process(ITelemetry item)
// To filter out an item, just terminate the chain:
166
+
// To filter out an item, return without calling the next processor.
179
167
return;
180
168
}
181
-
// Send everything else:
169
+
170
+
// Send everything else
182
171
this.Next.Process(item);
183
172
}
184
173
```
185
174
186
175
#### Filter out fast remote dependency calls
176
+
187
177
If you only want to diagnose calls that are slow, filter out the fast ones.
188
178
189
179
> [!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.
180
+
> This will skew the statistics you see on the portal.
191
181
>
192
182
>
193
183
@@ -205,17 +195,18 @@ public void Process(ITelemetry item)
205
195
```
206
196
207
197
#### 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.
209
198
199
+
[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.
210
200
211
201
<aname="add-properties"></a>
212
202
213
203
## 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.
215
204
216
-
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.
205
+
Use telemetry initializers to enrich telemetry with additional information and/or to override telemetry properties set by the standard telemetry modules.
217
206
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.
207
+
For example, the Application Insights for Web package collect 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.
208
+
209
+
If you provide a telemetry initializer, it is called whenever any of the Track*() methods are called. This includes `Track()` 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 enrichments done by initializers are visible to processors.
[See more of this sample.](https://github.com/Microsoft/ApplicationInsights-Home/tree/master/Samples/AzureEmailService/MvcWebRole)
285
277
286
-
**ASP.NET Core apps: Load your initializer**
278
+
**ASP.NET Core/ Worker Service apps: Load your initializer**
287
279
288
280
> [!NOTE]
289
-
> Adding initializer using `ApplicationInsights.config` or using `TelemetryConfiguration.Active` is not valid for ASP.NET Core applications.
281
+
> Adding initializer using `ApplicationInsights.config` or using `TelemetryConfiguration.Active` is not valid for ASP.NET Core applications or if you are using Microsoft.ApplicationInsights.WorkerService SDK.
290
282
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.
283
+
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.
292
284
293
285
```csharp
294
286
usingMicrosoft.ApplicationInsights.Extensibility;
@@ -364,25 +356,62 @@ Insert a telemetry initializer immediately after the initialization code that yo
364
356
365
357
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).
366
358
367
-
You can add as many initializers as you like.
359
+
You can add as many initializers as you like, and they are called in the order they are added.
360
+
361
+
### Example TelemetryInitializers
362
+
363
+
#### Add custom property
364
+
365
+
The following sample initializer adds a custom property to every tracked telemetry.
What's the difference between telemetry processors and telemetry initializers?
371
395
372
-
* There are some overlaps in what you can do with them: both can be used to add properties to telemetry.
396
+
* There are some overlaps in what you can do with them: both can be used to add or modify properties of telemetry, though it is recommended to use initializers for that purpose.
373
397
* TelemetryInitializers always run before TelemetryProcessors.
398
+
* TelemetryInitializers may be called more than once. By convention, they do not set any property that has already been set.
374
399
* TelemetryProcessors allow you to completely replace or discard a telemetry item.
375
-
* TelemetryProcessors don't process performance counter telemetry.
400
+
* All registered TelemetryInitializers are guaranteed to be called for every telemetry item. For Telemetry processors, SDK guarantees calling the very first telemetry processor. Whether the rest of the processors are called or not, is decided by the preceding telemetry processors.
401
+
* Use TelemetryInitializers to enrich telemetry with additional properties, or override existing one. Use TelemetryProcessor to filter out telemetry.
376
402
377
403
## Troubleshooting ApplicationInsights.config
404
+
378
405
* Confirm that the fully qualified type name and assembly name are correct.
379
406
* Confirm that the applicationinsights.config file is in your output directory and contains any recent changes.
0 commit comments