How To Monitor Azure Functions

How To Monitor Azure Functions

In this Azure tutorial, we will discuss How To Monitor Azure Functions. Along with this, we will also discuss a few other topics like How To Configure Azure Monitor Logs For The Azure Function, Create a Log Analytics workspace in the Azure portal and we also discussed Configure Azure Monitor Logs For The Azure Function, Azure Function Logging, and Azure Monitor, Azure Function Console Log.

How To Monitor Azure Functions? You can monitor your Azure Functions using the Azure Monitor Logs.

How To Monitor Azure Functions

Well, here we will discuss How to monitor Azure Functions. It is really important that we should have always the option to monitor your Azure Functions. Azure Function provides an excellent option to integrate your Azure Function with the Azure Monitor logs.

If you will try to understand the Azure Monitor Logs, Basically, Azure Monitor Logs is a feature of the Azure Monitor that actually collects from the different Sources and you can analyze those data whenever needed.

Another important thing is Azure Monitor Logs is based on the Azure Data Explorer. You need to use the Kusto Query Language (KQL) to query the Log details. Azure Monitor Logs collects the data and stores in a Log Analytics workspace that consists of multiple tables and each table actually contains the log data from a specific source.

Depends on your requirement, either you can use a single workspace or you can use multiple workspaces for your Log data. You can also configure to store the Application Insights log data in the Azure Monitor logs. Multiple Applications can also use the same workspace.

You can use the Log Analytics Tool which is available in the Azure Portal to analyze the logs in an interactive way.

How To Configure Azure Monitor Logs For The Azure Function

You can follow the below steps to configure Azure Monitor Logs For the Azure Function. But the first thing we need is, we need to create a Log Analytics workspace for configuring the Log Analytics workspace.

Create a Log Analytics workspace in the Azure portal

