Querying 404's using Kusto and Log Analytics in Azure Application Insights
Niels Swimberghe - - Azure
Follow me on Twitter, buy me a coffee
When you integrate Azure Application Insights into your web applications, a lot of telemetry is captured and made available for querying and visualizing. HTTP requests are one of those datapoints stored in the underlying Log Analytics workspace.
Over time websites evolve and so do their URL's. One challenge in maintaining websites is finding broken URL's that no longer exist and providing redirects to the new location of the content. Using Application Insights and its built-in Log Analytics workspace, you can easily query HTTP requests that resulted in a 404 HTTP Status response code.
Querying 404's #
Navigate to your Application Insights resource in the Azure Portal:
Navigate to the 'Logs' section:
Inside of the query editor, copy/paste the following Kusto query:
requests | where resultCode == 404 | summarize Count=sum(itemCount) by url | sort by Count
This query does the following:
- fetch requests from the 'requests' table. This is the table where HTTP requests are stored.
- filter to requests that resulted in a 404 HTTP Status response.
- Group the requests by 'url'. Sum-aggregate the request rows by the 'itemCount' property. 'itemCount' will be 1 or higher depending on how the requests were sampled. If similar requests were sampled, they are rolled into a single row and the 'itemCount' is increased.
- Sort descending by the aggregated 'Count'.
The result will be a table where every URL represents a row and you can see the amount of requests made to that URL resulting in 404. You can use this data to find your biggest 404-issues and then manually figure out how to resolve them.
404's don't have to necessarily be resolved. When a URL doesn't have any content and there's no new location where that content lives, a 404 is the correct response. Though from a business perspective, that may not be the desired user experience.
Hopefully, this helps you improve your websites!