Monitoring Azure Functions
Effective monitoring is crucial for understanding the health, performance, and usage of your Azure Functions. This section explores the tools and techniques available to monitor your serverless applications.
Application Insights
Azure Application Insights is a powerful Application Performance Management (APM) service that provides deep insights into the performance of your live applications. For Azure Functions, it offers:
- Live Metrics Stream: Real-time monitoring of requests, failures, and server response times.
- Performance Analysis: Identifying performance bottlenecks and slow dependencies.
- Failure Analysis: Diagnosing and analyzing exceptions and errors.
- Usage Tracking: Understanding how users interact with your functions.
- Custom Telemetry: Sending custom events and metrics from your function code.
Enabling Application Insights
When you create an Azure Function App, you can easily integrate it with Application Insights. If your Function App is already created, you can enable it from the Azure portal:
- Navigate to your Function App in the Azure portal.
- Under the "Monitoring" section, select "Application Insights".
- Click "Turn on Application Insights" and follow the prompts to create or link an existing Application Insights resource.
Azure Monitor
Azure Monitor is a comprehensive solution for collecting, analyzing, and acting on telemetry from your cloud and on-premises environments. It aggregates logs and metrics from Azure Functions and other Azure services.
- Metrics: Key performance indicators like function executions, data in/out, and execution units.
- Logs: Detailed information about function execution, errors, and custom logs written from your code.
- Alerts: Setting up rules to notify you when certain conditions are met (e.g., high error rate, increased latency).
Diagnostic Logging
Azure Functions provide detailed diagnostic logs that can be sent to various destinations, including Application Insights, Azure Storage, or Event Hubs. This is essential for debugging and troubleshooting.
Log Levels
You can configure the log level for your functions to control the verbosity of the logs generated:
- Error: Only logs errors.
- Warning: Logs warnings and errors.
- Information: Logs informational messages, warnings, and errors.
- Debug: Logs debug messages, informational messages, warnings, and errors.
The host.json
file can be used to configure the logging levels.
{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Information",
"MyNamespace.MyFunction": "Debug"
}
}
}
Key Metrics to Monitor
When monitoring Azure Functions, pay attention to the following key metrics:
- Function Executions: The total number of times your functions have been executed.
- Execution Count: Number of successful executions.
- Failed Executions: Number of executions that resulted in an error.
- Average Duration: The average time it takes for a function to execute.
- Max Duration: The maximum time any single execution took.
- Memory Working Set: The amount of memory your function instances are consuming.
- HTTP 5xx Errors: Server-side errors returned by HTTP-triggered functions.
Best Practice: Regularly review your monitoring data to proactively identify and address potential issues before they impact your users.
Visualizing and Analyzing Data
Application Insights provides powerful dashboards, workbooks, and the "Analyze" blade for deep dives into your telemetry data. You can use Kusto Query Language (KQL) to write custom queries for logs and metrics.
Example KQL Query for Function Errors:
requests
| where success == "False"
| project timestamp, name, url, resultCode, duration, operation_Name
| order by timestamp desc