Follow the below steps to create a Log analytics workspace in the Azure portal.

  1. Log in to the Azure Portal (https://portal.azure.com/)
  2. Once you logged in to the Azure Portal, search for the Log Analytics workspace and click on the search result
Create a Log Analytics workspace in the Azure portal
monitor azure

3. Now on the Log Analytics workspaces page, click on the + Add button.

How to Create a Log Analytics workspace in the Azure portal
azure functions monitoring

4. On the Create Log Analytics Workspace, Provide the below details

  • Subscription: Choose your correct subscription
  • Resource group: Select your existing Resource group. If you don’t have any existing Resource Group by clicking on the Create new link to create the Resource group.
  • Name: Provide a unique name.
  • Region: Choose the location for the Log Analytics Workspace.

Now you can keep the others tabs option as default and then click on the Review + Create button.

How to Create a Log Analytics workspace using the Azure portal
monitoring azure functions

5. Now on the next screen, It will validate the Information Provided by you and will show you Validation passed and then you click on the create button to create the Log Analytics workspace.

Create a Log Analytics workspace using the Azure portal
functionapplogs

6. Now it will show you that Your deployment is complete. Click on the Go to resource button to see the Log Analytics Workspace that you have created just now.

Create a Log Analytics workspace from the Azure portal
MONITOR DURABLE FUNCTIONS

You can able to see below the Log Analytics Workspace that we have created just now

How to Create a Log Analytics workspace from the Azure portal
Azure Functions monitoring integration

Configure Azure Monitor Logs For The Azure Function

Now, our Log Analytics workspace is ready, We need to configure the Azure Monitor Logs.

  1. Log in to the Azure Portal (https://portal.azure.com/)
  2. Navigate to the Azure Function App for the one you want to configure the Azure Monitor Logs
How To Configure Azure Monitor Logs For The Azure Function
guide to Azure Functions error monitoring

3. The next step is, click on the Diagnostic settings (Preview) option, and then click on the + Add diagnostic setting as highlighted below.

Configure Azure Monitor Logs For The Azure Function
Microsoft Azure Functions Monitoring

4. On the Diagnostics setting page, Provide a name for the Diagnostic settings then select the FunctionAppLogs and Send to Log Analytics option. Once you select the Send to Log Analytics option, You can able to see the Subscription and Log Analytics workspace option. Choose the correct Subscription for you and For the Log Analytics workspace option, choose the Log Analytics workspace that we have created above.

5. Finally click on the Save button.

Configure Azure Monitor Logs For The Azure Function in Azure Portal
Monitor Azure Functions

6. Now again, if you will navigate to the Azure Function App and click on the Diagnostic settings (Preview) option, you can able to see that it has been successfully created as highlighted below.

Configure Azure Monitor Logs For The Azure Function using Azure Portal

Query the logs

Now to query the logs that are generated, you need to follow the below steps

  • Navigate to the Azure Function App, From the left navigation, click on the Diagnostic settings (preview) and click on the Log Analytics workspace that you configured it just before.
Configure Azure Monitor Logs For The Azure Functions
  • From the Log Analytics workspace page, click on the Logs from the left navigation and you can able to see the FunctionAppLogs table as highlighted below. Azure Functions writes all logs to the FunctionAppLogs table.
How to Configure Azure Monitor Logs For The Azure Functions
  • You can write the query in the Query window and click on the Run button to execute the Query or you can double click on the FunctionAppLogs and click on the Run button. If you want to retrieve the log details for the specific Azure Function, then, you can paste the below query in the Query window and click on the Run button.
FunctionAppLogs
| where FunctionName == "YourAzureFunctionName"
  • You can also get the complete log details using the below query.
FunctionAppLogs
| order by TimeGenerated desc
  • You can see it here
How to Query the Azure Monitor logs

Azure Function Logging

By Default, the Ilogger instance was present with the Azure Function Signature. The Azure Function with ILogger instance looks like below

[FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)

You can see the same as below

Azure Function Logging

The instance of ILogger will be there as the parameter of the Azure Function as shown above.

With ILogger instance, you can log information, LogError, LogWarning, LogDebug, LogTrace, and LogCritical, etc. You can take a look below.

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            log.LogError("Error details");
            log.LogWarning("Warning Details");
            log.LogDebug("Log Debug Details");
            log.LogTrace("Log Trace Details");
            log.LogCritical("LogCritical details");

Along with the information you can append the actual value as well For Example, the actual Exception Details.

Above are the example of the Azure Function with the C# language.

In case of the JavaScript Azure Function, you can implement in the following ways

module.exports = async function(context, myTrigger, Input1, Input2)

The context object has a log() function and a log object, you call the appropriate log functions based on your requirement.


context.log.warn( "Warning Details" )
context.log.error( "Error details" )
context.log.info( "Information Details" )
context.log.verbose( "Details" )
context.log( "Put the Details")

Azure Monitor

Azure Monitor is a tool from Microsoft that helps you to collect and analyze the data from the different Azure resources. Mainly, it helps you with the information, How is your Application performing or Any issue is there with your Application that might affect the performance of your Application.

Azure Monitor can collect different categories of data from the difference sources like below

  • Data of the Application monitoring: This is data for the performance of the code you have written.
  • Data from the Azure Resource monitoring: Data from different Azure resources in terms of their operation.
  • Data from the Azure Subscription monitoring: This is the data about the management of the Azure Subscription.
  • Data from the Azure Tenant monitoring: This data is all about the operational data from different tenant-level Azure services.

What You Can Do With Azure Monitor

You might be thinking of what exactly we can do with the Azure Monitor. Below is the few key usages of the Azure Monitor.

  • Find out different issues and diagnose them from different applications and dependencies with the Application insights.
  • With the help of the Azure Monitor tool, you can analyze deeper and troubleshoot the data with the Log Analytics.
  • It helps you to create the visualizations with the Azure dashboards and workbooks.
  • Azure Monitor also helps you to Correlate the infrastructure issues with the Azure VMs.

Azure monitors for your Azure Virtual machines analyzes your Virtual machine performance and health condition.

Application insights help you to monitor your web application availability, performance, and usage, etc. Here, the Azure Monitor helps you to deeply analyze the operation of your applications and help you to diagnose the errors without waiting for the user’s to report those issues.

There is also a set of logic that provides insight for a specific application or specific Azure Service that is termed as a Monitoring solution in the case of Azure Monitor. They are responsible to collect the monitoring data for the application or services, they also provide the queries to analyze those data.

Azure Function Console Log

When you are working with the Azure Function in Visual Studio, Once you will create the Azure Function if you will closely look at the Function code, you can able to see that the ILogger injected into your Function that you can use for the logging functionality.

When you press F5 to run the Azure Function locally, you will notice that this logger will display the messages in the console window of your Visual Studio IDE.

But, Once you will deploy the Azure Function to the Azure, You will not see the Visual Studio console window which is obvious. So that time you need to connect to the Azure Application Insight to your Azure Function to capture these logs.

Azure Function ilogger Application Insights

While working with the Azure function, it’s a great idea to implement the logging to track what’s going on with the Azure function. For that we need to enable the Application insights for the Azure Function and in this case, with the use of iLogger interface, we can able to send the logs to the application insights.

How to Configure Application Insights Logging

As we know already, Application Insights is an excellent option to store your logs related to exception, Information, etc. So it is always a great idea to configure the Application Insights option for your Azure Function. So, Configure the Azure Application Insight for your Azure Function now.

In Built Report

The inbuilt performance report for your Azure Function is really looking great with some key information like below. It provides a report on the below key factors

  • The Successful and failed requests that are processed by the Azure Function Instance.
  • How many exceptions exactly been thrown with the Azure Functions.
  • The resources allocated to your Azure Functions.

The default performance reports for Azure Functions are quite good for real-time performance monitoring, even locally. It gives a great view of:

  • the resources allocated to the Functions;
  • how many successful and failed requests have been processed by the Function instance
  • how many exceptions have been thrown
  • The overall health of your Azure Function.
Azure Function Console Log
azure functions application insights logging

Build Your Custom Reports

If you are still not happy with the Build in Reports, you can build your Custom Reports with the Additional information based on your requirements. You can use different quires to build your custom reports based on your business need.

By just enabling application insights, without any further addons we get all the logs that are sent to the ILogger object to application Insights. And we also get some End-To-End tracking via supported SDK’s and HttpClient requests. This is very useful to get an understanding of what is happening. So let’s look in to how this manifests in a rather complex scenario.

You may also like following the below articles

Wrapping Up

Well, in this article, we discussed, How To Monitor Azure Functions, How To Configure Azure Monitor Logs For The Azure Function, Create a Log Analytics workspace in the Azure portal and we also discussed Configure Azure Monitor Logs For The Azure Function, Azure Function Logging, and Azure Monitor, Azure Function Console Log, Azure Function ilogger Application Insights. Hope you have enjoyed this article !!!