Application Insights comes enabled by default for Azure Functions. It allows you to monitor what's happening inside your functions for a cost that varies based on the amount of data you send it. That got me thinking about the different ways to maximize the return on that investment.
Microsoft like to tout Application Insights as the best way to understand what's going on inside your serverless applications. That's part of the answer, but not the whole story, especially when it comes to larger, more complex apps. With that in mind, let's look at some of the other ways to get the most out of Application Insights.
The total number of executions above the first million.
The execution time, calculated by multiplying the consumed memory by the execution time in seconds of a function.
The metrics for both of those aspects can be easily observed in Application Insights, and concrete actions can be taken to lower the monthly cost you're paying for those functions.
The total number of executions can be reduced by adding a cache in front of HTTP-based functions. Troy Hunt does this to incredible success for his Have I Been Powned service. The cost of caching with a provider like Cloudflare is insignificant next to the cost of the equivalent function execution.
It's also worthwhile to spend some time looking at the memory profile of your functions. Memory usage is rounded to the closest 128Mb, so keep an eye on memory-intensive operations like retrieving and manipulating large data sets. It'll go a long way to reducing the cost of your functions.
And finally, observe any anomalies on execution time. If your functions are running for more than a few seconds at a time, you might want to look into any potential performance bottlenecks, such as I/O operations, or slow downstream services that are costing you more money on every execution.
One of the biggest advantages to the serverless architecture is that infrastructure, hardware, and platform failures are handled entirely by the Azure Functions team. The only thing that developers need concern themselves with are problems within their own function's code.
Application Insights takes you from being reactive to proactive by monitoring a bunch of metrics for you. Anything that exceeds a predefined threshold triggers an email notification to which you can take appropriate action.
There are at minimum two types of errors that you should be alerted for: discrepancies in response time and the frequency of exceptions. A sudden increase in response time or total number of exceptions indicates that something abnormal is happening inside your functions, and bears investigation. The former could have an impact on your monthly bill, whilst the latter could result in unhappy customers.
Last but not least, Application Insights can observe custom events and metrics that occur within your serverless functions, providing you with a better view of what's happening at a given time.
Let's clarify the true power of this point by way of an example. Let's say we're operating an e-commerce business built entirely on Azure Functions. The different components of the system, such as ordering, billing and shipping are each broken up into separate Function Apps, which are each composed of multiple functions to accomplish different tasks.
Determining the average processing time from start to finish in a distributed system like this can be hard to accomplish, since each Function App is only responsible for keeping track of its own internal state.
With Application Insights you can track a metric after every order is placed, billed, and shipped that will tell you how long each step has taken. Calculating the total processing time then becomes a matter of running a query in the analytics explorer to get an average of the processing time across all three Function Apps.
This only scratches the surface of what you can accomplish with events and metrics, but it gives you an idea of the dashboards and graphs that can be created with it.