Digging deeper again - querying Application Insights data

In the one of the previous post I presented you the basics of using Analytics in Application Insights. Just to remind you - it's a tool, which allows you to get much more detailed information regarding how your application perform and behaves. In this post I'll try to encourage you to use this tool on daily basis, especially when something is clearly wrong and you don't want to click through all those tabs in Azure Portal.

Quickly finding an exception

Let's consider following example - you're informed by your client, that he or she got an error saying Object reference not set to an instance of an object. You know the time and assume, that we're facing NullReferenceException here. What could you do to quickly find more details regarding this exception? Let's consider following query:

/
exceptions
    | where timestamp >= ago(24h) 
        and type == "System.NullReferenceException" 
        and outerAssembly 
            contains "MyProject" 
    | project method, details, timestamp 

This query will project a simple result containing a method in which an exception was thrown, details(like inner stack) and the exact time when it occurred. You can modify it and use different timestamp to get results, which will satisfy you the most.

Finding pages which are loading too slow

This is another example - you have a SPA and you'd like to know, which pages are loading longer, that a fixed threshold. Fortunately we can use a really simple query, which can help us here:

/
pageViews
    | where duration > 500

Yes, this is really it. Of course you can extend this query and use other metrics to further filter the data like:

/
pageViews
    | where duration > 50
        and operation_Name == "some_operation"
        and timestamp > ago(1h)  

Or even visualize your data to be aware which slower requests are really the slowest ones:

/
pageViews
    | where duration >= 250
    | render piechart  

Custom metrics

You can use predefined metrics to gather some information about your application, but where this tool really shines is the possibility to query custom metric. This allows you to easily build your own reports and really focus on what you're interested in.

For example I'd like to know how many times given method is called so I can be aware of all potential hot-paths and focus my optimization efforts on it. To do this I need two things:

  • a tool, which will decorate methods calls with a custom metrics logging(AOP/assembly weaving will help here)
  • custom query in Application Insights Analytics

Let's say, that this decorator will log one thing - a method name. Now in Analytics we could create a query, which would use this custom metric to show us which method is called the most.

Application Insights Analytics - digging deeper into your application metrics

Data which is provided by an instance of Application Insights connected with your application is in most cases more than enough. As long as you're logging satisfying amount of information, you can easily track all your metrics and diagnose problems with ease. But what if you'd like to get a deeper insight into "what is really going on there"? I guess it'd possible to use AI's REST API and fetch all the data into your custom tool(or any kind of 3rd-party software) to analyze it - but who needs it when you have Application Insights Analytics?

First look

To access Analytics you need only to access this link - https://analytics.applicationinsights.io. When accessed, you'll see a welcome screen, which out-of-the-box allows you to access some common queries.

A welcome screen gives you a rapid start when it comes to analyze common statistics

I strongly recommend you to try out common queries - they allow you to quickly get an overview of the capabilities of this tool.

Querying the data

For sure you'll notice, that charts and other statistics are the result of a query. This is what makes Analytics a really powerful tool - you can query any kind of metric available to you(like dependency duration, custom events, client OS and many many more) and combine them to get a what you're looking for.

An expanded tab of traces of the left - still there're some missing on the screen...

What is more, when creating or editing a query you can take advantage of inbuilt editor, which helps with a syntax and highlights all your errors. It's definitely much more polished than the one from the Function Apps :)

You can easily add metrics from the tabs on the left and then use an intuitive editor to combine them

Smart Diagnostics

There's a one cool feature, which makes Analytics really helpful in searching the root cause of a problem - Smart Diagnostics. It allows you to quickly discover what is "strange" in this particular fragment of your log(maybe one dependency fails to respond or responds three times longer than usual). 

Those highlighted dots on the chart allow you to run a smart detection on those parts of the data, which doesn't match the rest.

You have to be aware of the fact, that this diagnostics is not perfect and relies on the data you provide(so if you provide not enough data, it will tell you, that something is wrong, but not what it exactly is). Nonetheless is encourage you to gather more and more data, so its proposals are more and more valid and precise.

In the next post we'll try to run more advanced queries and find where limits of this tool are.