MSDN Documentation

Azure Functions | Monitoring

Monitoring Azure Functions

Effective monitoring is crucial for understanding the health, performance, and usage patterns of your Azure Functions. This section covers the primary tools and techniques for monitoring your serverless applications.

Azure Application Insights

Azure Application Insights is the central hub for monitoring your Azure Functions. It provides comprehensive telemetry, analytics, and alerting capabilities.

Key Features:

Setting up Application Insights:

When you create an Azure Function app, you can typically choose to enable Application Insights. If not, you can add it later through the Azure portal:

  1. Navigate to your Function App in the Azure portal.
  2. Under "Settings", select "Application Insights".
  3. Click "Turn on Application Insights".
  4. Follow the prompts to create or link an existing Application Insights resource.
💡 Tip: Ensure your AzureWebJobsDashboard or APPINSIGHTS_INSTRUMENTATIONKEY (or APPINSIGHTS_CONNECTION_STRING) application setting is correctly configured in your Function App for telemetry to flow to Application Insights.

Querying Telemetry with Log Analytics

Application Insights integrates with Azure Log Analytics, allowing you to write powerful Kusto Query Language (KQL) queries to explore your telemetry data.

Common Log Analytics Tables:
Example KQL Query to find failed requests in the last hour:

requests
| where timestamp > ago(1h)
| where success == false
| summarize count() by operation_Name, resultCode
| order by count_ desc
            
Azure Functions Application Insights Overview

Azure Monitor Alerts

Leverage Azure Monitor to create alerts based on Application Insights data. This allows you to proactively respond to issues.

Alerting Scenarios:

Creating an Alert Rule:

  1. Navigate to your Application Insights resource.
  2. Under "Monitoring", select "Alerts".
  3. Click "+ Create alert rule".
  4. Configure the "Scope", "Condition" (based on your KQL query), and "Actions" (e.g., send an email, trigger a webhook).

Logging within Functions

Writing effective logs within your function code is fundamental to monitoring. Use the built-in logging capabilities provided by the Azure Functions runtime.

Example (JavaScript):


module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? 'Hello, ' + name + '. This HTTP triggered function executed successfully.'
        : 'This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.';

    if (!name) {
        context.log.warn('No name provided in the request.');
    } else {
        context.log.info(`Processing request for: ${name}`);
    }

    context.res = {
        status: 200,
        body: responseMessage
    };
};
            
📝 Note: Different languages have specific logging APIs. Refer to the Azure Functions documentation for your chosen language.

Distributed Tracing

For complex applications involving multiple functions and services, distributed tracing helps you understand the flow of requests across different components.

Monitoring Consumption vs. Premium/App Service Plans

While Application Insights is the primary tool for both, consider the differences:

Best Practices for Monitoring