Reputation: 131
We are using smart detectors in Azure App Insights to generate some alerts when there are anomalies in our app. However, there are some failures that are intentional in our code, where we throw a 403. Is there a way to modify these "Smart Alerts" in Application Insights, so that these known failures can be excluded in its detection logic? We have a specific exception type that is relevant for these expected failures that we can easily use to exclude these in the anomaly detection if there is a way to do that, but I can't find an option on the UI to do this.
Thanks for any pointers.
Upvotes: 0
Views: 437
Reputation: 8252
You cannot do that directly from the Azure portal but you need to implement a Telemetry Processor which can help you override telemetry properties set.
If request flag as failed with a response code = 403. But if you want to treat it as a success, you can provide a telemetry initializer that sets the success property.
Define your initializer
C#
using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace MvcWebRole.Telemetry
{
/*
* Custom TelemetryInitializer that overrides the default SDK
* behavior of treating response codes >= 400 as failed requests
*
*/
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
// Is this a TrackRequest() ?
if (requestTelemetry == null) return;
int code;
bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
if (!parsed) return;
if (code >= 400 && code < 500)
{
// If we set the Success property, the SDK won't change it:
requestTelemetry.Success = true;
// Allow us to filter these requests in the portal:
requestTelemetry.Properties["Overridden400s"] = "true";
}
// else leave the SDK to set the Success property
}
}
}
In ApplicationInsights.config:
XMLCopy
<ApplicationInsights>
<TelemetryInitializers>
<!-- Fully qualified type name, assembly name: -->
<Add Type="MvcWebRole.Telemetry.MyTelemetryInitializer, MvcWebRole"/>
...
</TelemetryInitializers>
</ApplicationInsights>
For more information, you can refer to this Document.
Upvotes: 1