Monitoring Azure Functions

This tutorial guides you through the essential aspects of monitoring your Azure Functions applications to ensure they are healthy, performant, and reliable.

Introduction

Effective monitoring is crucial for understanding the behavior of your serverless applications, diagnosing issues, and optimizing performance. Azure Functions integrates deeply with Azure's monitoring services, providing comprehensive insights into your function executions.

Key Monitoring Tools

Azure offers a suite of powerful tools for monitoring your functions:

Application Insights

Application Insights is your primary tool for detailed monitoring of Azure Functions. It automatically captures a wealth of telemetry data.

Key Features:

Enabling Application Insights:

Application Insights is typically enabled by default when you create an Azure Function App. If not, you can link it manually through the Azure portal.

Ensure your host.json file is configured to send logs to Application Insights if you're not using the default integration.


{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}
                

Azure Monitor

Azure Monitor provides a broader view of your Azure resources, including metrics and logs for your Function App.

Metrics:

Logs:

You can query logs stored by Application Insights using Kusto Query Language (KQL) in the Azure portal's Log Analytics workspace.

Example KQL query to see function execution logs:


requests
| where success == "false"
| project timestamp, name, url, resultCode, duration, cloud_RoleName, itemType
| order by timestamp desc
            

Logging Strategies

Effective logging is key to debugging and monitoring.

Example of structured logging:


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

public static class MyFunction
{
    [FunctionName("MyFunction")]
    public static void Run([QueueTrigger("myqueue-items")] string myQueueItem, ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

        // Simulate some processing
        var data = JsonConvert.DeserializeObject(myQueueItem);
        var correlationId = data?.correlationId ?? Guid.NewGuid().ToString();
        var processingResult = new { status = "success", processedItems = 1 };

        log.LogInformation($"Processing completed. CorrelationId: {correlationId}", new { correlationId, processingResult });
    }
}
            

Alerting

Set up alerts to be notified proactively when critical events occur.

Common Alerts:

You can configure alerts directly within the Azure portal under the "Alerts" section of your Function App or Log Analytics workspace.

Performance Tuning

Monitor your functions to identify areas for performance improvement.

Next Steps

Explore the detailed documentation for Application Insights and Azure Monitor to master advanced querying and alerting techniques.