Monitoring Azure Functions

Introduction to Monitoring

Effective monitoring is crucial for understanding the health, performance, and usage of your Azure Functions. It allows you to detect issues early, optimize performance, and ensure your applications are running reliably.

Azure Functions integrates deeply with Azure Monitor, providing comprehensive insights into your function executions, resource utilization, and potential errors.

Key Monitoring Tools and Metrics

Azure Application Insights

Application Insights is the primary tool for monitoring Azure Functions. It provides:

You can enable Application Insights by configuring it in your Function App's settings or during creation.

Application Insights Dashboard Example

Example of an Application Insights dashboard for Azure Functions.

Azure Monitor Logs (Log Analytics)

When your Function App is configured with Application Insights, logs are sent to a Log Analytics workspace. This allows you to query your logs using Kusto Query Language (KQL) for deeper analysis and custom troubleshooting.

Common log tables include:

-- Example KQL query to find failed function executions in the last hour
requests
| where timestamp > ago(1h)
| where success == false
| project timestamp, name, resultCode, duration
| order by timestamp desc

Azure Monitor Metrics

Azure Monitor provides a rich set of metrics for your Function App, including:

These metrics can be viewed in the Azure portal and used to set up alerts.

Configuring Logging

Azure Functions can be configured to log different levels of detail:

You can configure the logging level in your Function App's host.json file:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "default": "Information",
      "Host.Results": "Information",
      "Function.MyFunctionName": "Debug"
    }
  }
}

Using context.log() or ILogger in your function code allows you to emit custom messages that will appear in Application Insights or Log Analytics.

Setting Up Alerts

Proactive alerting is essential for responding quickly to issues. You can configure alerts in Azure Monitor based on metrics and log queries.

Alerting on Metrics

Create alerts for conditions like:

Alerting on Logs

Create alerts based on KQL queries, for example:

Alerts can trigger notifications via email, SMS, webhooks, or other actions.

Best Practices for Monitoring