Application Insights es una excelente herramienta de monitoreo, pero considera a todos los status codes 4xx y 5xx como errores, y al escribir una API REST algunos de estos códigos tienen un significado particular y no son errores. Una respuesta con 404 (Not found) en una API REST normalmente significa que no hubo resultados para un dado recurso, no que solicitamos una página inexistente.
Así que, ¿cómo le decimos a Application Insights que ignore esos 404? Simple: creamos lo que se llama un Telemetry processor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.ApplicationInsights.Channel; | |
using Microsoft.ApplicationInsights.DataContracts; | |
using Microsoft.ApplicationInsights.Extensibility; | |
namespace Samples | |
{ | |
public class My404Filter : ITelemetryProcessor | |
{ | |
private ITelemetryProcessor Next { get; set; } | |
public My404Filter(ITelemetryProcessor next) | |
{ | |
Next = next; | |
} | |
public void Process(ITelemetry item) | |
{ | |
// To filter out an item, just return | |
if (ShouldIgnoreRequest(item)) | |
{ | |
return; | |
} | |
Next.Process(item); | |
} | |
private static bool ShouldIgnoreRequest(ITelemetry item) | |
{ | |
if (item.Context.Operation.Name != null) | |
{ | |
var operationName = item.Context.Operation.Name.ToLower(); | |
if (operationName.StartsWith("get api/getobjectwith404s")) | |
{ | |
var req = item as RequestTelemetry; | |
return req != null && req.ResponseCode.Equals("404", StringComparison.OrdinalIgnoreCase); | |
} | |
return false; | |
} | |
return false; | |
} | |
} | |
} |
Luego necesitamos agregar este processor a la configuración de telemetría de Application Insights, para eso usaremos una clase helper:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
using Microsoft.ApplicationInsights.Extensibility; | |
namespace Samples.App_Start | |
{ | |
public static class AppInsightsConfig | |
{ | |
public static void RegisterAppInsights() | |
{ | |
TelemetryConfiguration.Active.InstrumentationKey = | |
System.Web.Configuration.WebConfigurationManager.AppSettings["ApplicationInsightsKey"]; | |
var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder; | |
builder.Use(next => new My404Filter(next)); | |
builder.Build(); | |
} | |
} | |
} |
Y finalmente la llamamos en nuestro global.asax
:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Optimization; | |
using System.Web.Routing; | |
using Samples.App_Start; | |
namespace Samples | |
{ | |
public class MvcApplication : HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AppInsightsConfig.RegisterAppInsights(); | |
AreaRegistration.RegisterAllAreas(); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
} | |
} | |
} |
Ahora no veremos más esos errores 404 en Application Insights.