Monitoring Azure Functions

This tutorial guides you through the process of monitoring your Azure Functions. Effective monitoring is crucial for understanding the performance, availability, and health of your serverless applications.

Key Monitoring Tools

Azure Functions integrates deeply with several Azure services to provide comprehensive monitoring capabilities:

  • Application Insights: Provides rich telemetry about function execution, performance, and failures.
  • Azure Monitor Logs (Log Analytics): Allows you to query execution logs and custom telemetry data.
  • Azure Portal Metrics: Offers real-time and historical performance metrics for your functions.

Getting Started with Application Insights

Application Insights is the primary tool for monitoring Azure Functions. It automatically collects data such as requests, dependencies, exceptions, and traces.

Enabling Application Insights

When you create an Azure Function App, you can enable Application Insights integration. If it's not enabled, you can add it later:

  1. Navigate to your Function App in the Azure portal.
  2. Under Settings, select Application Insights.
  3. Click Turn on Application Insights.
  4. Select an existing resource or create a new one.

Viewing Telemetry Data

Once Application Insights is configured, you can access its features:

  • Overview: A dashboard showing key metrics like requests, failures, and server response time.
  • Failures: Detailed information about exceptions and failed requests.
  • Performance: Analysis of operation durations and dependencies.
  • Live Metrics: Real-time telemetry streaming.
  • Logs: Powerful query interface (Kusto Query Language - KQL) to explore detailed logs.

Using Azure Monitor Logs (Log Analytics)

For advanced querying and analysis, Azure Monitor Logs is invaluable. You can write KQL queries to extract specific information from your function execution logs.

Common Log Queries

Here are some example KQL queries you might use:


// All function invocations in the last hour
requests
| where timestamp > ago(1h)
| order by timestamp desc

// Function executions that resulted in an exception
exceptions
| where timestamp > ago(1h)
| project timestamp, message, details, outerMessage

// Count of invocations per function name in the last day
requests
| where timestamp > ago(1d)
| summarize count() by name
| order by count_ desc
                

Function App Metrics

The Azure portal provides basic metrics for your Function App, including:

  • Function Execution Count: The number of times your functions have been executed.
  • Http Server Errors: Number of 4xx and 5xx responses.
  • Memory Working Set: Memory usage of the Function App.
  • Cpu Time: CPU utilization.

These metrics can be viewed under the Overview page of your Function App and can be pinned to your Azure dashboard.

Custom Logging

You can add custom logging within your function code to provide more context for troubleshooting.

For C#, use the ILogger interface:


using Microsoft.Extensions.Logging;

public static class MyFunction
{
    [FunctionName("MyHttpTrigger")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        // ... function logic ...
        log.LogWarning("A potential issue was detected.");
        return new OkObjectResult("Welcome to Azure Functions!");
    }
}
                

For JavaScript, use context.log:


module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    // ... function logic ...
    context.log.warn('A potential issue was detected.');
    context.res = {
        body: "Welcome to Azure Functions!"
    };
};
                

Alerting

Configure alerts in Azure Monitor to notify you when specific conditions are met, such as a high error rate or a dip in function executions.

  • Navigate to Monitor in the Azure portal.
  • Select Alerts.
  • Click Create alert rule.
  • Define the scope (your Function App), conditions (metric or log query), and actions (email, webhook, etc.).

Conclusion

By leveraging Application Insights, Azure Monitor Logs, and custom logging, you can gain deep insights into your Azure Functions' behavior, ensuring reliability and performance.