Skip to content

Filters (2.x)

Trask Stalnaker edited this page Jan 20, 2021 · 1 revision

Filters serve as a way to reduce traffic from the SDK.

You can filter which telemetry gets sent to Application Insights based on : • Trace severity level • Filter out specific URLs, keywords or response codes • Filter out requests that take less than a configured time threshold (short requests) • Specific event names • Custom filter (code the logic)

There are two ways you can add filters. The filters go under a new XML tag "TelemetryProcessor" in the ApplicationInsights.xml file?. There you can choose and customize the built in ones and/or new custom filters For example, <ApplicationInsights> <TelemetryProcessors>

  `<BuiltInProcessors>`
       `<Processor type="TraceTelemetryFilter">`
              `<Add name="FromSeverityLevel" value="ERROR"/>`
       `</Processor>`

       `<Processor type="RequestTelemetryFilter">`
              `<Add name="MinimumDurationInMS" value="100"/>`
              `<Add name="NotNeededResponseCodes" value="200-400"/>`
       `</Processor>`

       `<Processor type="PageViewTelemetryFilter">`
              `<Add name="DurationThresholdInMS" value="100"/>`
       `</Processor>`

      `<Processor type="SyntheticSourceFilter"/>`

 `</BuiltInProcessors>`

 `<CustomProcessors>`
    `<Processor type="com.microsoft.applicationinsights.sample.SampleFilter">`
        `<Add name="Pass" value="false"/>`
    `</Processor>`
`</CustomProcessors>`

</TelemetryProcessors> </ApplicationInsights>

In this example the configuration file declares four built in filters and one custom one.

Custom Filters

To create a custom filter you need to implement the TelemetryProcessor interface The method to implement is 'public boolean process(Telemetry telemetry)' where a telemetry is passed to the method which returns a boolean value: 'True' - The telemetry is 'approved'. Note that other filters can veto the telemetry which will prevent it from being sent 'False' - The telemetry is not approved. This means the telemetry will not be sent The filter can get data from configuration by having a 'setXXX' method that matches its property name. For example, if the filter has a property 'percentage', it should have a 'setPercentage(string percentage)' method and then you can pass data from configuration like this:

Please note that the SDK will ignore a filter that threw any exception thrown during initialization.

A sample filter can be found here

Built In Filters

The framework also sets its own built in filters to ease the work of users. The following are the current 'built in' filters

TraceTelemetryFilter

This filter can filter out log telemetries. In our above example, only logs with severity level of Error and above (Critical) will be sent. This is a good way to have your app produce logs in lower severity level but on the same time, send only the ones that will be fully helpful

PageViewTelemetryFilter

This filter can filter out PageViewTelemetries. You can set the 'NotNeededUrls' to collection of urls to prevent them from being sent. For example: which means that Urls that contain those keywords will not be sent You can set the 'NotNeededNames' much in the same way to prevent PageView with those names You can set the 'DurationThresholdInMS' to filter out PageViews that had a duration of less than this value

RequestTelemetryFilter

This filter can filter out RequestTelemetries You can set the 'NotNeededResponseCodes' to prevent from requests with those response codes to be sent. For example You can set the 'MinimumDurationInMS' to send only 'slow' requests.

SyntheticSourceFilter

This filter can filter out all telemetries that are originated in a synthetic source.

You can also set the source names to block like this

TelemetryEventFilter

This filter can filter out EventTelemtries This filter can be useful in case you use the SDK for creating Event Telemetries but then for some reason you would like to prevent some of them from being sent without re-compiling your code. You can select event names to block like this